--[[  v1.0
2024-01-19 Scripted by Koentje

 ${lua_parse log /var/log/auth.log 4 48 78 1580}\
                                   ^  ^ ^  ^
                                   |  | |  |
                                   |  | |  |_____ ${goto xxx} position of text (because normal goto only works for first line)
                                   |  | |_________ Ending point on a line
                                   |  |___________ Starting point on a line
                                   |______________ How many lines shown of the log
]]--



function conky_log(file, lines, starting, ending, igoto)
   if file == nil then
      return "ERROR: No path set to logfile !"
   end

   -- Set standard values
   if lines == nil then lines = 5 end
   if starting == nil  then starting = 0 end
   if ending == nil then ending = 50 end
   if igoto == nil then igoto = 100 end

   -- Run command to show log
   if io.open(file, "r") == nil then
      print("ERROR: Could not find '".. file .."' !") os.exit()
   else
      cmd="echo \"$(cat ".. file .." | tail -n".. lines .." | awk '{ print substr($0, ".. starting ..", ".. ending ..")}')\" | awk '{print \"${goto ".. igoto .."}\"$0}'"
      local handle = io.popen(cmd)
      local output = handle:read("*a")
      local result = output:gsub('[\r]', '')
      handle:close()
--      print(result)
      return (result)
   end
end
