-- This file contains wrappers for hilbish.run for common POSIX utilities.

local M = {}

M.pwd = function()
    return hilbish.run("pwd", false)
end

M.touch = function(file, opts)
    local cmd = "touch "
    if opts then
        if opts.sudo ~=nil and opts.sudo == true then
            cmd = "sudo " .. cmd
        end
        if opts.change_mod_time ~= nil and opts.change_mod_time == true then
            cmd = cmd .. "-m "
        end
        if opts.change_access_time ~= nil and opts.change_access_time == true then
            cmd = cmd .. "-a "
        end
        if opts.flags ~= nil then
            cmd = cmd .. opts.flags .. " "
        end
    end
    cmd = cmd .. file
    return hilbish.run(cmd, false)
end

M.cp = function(file1, file2, opts)
    local cmd = "cp "
    if opts then
        if opts.sudo ~= nil and opts.sudo == true then
            cmd = "sudo " .. cmd
        end
        if opts.force ~= nil and opts.force == true then
            cmd = cmd .. "-f "
        end
        if opts.recursive ~= nil and opts.recursive == true then
            cmd = cmd .. "-r "
        end
        if opts.flags ~= nil then
            cmd = cmd .. opts.flags .. " "
        end
    end

    cmd = cmd .. file1 .. " " .. file2
    return hilbish.run(cmd, false)
end

M.rm = function(file, opts)
    local cmd = "rm "
    if opts then
        if opts.sudo ~= nil and opts.sudo == true then
            cmd = "sudo " .. cmd
        end
        if opts.force ~= nil and opts.force == true then
            cmd = cmd .. "-f "
        end
        if opts.recursive ~= nil and opts.recursive == true then
            cmd = cmd .. "-r "
        end
        if opts.flags ~= nil then
            cmd = cmd .. opts.flags .. " "
        end
    end

    cmd = cmd .. file
    return hilbish.run(cmd, false)
end

M.mv = function(file1, file2, opts)
    local cmd = "mv "
    if opts then
        if opts.sudo ~= nil and opts.sudo == true then
            cmd = "sudo " .. cmd
        end
        if opts.flags ~= nil then
            cmd = cmd .. opts.flags .. " "
        end
    end
    cmd = cmd .. file1 .. " " .. file2
    return hilbish.run(cmd, false)
end

M.find = function(dir, opts)
    local cmd = "find " .. dir .. " "
    if opts then
        if opts.depth ~= nil then
            cmd = cmd .. "-depth " .. opts.depth .. " "
        end
        if opts.maxdepth ~= nil then
            cmd = cmd .. "-maxdepth " .. opts.maxdepth .. " "
        end
        if opts.type ~= nil then
            cmd = cmd .. "-type " .. opts.type .. " "
        end
        if opts.name ~= nil then
            cmd = cmd .. "-name " .. opts.name .. " "
        end
        if opts.flags ~= nil then
            cmd = cmd .. opts.flags .. " "
        end
    end
    return hilbish.run(cmd, false)
end

M.dirname = function(dir)
    return hilbish.run("dirname " .. dir, false)
end

M.sed = function(script, file, opts)
    local cmd = "sed -e '" .. script .. "' "
    if opts then
        if opts.sudo ~= nil and opts.sudo == true then
            cmd = "sudo " .. cmd
        end
        if opts.script_file ~= nil then
            cmd = cmd .. "-f " .. opts.script_file .. " "
        end
        if opts.suppress_default ~= nil and opts.suppress_default == true then
            cmd = cmd .. "-n "
        end
        if opts.flags ~= nil then
            cmd = cmd .. opts.flags .. " "
        end
    end
    cmd = cmd .. file
    return hilbish.run(cmd, false)
end

M.cat = function(files, opts)
    local cmd = "cat " .. files .. " "
    if opts then
        if opts.sudo ~= nil and opts.sudo == true then
            cmd = "sudo " .. cmd
        end
        -- The following two options are mutually exclusive
        if opts.output_file ~= nil then
            cmd = cmd .. "> " .. opts.output_file
        elseif opts.append_file ~= nil then
            cmd = cmd .. ">> " .. opts.append_file
        end
    end
    return hilbish.run(cmd, false)
end

return M
