require("luamods/command")
require("luamods/layout")
require("luamods/cgi")
require("config/network_conf")
lfs = require("lfs")

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

interfaces = {}
vlans = {}

interfaceCount = 0
vlanCount = 0
nextvlanid = 100

for file in lfs.dir(network_conf.netctl_profile_path) do
    local f = network_conf.netctl_profile_path..'/'..file
    local attr = lfs.attributes (f)
    if attr.mode ~= "directory" then
      profile = {}
      profile.name = file
      config = command.ex("cat "..f)
      profile.config = config
      profile.method = config:match("[^#]IP=(.-)\n") or "none"
      profile.type = config:match("[^#]Connection=(.-)\n")
      if profile.type == "mobile_ppp" then
        profile.method = "ppp"
      end
      if profile.type == "vlan" then
         --Suck some VLAN specific stuff
         profile.vlanid = config:match("[^#]VLANID=(.-)\n")
         profile.vlaninterface = config:match("[^#]BindsToInterfaces=(.-)\n") or ""
         profile.parent = config:match("[^#]After=%('(.-)'%)\n") or ""
      end
      local address = config:match("[^#]Address=%('(.-)/(.-)'%)\n") or config:match("[^#]Address=\"(.-)/(.-)\"\n") or nil
      local netmask = config:match("[^#]Address=%('.-/(.-)'%)\n") or config:match("[^#]Address=\".-/(.-)\"\n") or nil
      local gateway = config:match("[^#]Gateway='(.-)'\n") or nil
      profile.address = address
      profile.netmask = netmask
      profile.gateway = gateway
      profile.enabled = command.ex("netctl is-enabled "..file):match("(enabled)") or ""
      profile.interface = config:match("[^#]Interface=(.-)\n") or ""
      if profile.type ~= "vlan" then
         table.insert(interfaces,profile)
         interfaceCount = interfaceCount +1
      else
         table.insert(vlans,profile)
         if tonumber(profile.vlanid) >= nextvlanid then
            nextvlanid = tonumber(profile.vlanid) + 1
         end
         vlanCount = vlanCount +1
      end   
    end
end

content = ""

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

if (interfaceCount > 0) then
    content = content .. [[
                    <div>
                        <b><br>
                        <center>
                            This is a list of all network profiles<br>
                            managed with netctl <br><br>
                            Click on the profile names to manage them.</b>
                        </center><br>
                    
                        <TABLE border="1" class="onetwo">
                            <TR>
                                <TH>Profile name</TH>
                                <TH>Method</TH>
                                <TH>Enabled</TH>
                                <TH>Link State</TH>
                                <TH>IP configuration</TH>
                            </TR>
]]
      for idx,profile in ipairs(interfaces) do
        content = content .. [[
                            <TR>
]]                            
        if profile.method == "ppp" then
           content = content .. [[
                                <TD><a href="network_ppp.lua?interface=]]..profile.name..[[">]]..profile.name..[[ (]]..profile.interface..[[)<img src="images/spanner.png"/></a></TD>
]]
        else
           content = content .. [[
                                <TD><a href="network_manage.lua?interface=]]..profile.name..[[">]]..profile.name..[[ (]]..profile.interface..[[)<img src="images/spanner.png"/></a></TD>
]]
        end                                  
        content = content .. [[
                                <TD>]]..profile.method..[[</TD>
]]

        if profile.enabled == "enabled" then
            content = content .. [[
                                <TD>Yes</TD>
]]
        else
            content = content .. [[
                                <TD>No</TD>
]]
        end

        local result = command.ex("ip link show dev "..profile.interface.."  2>&1")
        local if_state = result:match(".+state%s(%u-)%s.+") or ""
        if (result:match("error fetching interface information") ~= nil) then
            content = content .. [[
                                <TD>No</TD>
]]
        else
            content = content .. [[
                                <TD>]]..if_state..[[</TD>
]]
        end

            content = content..[[
                                <TD>
]]

        if (profile.method == "static") then
            local address
            local gateway
            local netmask
            if profile.address ~= nil then address = profile.address else address = "none" end
            if profile.gateway ~= nil then gateway = profile.gateway else gateway = "none" end
            if profile.netmask ~= nil then netmask = profile.netmask else netmask = "none" end
            content = content..[[
                                    <b>Address:</b> ]]..address..[[<BR>
                                    <b>Netmask:</b> ]]..netmask..[[<BR>
                                    <b>Gateway:</b> ]]..gateway..[[<BR>
]]
        elseif (profile.method == "dhcp") then
            content = content..[[
                                    Obtain from DHCP
]]
        elseif (profile.method == "ppp") then
            content = content..[[
                                    Obtain from PPP
]]
        end

        content = content .. [[
                                </TD>
                            </TR>
]]                        
    end

    content = content..[[
                        </TABLE>
                    </div>
]]


    content = content .. [[
                    <div>
                        <b><br>
                        <center>
                            Virtual Interfaces<br><br>
                            Click on the profile names to manage them.</b>
                        </center><br>
                    
                        <TABLE border="1" class="onetwo">
                            <TR>
                                <TH>Profile name</TH>
                                <TH>Method</TH>
                                <TH>Enabled</TH>
                                <TH>Link State</TH>
                                <TH>IP configuration</TH>
                            </TR> ]]
      
      for idx,profile in ipairs(vlans) do
        content = content .. [[
                            <TR>
                                <TD><a href="network_vlan.lua?interface=]]..profile.name..[[&parent=]]..profile.parent..[[">]]..profile.name..[[ (]]..profile.parent..[[-]]..profile.vlaninterface..[[)<img src="images/spanner.png"/></a></TD>
                                <TD>]]..profile.method..[[</TD>
]]

        if profile.enabled == "enabled" then
            content = content .. [[
                                <TD>Yes</TD>
]]
        else
            content = content .. [[
                                <TD>No</TD>
]]
        end

        local result = command.ex("ip link show dev "..profile.interface.."  2>&1")
        local if_state = result:match(".+state%s(%u-)%s.+") or "DOWN"
        if (result:match("error fetching interface information") ~= nil) then
            content = content .. [[
                                <TD>No</TD>
]]
        else
            content = content .. [[
                                <TD>]]..if_state..[[</TD>
]]
        end

            content = content..[[
                                <TD>
]]

        if (profile.method == "static") then
            local address
            local gateway
            local netmask
            if profile.address ~= nil then address = profile.address else address = "none" end
            if profile.gateway ~= nil then gateway = profile.gateway else gateway = "none" end
            if profile.netmask ~= nil then netmask = profile.netmask else netmask = "none" end
            content = content..[[
                                    <b>Address:</b> ]]..address..[[<BR>
]]
        elseif (profile.method == "dhcp") then
            content = content..[[
                                    Obtain from DHCP
]]
        else
            content = content..[[
                                    Obtain from Outer Space
]]
        end

        content = content .. [[
                                </TD>
                            </TR>
]]                        
    end
    content = content .. [[      
                        </TABLE>
                        <br>
                        <input type="submit" name="newButton" value="Add VLAN" onclick="window.location.assign('network_vlan.lua?vlanid=]]..nextvlanid..[[');"/>
                     </div> 
                       
]]


else
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> No interfaces found</h2>
                            Either the interfaces file "]]..network_conf.interface_file..[[" is missing or unreadable,<br>
                            or none of the interfaces have been marked as being configurable.<br><br>
                            <a href="network_status.lua">View network status</a> for more information.
                        </center>
                    </div>]]
end




if (intf ~= "") and (intf ~= nil) then
    content = content .. [[
                    <script>
                        function toggleVis(target)
                        {
                            if (target.style.display == "none")
                            {
                                target.style.display = "inline-block";
                            }
                            else
                            {
                                target.style.display = "none";
                            }
                            return false;
                        }
                    </script>
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('fileContents'));">View ]]..network_conf.interface_file..[[</a></a><br>
                    <div id="fileContents" style="display: none;">
                        <pre class="formLayoutfull">
]]..intf..[[
                        </pre>
                    </div>
]]
end

print(header..content..footer)
