--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- A small utility function to generate thumbnails for images
local spawn = require("awful.spawn")
local thumbnailer = {}
-- annoying shit but that's what happens
if _VERSION ~= "Lua 5.1" then
    execute = function(comm)
        return select(3,os.execute(comm))
    end
else
    execute = os.execute
end
thumbnailer.generate = function(path,thumb_path,height)
    assert(type(path) == "string", "argumenr #1 (path) is not a string")
    assert(type(thumb_path) == "string", "argument #2 (thumbnail path) is not a string")
    assert(type(height) == "number","argument #3 (height) is not a number")
    if execute("ls "..path) ~= 0 then
        return false, "unable to access image directory"
    end
    if execute("ls "..thumb_path) == 0 then  
        return true
    end
    if execute("mkdir "..thumb_path) ~= 0 then
        return false, "unable to create thumbnail directory"
    end
    spawn("mogrify -thumbnail x"..tostring(height).." -path '"..thumb_path.."' '"..path.."'/*.{jpg,png}")
    return true
end

return thumbnailer
