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

rtr = ""

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

function parsePackages()
    rtr = command.ex(software_conf.opkg_all_pack)
    
    if (rtr == nil) then rtr = "" end
    packages = {}
    for line in rtr:gmatch("(.-\n)") do
        name,version,desc = line:match("([^%s]+) %- ([^%s]+)%s*%-?%s*([^\n]*)\n")
        if (name ~= nil) and (version ~= nil) then
            package = {}
            package.name = name
            package.version = version
            package.desc = desc or ""
            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 - All Available Packages</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 available on the repository server.<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 available packages as CSV</a><br><br>
                        <table class="onetwo" border="1">
                            <tr>
                                <th>Package name</th>
                                <th style="white-space:nowrap;" >Version</th>
                                <th>Description</th>
                            </tr>
]]
    for num, package in pairs(pkgs) do
        content = content .. [[
                            <tr>
                                <td><a href="software_pkg_info.lua?name=]]..package.name..[[&version=]]..package.version..[[">]]..package.name..[[</a></td>
                                <td style="white-space:nowrap;">]]..package.version..[[</td>
                                <td>]]..package.desc..[[</td>
                            </tr>
]]                            
    end
    content = content .. [[
                        </table><br>
                        <a href="software_export_all.lua">Export list of available packages as CSV</a>

                    </div>
]]
else
    content = content .. [[
                <div id="info">
                    <center>
                        <h2><img src="images/warning.png"/> No available packages</h2>
                        Either the package manager (opkg-cl) is not responding,<br>
                        or there are no packages in the repository.
                    </center>
                </div>
]]
end


print(header .. content .. footer)
