require("luamods/command")
require("luamods/layout")
require("luamods/cgi")
require("config/software_conf")

userLevel = db.getPriv(layout.username) or 1

if (userLevel == 1) then
    buttonControl = " disabled"
    inputControl = " readonly"
    scriptControl = "//"
else
    buttonControl = ""
    inputControl = ""
    scriptControl = ""
end

rtr = ""
content = ""

header = layout.getHeader()
footer = layout.getFooter()

if (cgi.post.installButton == nil) then
    pkgName = cgi.get.name
    pkgVer = cgi.get.version
    pkgInstalled = cgi.get.installed
    pkgUpgrade = cgi.get.upgrade
else
    pkgName = cgi.post.name
    pkgVer = cgi.post.version
    pkgInstalled = cgi.post.installed
    pkgUpgrade = cgi.post.upgrade
end    

if (pkgName == "") then pkgName = nil end
if (pkgVer == "") then pkgVer = nil end

if (pkgName == nil) or (pkgVer == nil) then
    -- Bail on error
    content = [[
                    <div id="info"><center><H1>Software - Package Info</H1></center></div>
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Missing package details</h2>
]]
    if (pkgName == nil) then content = content..[[
                            No package name was specified<br>
]]
    end
    if (pkgVer == nil) then content = content..[[
                            No package version was specified<br><br>
]]
    end
content = content..[[
                            <a href="software_all.lua">View available packages</a> | <a href="software_installed.lua">View installed packages</a><br>
                        </center>
                    </div>
]]
    html = header..content..footer
    print(html)
    return    
end

if (userLevel ~= 1) then
    if (cgi.post.installButton == "Install") then
        installResult = command.ex(software_conf.opkg_install.." "..pkgName.." 2>&1")
    elseif (cgi.post.installButton == "Reinstall") then
        installResult = command.ex(software_conf.opkg_install.." "..pkgName.." 2>&1")
    elseif (cgi.post.installButton == "Uninstall") then
        installResult = command.ex(software_conf.opkg_uninstall.." "..pkgName.." 2>&1")
    end
end    

allInfo = command.ex(software_conf.opkg_info.." "..pkgName)
matchedPkg = {}
matchedPkg.status = pkgInstalled
matchedPkg.filename = "Does not matter"
matchedPkg.size= "Size really does not matter"
matchedPkg.upgrade = pkgUpgrade
command.ex([[echo "]]..allInfo..[[" > /var/log/lighttpd_cgi.log]])
infoLines = command.split("\n",allInfo)
--[[
for pkgInfo in allInfo:gmatch("%s*(Package:.-)\n\n") do
    for tmpName, tmpVer in pkgInfo:gmatch("Package:%s*([^\n]+)%s*\nVersion:%s*([^\n]+)%s*\n") do
        if (tmpName == pkgName) and (tmpVer == pkgVer) then
            matchedPkg = {}
            matchedPkg.depends = pkgInfo:match("Depends: ([^\n]*)\n") or ""
            matchedPkg.provides = pkgInfo:match("Provides: ([^\n]*)\n") or ""
            matchedPkg.status = pkgInfo:match("Status: ([^\n]*)\n") or ""
            matchedPkg.section = pkgInfo:match("Section: ([^\n]*)\n") or ""
            matchedPkg.arch = pkgInfo:match("Architecture: ([^\n]*)\n") or ""
            matchedPkg.maintainer = pkgInfo:match("Maintainer: ([^\n]*)\n") or ""
            matchedPkg.md5 = pkgInfo:match("MD5Sum: ([^\n]*)\n") or ""
            matchedPkg.size = pkgInfo:match("Size: ([^\n]*)\n") or ""
            matchedPkg.filename = pkgInfo:match("Filename: ([^\n]*)\n") or ""
            matchedPkg.source = pkgInfo:match("Source: ([^\n]*)\n") or ""
            matchedPkg.desc = pkgInfo:match("Description: ([^\n]*)\n?") or ""
        end
    end
end
]]
if (matchedPkg == nil) then
    -- Bail on error
    content = [[
                    <div id="info"><center><H1>Software - Package Info</H1></center></div>
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> No package info</h2>
                            Could not obtain information for the specified package.<br><br>
                            <b>Name:</b> ]]..pkgName..[[<br>
                            <b>Version:</b> ]]..pkgVer..[[<br><br>
                            <a href="software_all.lua">View available packages</a> | <a href="software_installed.lua">View installed packages</a><br>
                        </center>
                    </div>
]]
    html = header..content..footer
    print(html)
    return    
end

if (installResult ~= nil) and (installResult ~= "") then
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2>Installation Results</h2>
                        </center>
                    </div>
                    <pre class="formLayoutfull">
]] .. installResult .. [[
                    </pre>
]]
end

content = content .. [[
                    <div id="info">
                        <div style="background:url(images/software.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>Software - Package Info</h1></center>
                        </div>
                    </div>
]]

if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</B> Editing this section requires administrator privileges.</center><br>
]]                    
end


content = content..[[
                    <div class="formLayoutfull">]]
                     for k,line in ipairs(infoLines) do
content = content..[[<b>]]..line..[[</b><br>]]                     
                     end   
                        --<b>Package name: </b>]]..pkgName..[[<br>
                        --<b>Version: </b>]]..pkgVer..[[<br>
                        --<b>Dependencies: </b>]]..matchedPkg.depends..[[<br>
                        --<b>Provides: </b>]]..matchedPkg.provides..[[<br>
                        --<b>Status: </b>]]..matchedPkg.status..[[<br>
                        --<b>Section: </b>]]..matchedPkg.section..[[<br>
                        --<b>Architecture: </b>]]..matchedPkg.arch..[[<br>
                        --<b>Maintainer: </b>]]..matchedPkg.maintainer..[[<br>
                        --<b>MD5 Sum: </b>]]..matchedPkg.md5..[[<br>
                        --<b>Size: </b>]]..matchedPkg.size..[[<br>
                        --<b>Filename: </b>]]..matchedPkg.filename..[[<br>
                        --<b>Source: </b>]]..matchedPkg.source..[[<br>
                        --<b>Description: </b>]]..matchedPkg.desc..[[<br>
content = content..[[                        
                    </div>
                    <br>
                    <form action="software_pkg_info.lua" method="post">
                        <input type="hidden" name="name" value="]]..pkgName..[["/>
                        <input type="hidden" name="version" value="]]..pkgVer..[["/>
]]

if (matchedPkg.status == "installed") then
    if matchedPkg.upgrade == "up-to-date" then
        content = content..[[
                        This package is installed and up to date.<br>
                        <input type="submit" value="Reinstall" name="installButton" onclick="return confirm('Are you sure you want to reinstall this package?');"]]..buttonControl..[[/>
                        <input type="submit" value="Uninstall" name="installButton" onclick="return confirm('Are you sure you want to remove this package?');"]]..buttonControl..[[/>
]]
    else
        content = content..[[
                        The package is installed, but the version is not up to date.<br>
                        Either a newer version is available for installation, or the package has been deprecated.<br><br>
                        <input type="submit" value="Reinstall" name="installButton" onclick="return confirm('This will look for a newer version of the package and install it. Proceed?');"]]..buttonControl..[[/>
                        <input type="submit" value="Uninstall" name="installButton" onclick="return confirm('Are you sure you want to remove this package?');"]]..buttonControl..[[/>]]
    end
else
    content = content..[[
                        This package is not installed<br>
                        <input type="submit" value="Install" name="installButton" onclick="return confirm('Are you sure you want to install this package?');"]]..buttonControl..[[/>
]]                    
end

content = content..[[
                    </form>
]]

print(header .. content .. footer)
