os.loadAPI('/dop/apis/turtleAPI')

local ex = true
local sesName = ""

function append(file,txt)
  local f = io.open("/prog/save/"..file,"a")
  if f then
    f:write(txt)
    f:close()
  end
end

function load(file)
  local f = io.open("/prog/save/"..file,"r")
    txt=f:read('*a')
	f:close()
  return txt;
end

local tFunc = {
	["f"] = turtleAPI.forward,
	["b"] = turtleAPI.back,
	["u"] = turtleAPI.up,
	["d"] = turtleAPI.down,
	["l"] = turtleAPI.left,
	["r"] = turtleAPI.right,
  ["sel"] = turtle.select,
  ["dig"] = turtleAPI.dig,
  ["digu"] = turtleAPI.digUp,
  ["digd"] = turtleAPI.digDown,
  ["p"] = turtleAPI.place,
  ["pu"] = turtleAPI.placeUp,
  ["pd"] = turtleAPI.placeDown,
  ["cls"] = turtleAPI.clearInv
}

function _no(n)end

local bFunc = {
	["f"] = turtleAPI.back,
	["b"] = turtleAPI.forward,
	["u"] = turtleAPI.down,
	["d"] = turtleAPI.up,
	["l"] = turtleAPI.right,
	["r"] = turtleAPI.left,
  ["sel"] = _no,
  ["dig"] = _no,
  ["digu"] = _no,
  ["digd"] = _no,
  ["p"] = _no,
  ["pu"] = _no,
  ["pd"] = _no,
  ["cls"] = turtleAPI.clearInv
}

function go(paramStr,_back)
  paramStr = string.lower(paramStr)
  if string.sub(paramStr,-1)~=" " then paramStr = paramStr.." " end
  print(paramStr)
  if string.len(sesName)>0 then
    append(sesName,paramStr)
  end
  ar = {}
  max = 0
  for i in string.gmatch(paramStr,'(%w+) ',l) do
    ar[max] = i
    max = max+1
  end
  
  local n,step
  if _back
    then n = #ar;step = -1;Func = bFunc
    else n = 0;step = 1; Func = tFunc
  end
  
  if _back then
    n = #ar; 
    while n>=0 do
	    local nD = 1
      if n>=0 then 
        local num = tonumber( ar[n] )
	  	  if num then
			    nD = num
  			  n = n -1
		    end
      end
      local sD = ar[n]
      n = n - 1
      if sD=="cls"
       then bFunc[sD]()
       else bFunc[sD](nD)
      end
    end
  else
    while n <= #ar do
      local sD = ar[n]
	    local nD = 1
	    if n <= #ar then
    		local num = tonumber( ar[n + 1] )
	    	if num then
		  	  nD = num
			    n = n + 1
		    end
	    end
	    n = n + 1
      if sD=="cls"
       then tFunc[sD]()
       else tFunc[sD](nD)
      end
    end
  end
end

local ct = 1
local ctN = 1
term.clear()
while ex do
  term.setCursorPos(1,1)
  term.write('Asses File = '..sesName)
  term.setCursorPos(26,1)
  term.write('Count = '..ctN)  
  print('')
  print('base info: exit,help,command')
  term.setCursorPos(1,12)
  write('> ')
  s = string.lower(read())
  term.clear()
  if string.find(s,' ')
   then t = string.sub(s,1,string.find(s,' ')-1)
   else t = s
  end
  
  while ct>0 do
    ctN = 1
    if t == 'exit' then
      ex = false
    elseif t == 'count' then
      ctN = tonumber(string.sub(s,#t+2))
      if ctN then
        if ctN>99 then ctN = 99 end
      else
        ctN =1 
      end;      
    elseif t == 'help' then
      term.setCursorPos(1,13)
      print('')
      print('assign <NameFile>')
      print('clear')
      print('close')
      print('mv [<NameFile>]')
      print('bk [<NameFile>]')
      print('go [<NameFile>]')
      print('back [<NameFile>]')
      print('count <Num>')
      print('<command> [<Count>] ...')
      --print('')
    elseif t =='command' then
      term.setCursorPos(1,13)
      print('l - Left      |  r - Right')
      print('u - Up        |  d - Down')
      print('f - Forward   |  b - Back')
      print('sel - Select  |  dig - Dig')
      print('digu - DigUp  |  digd - DigDown')
      print('pu - PlaceUp  |  pd - PlaceDown')
      print('p - Place     |  cls - ClearInv')
      print('')
    elseif t == 'assign' then
      sesName = string.sub(s,#t+2)
    elseif t =='clear' and string.len(sesName)>0 then
      local f = io.open("/prog/save/"..sesName,"w")
      if f then
        f:write('')
        f:close()
      end
    elseif t == 'close' then
      sesName = ""
    elseif t == 'mv' then
      ss = string.sub(s,#t+2)
      if not ss then ss=sesName end
      if string.len(ss)>0 then
        tt = load(ss)
        go(tt,false)
        os.sleep(5)
        go(tt,true)
      end
    elseif t == 'bk' then
      ss = string.sub(s,#t+2)
      if not ss then ss=sesName end
      if string.len(ss)>0 then
        tt = load(ss)
        go(tt,true)
        os.sleep(5)
        go(tt,false)
      end
    elseif t == 'go' then
      ss = string.sub(s,#t+2)
      if not ss then ss=sesName end
      if string.len(ss)>0 then
        go(load(ss),false)
      end
    elseif t == 'back' then
      ss = string.sub(s,#t+2)
      if not ss then ss=sesName end
      if string.len(ss)>0 then 
        go(load(ss),true)
      end
    elseif #t>0 then
      go(s,false)
    end
    ct = ct - 1
  end
  ct = ctN;
end
