require("luamods/command")
require("luamods/layout")
require("config/network_conf")

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

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

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

if (cgi.post.submitButton == "Save and Apply") and (userLevel ~= 1) then
    intfName = cgi.post.interface or "unknown"
    saved = 1
else
    intfName = cgi.get.interface or "unknown"
end

currIntf = command.ex("cat "..network_conf.netctl_profile_path.."/"..intfName)
if (currIntf ~= nil) then
    --ifconfigResult = command.ex(network_conf.if_config.." "..intfName.." 2>&1") or ""
    interface = {}
     interface.name = intfName
     interface.enabled = command.ex("netctl is-enabled "..interface.name):match("(enabled)") or ""
     interface.apn =  currIntf:match("[^#]AccessPointName=(.-)\n") or "internet"
     interface.pin =  currIntf:match("[^#]Pin=(.-)\n") or ""
     interface.user =  currIntf:match("[^#]User=['\"]*(.-)['\"]*\n") or ""
     interface.password =  currIntf:match("[^#]Password=['\"]*(.-)['\"]*\n") or ""
     
    if (saved == 1) then
        newPin = cgi.post.pin or ""
        newAPN = cgi.post.apn or ""
        newUser = cgi.post.username or ""
        newPasswd = cgi.post.password or "" 

        if (cgi.post.profile == "enabled") then
            newEnabled = "enabled"
        else
            newEnabled = ""
        end
        
        if newPin ~= "" then
           currIntf = currIntf:gsub("#Pin","Pin")
           currIntf = currIntf:gsub("Pin=.-\n","Pin="..newPin.."\n")
        else
           currIntf = currIntf:gsub("[^#]Pin","\n#Pin")
        end
        if newAPN ~= "" then
           currIntf = currIntf:gsub("#AccessPointName","AccessPointName")
           currIntf = currIntf:gsub("AccessPointName=.-\n","AccessPointName="..newAPN.."\n")
        else
           currIntf = currIntf:gsub("[^#]AccessPointName","\n#AccessPointName")
        end
        if newUser ~= "" then
           currIntf = currIntf:gsub("#User","User")
           currIntf = currIntf:gsub("User=.-\n","User='"..newUser.."'\n")
        else
           currIntf = currIntf:gsub("[^#]User","\n#User")
        end
        if newPasswd ~= "" then
           currIntf = currIntf:gsub("#Password","Password")
           currIntf = currIntf:gsub("Password=.-\n","Password='"..newPasswd.."'\n")
        else
           currIntf = currIntf:gsub("[^#]Password","\n#Password")
        end
        
     command.writefile(network_conf.netctl_profile_path.."/"..intfName, currIntf)

     if newEnabled ~= interface.enabled then
        if newEnabled == "enabled" then
          command.ex([[netctl enable ]]..intfName)
        else
          command.ex([[netctl disable ]]..intfName)
        end
     end

        interface.apn = newAPN
        interface.pin = newPin
        interface.user = newUser
        interface.password = newPasswd
        interface.enabled = newEnabled
        
        if interface.enabled == "enabled" then
           command.ex([[netctl restart ]]..intfName)
        else
           command.ex([[netctl stop ]]..intfName)
        end
    end
    ifconfigResult = currIntf
    
    
else
    -- Bail on error here
    content = [[
                    <div id="info"><center><H1>Network - Manage Interface</H1></center></div>
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Cannot load interface details</h2>
                            An invalid interface was selected, or the interface has not been marked<br>
                            as being configurable in the interfaces file "]]..network_conf.interface_file..[["<br><br>
                            <a href="network_interfaces.lua">View network interfaces</a> | <a href="network_status.lua">View network status</a>
                        </center>
                    </div>
]]
    html = header..content..footer
    print(html)
    return        
end    


local enable_checked = ""
if interface.enabled == "enabled" then enable_checked = "checked" end


content = [[
                    <div id="info">
                        <div>
                            <center><h1>]]..interface.name..[[ PPP - Profile Configuration</h1></center>
                        </div>
                    </div>
                    
                    <form action="network_ppp.lua" method="POST">
                        <div class="formLayoutfull">
                            <h3>Profile settings: </h3>
                            <label for="enable">Enable this profile:</label>
                            <input type="checkbox" value="enabled" name="profile" id="enable" ]]..enable_checked..inputControl..[[/><br>
                            <hr>
                            <h3>Modem settings: </h3>
                            <label style="display: inline-block; width: 100px;">PIN: </label>
                            <input type="text" name="pin" id="pin" value="]]..interface.pin..[["]]..inputControl..[[/><br>
                            <label style="display: inline-block; width: 100px;">APN: </label>
                            <input type="text" name="apn" id="apn" value="]]..interface.apn..[["]]..inputControl..[[/><br><br>
                            <hr>
                            <h3>Dial-up authentication:</h3>
                            <label style="display: inline-block; width: 100px;">Username: </label>
                            <input type="text" name="username" id="username" value="]]..interface.user..[["]]..inputControl..[[/><br>
                            <label style="display: inline-block; width: 100px;">Password: </label>
                            <input type="text" name="password" id="password" value="]]..interface.password..[["]]..inputControl..[[/><br><br>
                            <hr><br>
]]
content = content .. [[
                            <input type="hidden" name="interface" value="]]..interface.name..[["/>
                            <hr>
]]

if (saved == 1) then
        content = content .. [[
                            <b><img src="images/ok.png"/> Changes saved.</b><br><br>
]]
end
    content = content .. [[
                            <input type="Reset" name="resetButton" value="Undo"]]..buttonControl..[[/>
                            <input type="Submit" name="submitButton" value="Save and Apply"]]..buttonControl..[[/>
                        </form>
                        </div>
                    </form>
]]                    
    content = content .. [[
                    <script>
                        function toggleVis(target)
                        {
                            if (target.style.display == "none")
                            {
                                target.style.display = "inline-block";
                            }
                            else
                            {
                                target.style.display = "none";
                            }
                            return false;
                        }
                    </script>
]]
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('conf'));">View ]]..network_conf.netctl_profile_path.."/"..intfName..[[</a></a><br>
        
                    <div id="conf" style="display: none;">
                        <pre class="formLayoutfull">
]]..currIntf..[[
                        </pre>
                    </div>
]]            

print(header..content..footer)

