local args = {...}
local selectedSlot = 1
turtle.select(1)
local count = 1
local loadedActions = {}
local actions = {}

-- start reading Actions
local h = fs.open(args[1],"r")
local line = h.readLine()
while line do
local commandCollection = {}
for command in line:gmatch("[^ ]+") do
    table.insert(commandCollection,command)
end
table.insert(loadedActions,commandCollection)
line = h.readLine()
end
h.close()
-- end reading Actions
-- common functions
local slot = 1
function selectSlot(arg_slot)
  if arg_slot>16 then
    error("Max slots is 16!")
  end
  slot = arg_slot
  turtle.select(slot)
end
function nextSlot()
  selectSlot(slot+1)
end
function slotNeeded()
  if slot == 1 then
    return turtle.getItemCount(1)<2
  end
  return not turtle.compareTo(1) or turtle.getItemCount(slot)==0
end

-- start list of Actions
actions["nothing"] = function() end
actions["n"] = actions["nothing"]
actions[""] = actions["nothing"]

-- Digging
dig = function(doDig)
  local lastSlot = slot
  selectSlot(16)
  doDig()
  selectSlot(lastSlot)  
end

actions["digdown"] = function() dig(turtle.digDown) end
actions["dd"] = actions["digdown"] --shorthand

-- Block placement
function place(doPlace)
  while slotNeeded() do
    nextSlot()
  end
  while not doPlace() do
    if slotNeeded() then
    nextSlot()
    else
      error("Something is in the way!")
    end
  end
end

actions["placedown"] = function() place(turtle.placeDown) end
actions["pd"] = actions["placedown"]


-- start loop
selectSlot(1)
while true do
  if not turtle.forward() then
    break
  end
  count = count + 1
  if count>#loadedActions then
    count = 1
  end
  for i,command in pairs(loadedActions[count]) do
    local cmd = actions[command]
    if cmd then
      cmd()
    else
      print(command)
    end
  end
end
