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

rtr = ""

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

testData = [[
solid 5.16.0-1 -> 5.17.0-1
sonnet 5.16.0-1 -> 5.17.0-2
source-highlight 3.1.8-3 -> 3.1.8-5
sqlitebrowser 3.7.0-1 -> 3.7.0-2
step 15.08.3-1 -> 15.12.0-1
strigi 0.7.8-7 -> 0.7.8-9
subversion 1.9.2-1 -> 1.9.3-1
taglib 1.10-1 -> 1.10-3
tdb 1.3.7-1 -> 1.3.8-1
]]
function parsePackages()
    rtr = command.ex(software_conf.opkg_update_needed)
    --rtr = testData
    if (rtr == nil) then rtr = "" end
    packages = {}
    for line in rtr:gmatch("(.-\n)") do
        name,version,newversion = line:match("(.-)%s+(.-)%s+%->%s+(.-)\n")
        if (name ~= nil) and (version ~= nil) then
            package = {}
            package.name = name
            package.version = version
            package.newversion = newversion
            table.insert(packages, package)
        end
    end
    return packages, #(packages)
end

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 - Current Status</h1></center>
                        </div>
                    </div>
]]

pkgs, pkgCount = parsePackages()

if (pkgCount > 0) then
    content = content .. [[
                    <div>
                        <b><br>
                        <center>
                            This is a list of all the software packages that has updated versions in therepository.<br>
                            Be sure to <a href="software_sources.lua">update the repository sources</a> periodically to get the latest versions.<br><br>
                            Click on the package names to manage individual software packages.</b>
                        </center><br>
                        <a href="software_export_all.lua">Export list of out of date packages as CSV</a><br><br>
                        <table class="onetwo" border="1">
                            <tr>
                                <th>Package name</th>
                                <th style="white-space:nowrap;" >Old Version</th>
                                <th style="white-space:nowrap;" >New Version</th>
                            </tr>
]]
    for num, package in ipairs(pkgs) do
        content = content .. [[
                            <tr>
                                <td><a href="software_pkg_info.lua?name=]]..package.name..[[&version=]]..package.version..[[&installed=installed&upgrade=upgrade">]]..package.name..[[</a></td>
                                <td style="white-space:nowrap;">]]..package.version..[[</td>
                                <td>]]..package.newversion..[[</td>
                            </tr>
]]                            
    end
    content = content .. [[
                        </table><br>
                        <a href="software_export_all.lua">Export list of out of date packages as CSV</a>

                    </div>
]]
else
    content = content .. [[
                <div id="info">
                    <center>
                        <h2><img src="images/ok.png"/> All packages seems to be up to date</h2>
                        Be sure to <a href="software_sources.lua">update the repository sources</a> periodically to get the latest versions.<br>
                        Also see the <a href="software_installed.lua"> list of installed packges</a> for further information
                    </center>
                </div>
]]
end


print(header .. content .. footer)
