local args = {...}

-- Get the printer!
local printer = peripheral.wrap("left")

if args[1]==nil then error("No file specified.") end
local floc = shell.resolve(args[1]) -- turn this into a fileselect dialog or something!

-- Does the file exist?
if not fs.exists(floc) then error("File not found!") end

-- Open file.
local fhandle = io.open(floc)

-- Read file an start printing, first line is title!
printer.newPage()
printer.setPageTitle(fhandle:read())

-- Get size (to know when to start a new page)
local width, height = printer.getPageSize()

function newline()
  local x,y = printer.getCursorPos()
  printer.setCursorPos(1,y+1)
end

--Start printing
local text = fhandle:read()
while text~=nil do
  
  --Start new page when required
  local x, y = printer.getCursorPos()
  if y+1>height then
    printer.endPage()
    if not printer.newPage() then
      error("Not enough paper")
    end
  end
  
  -- Print lines!
  --Unwritten code-- --WARNING--
  for c in text:gmatch"." do
    -- build strings  
  end
  newline()
  
  text = fhandle:read()  
end

printer.endPage()
fhandle:close()
 

