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

function parsePackages(searchTerm)
    rtr = command.ex(software_conf.opkg_all_pack.." "..searchTerm)

    if (rtr == nil) then rtr = "" end
    packages = {}
    
    for repo,pkg,ver,desc in string.gmatch(rtr,"(.-)/(.-)%s(.-)\n(.-)\n") do
       local installed = string.match(ver,"%[(installed).-%]") or "not-installed"
       local upgrade = string.match(ver,"%[installed(.+)%]") or nil
       ver = string.gsub(ver,"(%[installed.-%])","")
       ver = string.gsub(ver,"(%(.-%))","")
       ver = string.gsub(ver,"(%s)","")
       package = {}
       package.name = pkg
       package.version = ver
       package.desc = desc or ""
       package.installed = installed
       package.upgrade = upgrade or "up-to-date"
       table.insert(packages, package)
    end
    
    return packages, #(packages)
end

pkgName = cgi.get.pkgName or ""

if (pkgName == "") then
        content = [[
                <div id="info">
                    <center>
                        <h2>Blank search query received</h2>
                    </center>
                </div>
                <div>
                    <center>
                        You submitted a blank search query.<br>
                    </center>
                </div>
]]
end

pkgs, pkgCount = parsePackages(pkgName)

if (pkgName ~= "") then
    if (pkgCount > 0) then
        content = [[
                    <div id="info">
                        <center>
                            <h2>Search results</h2>
                        </center>
                    </div>
                    <div>
                        <center>
                            <b>The following packages match your query.<br>
                            Click on the package names to manage individual software packages</b>
                        </center><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..[[&installed=]]..package.installed..[[&upgrade=]]..package.upgrade..[[">]]..package.name..[[</a></td>
                                <td style="white-space:nowrap;">]]..package.version..[[</td>
                                <td>]]..package.desc..[[</td>
                            </tr>
]]                            
        end
        content = content .. [[
                        </table><br>

                    </div>
]]
    else
        content = [[
                <div id="info">
                    <center>
                        <h2>No search results</h2>
                    </center>
                </div>
                <div>
                    <center>
                        Could not find any packages that match your search query.<br>
                        You searched for "<b>]]..pkgName..[[</b>"<br>
                    </center>
                </div>
]]
    end
end

print(content)