--[[  v1.0
2025-12-10 Scripted by Koentje  (remon@cobrasoft.nl)


                                          ________________ vertical position in pixels
                                         |  ______________ horizontal position in pixels
                                         | |  ____________ width of image
                                         | | |    ________ height of image
                                         | | |   |    ____ Transparency (1-100)
                                         | | |   |   |   _ r for reversed animation (leave empty for normal)
                                         | | |   |   |  |
usage:  ${lua animation ./pathtopictures 2 2 300 300 50 r}

]]

require 'cairo'
require "imlib2"

-- path to anicalc BASH script
ANICALC="./scripts/anicalc"


-- Function to draw image
function gDrawImage(gcr,gpath,xx,yy,ww,hh)
    local img =  cairo_image_surface_create_from_png(gpath)
    local w_img = cairo_image_surface_get_width(img)
    local h_img = cairo_image_surface_get_height(img)
    cairo_save(gcr)
    cairo_translate (gcr, xx, yy)
    cairo_scale (gcr, ww/w_img, hh/h_img)
    cairo_set_source_surface (gcr, img, xx + 6, yy + 64)
    cairo_paint_with_alpha (gcr, .5)
    cairo_surface_destroy (img)
    collectgarbage ()
    cairo_restore (gcr)
  return 'finished'
end



-- Function to check if path exists
function isdir(gpath)
   if io.open(gpath, "r") == nil then
      print("\27[31mERROR:\27[m Could not find folder to animation files: '".. gpath .."' !") os.exit()
   else
      -- Check if path exist and if trailing slash. If so remove trailing slash
      local ghandle = io.popen("if [ -d \"".. gpath .."\" ]; then echo \"".. gpath .."\" | sed 's:/*$::'; fi")
      local output = ghandle:read("*a")
      local gresult = output:gsub('[\n\r]', '')
      ghandle:close()
      -- Return the correct path without trailing slash
      return gresult
   end
end



-- Main function
function conky_globe_animation(gpath,xxx,yyy,www,hhh,gfrstart,gfrstop)
    if conky_window==nil then return '' end
    local gcs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
    local gcr=cairo_create(gcs)
    local sort = ""

    if gpath == nil then print("\27[31mERROR:\27[m Path is empty! Check your conky text section.\27[m") os.exit() end
    local realgpath = isdir(gpath)

    -- Run anicalc bash script to get index of next PNG file
    os.execute(ANICALC .." \"".. realgpath .."\" \"".. sort .."\"")

    -- Uncomment below line for debug on terminal
    --os.execute("echo \"".. realpath .."\" \t = $(cat \"".. realpath .."\"/current/conky_slideshow_index)")


    -- Check if frame nr is not nil
    if gfrstart == nil then
      print("\27[31mERROR:\27[m No start frame nr for '".. gpath .."'")
      os.exit()
    end
    if gfrstop == nil then
      print("\27[31mERROR:\27[m No stop frame nr for '".. gpath .."'")
      os.exit()
    end
    gframestart = tonumber(gfrstart)
    gframestop  = tonumber(gfrstop)


    -- Get frame number from sconky_slideshow_index
    gindexfile="./images/space/current/conky_slideshow_index"
    if io.open(gindexfile, "r") == nil then
       print("ERROR: Could not find '".. gindexfile .."' !") os.exit()
    else
       gfrcmd="echo $(cat \"".. gindexfile .."\")"
       local gfrhandle = io.popen(gfrcmd)
       local output = gfrhandle:read("*a")
       local gfrresult = output:gsub('[\r]', '')
       gfrhandle:close()
       gframenr = tonumber(gfrresult)
    end

    -- To test
    -- print("framenr: "..gframenr, "framestart: "..gframestart, "  framestop: "..gframestop, "path: "..realgpath)


    -- If frame nr is between x and y then execute drawing
    if gframenr >= gframestart and gframenr <= gframestop then
      gDrawImage(gcr,realgpath .."/current/current.png",xxx,yyy,www,hhh)
      --print("Draw globe !")
    end


    cairo_surface_destroy(gcs)
    cairo_destroy(gcr)
  return ''
end
