#!/usr/bin/luaweb
json = require("json")
omg = require("omg")

os.execute("echo \"" .. arg[0] .. " start: $(date) " .. (arg[1] or "") .. " " .. (arg[2] or "") .. " " .. (arg[3] or "") .. " " .. (arg[4] or "") .. " " .. (arg[5] or "") .. " " .. (arg[6] or "") .. "\" >> /tmp/sequence")

if arg[1]=="lo" then
    os.execute("echo \"" .. arg[0] .. " stop: $(date)\" >> /tmp/sequence")
    return 0
end

function getEnvironment()
    local val
    val=omg.cmd2TreeTable("omgenvironment --dump", ".", "=")
    return val
end

local ip=omg.ipConfig()
local cmdline=omg.getCmdline()
local resolvConf=omg.getResolvConf()
local environment=getEnvironment()
 
--remove all IP* values
for k,v in pairs(cmdline) do
   if string.find(k,"^IP") then
      cmdline[k]=nil
   end
end

--remove all IPV6 except scope global
for k,v in pairs(ip) do
   if ip[k]["inet6"] then
      for k2,v2 in pairs(ip[k]["inet6"]) do
	 if ip[k]["inet6"][k2]["scope"]~="global" then
	    ip[k]["inet6"][k2]=nil
	 end
      end
   end
end

envPrefix="IP_"
local dnsCount=1
for k,v in ipairs(resolvConf) do
    if resolvConf[k].domain~=nil then
       cmdline[envPrefix .. "DOMAIN"]=resolvConf[k].domain
    elseif resolvConf[k].search~=nil then
       cmdline[envPrefix .. "SEARCH"]=resolvConf[k].search
       cmdline[envPrefix .. "DOMAIN"]=resolvConf[k].search
    elseif resolvConf[k].nameserver~=nil then
       cmdline[envPrefix .. "DNS" .. dnsCount]=resolvConf[k].nameserver
       dnsCount=dnsCount+1
    end
end

for k,v in pairs(ip) do
   if k == "bond0" then
      envPrefix="IP_"
   elseif k == "eth0" then
      envPrefix="IP_"
   elseif k == "eth1" then
      envPrefix="IP2_"
   elseif k == "eth2" then
      envPrefix="IP3_"
   elseif k == "eth3" then
      envPrefix="IP4_"
   elseif k == "eth4" then
      envPrefix="IP5_"
   elseif k == "eth5" then
      envPrefix="IP6_"
   elseif k == "eth6" then
      envPrefix="IP7_"
   elseif k == "eth7" then
      envPrefix="IP8_"
   else
      envPrefix=nil
   end

   if envPrefix ~= nil then
      if cmdline[envPrefix .. "DHCPMODE"] == nil then
         if environment[envPrefix .. "DHCPMODE"] == nil then
            cmdline[envPrefix .. "DHCPMODE"] = "CLIENT"
	 else
            cmdline[envPrefix .. "DHCPMODE"] = environment[envPrefix .. "DHCPMODE"]
         end
      end   

      if cmdline[envPrefix .. "DHCPMODE_6"] == nil then
         if environment[envPrefix .. "DHCPMODE_6"] == nil then
            cmdline[envPrefix .. "DHCPMODE_6"] = "AUTO"
	 else
            cmdline[envPrefix .. "DHCPMODE_6"] = environment[envPrefix .. "DHCPMODE_6"]
         end
      end   
      
      if ip[k]["inet"] and ip[k]["inet"][1] then
	 cmdline[envPrefix .. "INTERFACE"]=k
	 cmdline[envPrefix .. "ADDRESS"]=ip[k]["inet"][1].address
	 cmdline[envPrefix .. "MASK"]=ip[k]["inet"][1].mask
      end
      
      if ip[k]["inet6"] and ip[k]["inet6"][1] then
	 cmdline[envPrefix .. "INTERFACE_6"]=k
	 cmdline[envPrefix .. "ADDRESS_6"]=ip[k]["inet6"][1].address
	 cmdline[envPrefix .. "MASK_6"]=ip[k]["inet6"][1].masklength
      end

      local defaultGw=omg.execute("/sbin/route -n | grep -e '0\.0\.0\.0.*UG.*" .. k .. "' | awk '{print $2}'")
      if defaultGw ~= nil and defaultGw ~= "" then
      	 cmdline["IP_GW"]=defaultGw
      	 cmdline["GWInterface"]=k
      end
   end
end 

if environment["HTTP_PROXY"] ~= nil then
   --for os
   cmdline["http_proxy"] = environment["HTTP_PROXY"]
   --for gate config
   cmdline["HTTP_PROXY"] = environment["HTTP_PROXY"]
end

gwInterface=cmdline["GWInterface"] or "eth0"
if ip[gwInterface]~=nil and ip[gwInterface]["inet"]~=nil and ip[gwInterface]["inet"][1]~=nil and ip[gwInterface]["inet"][1]["address"]~=nil then
   os.execute("echo " .. ip[gwInterface]["inet"][1]["address"] .. " > /tmp/ipaddress")
else
   os.execute("echo '' > /tmp/ipaddress")
end

os.execute("if ! grep -q '^#LOCKED$' /etc/hosts; then echo \"# automatically generated file\" > /etc/hosts;fi")

if ip[gwInterface]~=nil and ip[gwInterface]["inet"]~=nil and ip[gwInterface]["inet"][1]~=nil and ip[gwInterface]["inet"][1]["address"]~=nil then
   os.execute(
       "if ! grep -q '^#LOCKED$' /etc/hosts; then" .. 
           "     echo \"127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4\" >>/etc/hosts;" ..
           "     echo \"" .. ip[gwInterface]["inet"][1]["address"] .. " " .. (cmdline["HOSTNAME"] or "") .. " " .. (cmdline["SYSTEM"] or "") .. " omcard\" >>/etc/hosts;" ..
           " fi")
end

local index=1
if ip[gwInterface]~=nil and ip[gwInterface]["inet6"]~=nil then
    for k,v in ipairs(ip[gwInterface]["inet6"]) do
        if v.address == environment["IP_ADDRESS_6"] then
            index = k
        end
    end
end
if ip[gwInterface]~=nil and ip[gwInterface]["inet6"]~=nil and ip[gwInterface]["inet6"][index]~=nil and ip[gwInterface]["inet6"][index]["address"]~=nil then
   os.execute(
       "if ! grep -q '^#LOCKED$' /etc/hosts; then" .. 
           "     echo \"::1 localhost localhost.localdomain localhost6 localhost6.localdomain6\" >>/etc/hosts;" ..
           "     echo \"" .. ip[gwInterface]["inet6"][index]["address"] .. " " .. (cmdline["HOSTNAME"] or "") .. " " .. (cmdline["SYSTEM"] or "") .. " omcard\" >>/etc/hosts;" ..
           " fi")
   os.execute("echo " .. ip[gwInterface]["inet6"][index]["address"] .. " > /tmp/ipaddress6")
else
   os.execute("echo '' > /tmp/ipaddress6")
end

local f = io.open("/tmp/cmdline", "w")
if f then 
   local s = omg.dumpTableToDelimiterList(cmdline,".","=",nil,nil,"export ")
   f:write(s)
   f:close() 
end

if ip[gwInterface]~=nil and ip[gwInterface]["inet"]~=nil and ip[gwInterface]["inet"][1]~=nil then
   os.execute("echo  " .. gwInterface .. "=" .. (ip[gwInterface]["inet"][1]["address"] or "NIL") .. " >> /tmp/sequence")
end

if ip[gwInterface]~=nil and ip[gwInterface]["inet"]~=nil and ip[gwInterface]["inet"][1]~=nil and ip[gwInterface]["inet"][1]["address"]~="127.0.0.1" then
   os.execute("/sbin/route del 255.255.255.255 " .. gwInterface .. " 2>/dev/null")
   os.execute("/sbin/route add 255.255.255.255 " .. gwInterface)
   os.execute("/sbin/route del default metric 500 ".. gwInterface .. " 2>/dev/null")
   os.execute("/sbin/route add default metric 500 " .. gwInterface)

   os.execute("killall udpconfd 2>/dev/null")
   os.execute("if [ -e /tmp/cmdline ];then . /tmp/cmdline; fi ; if [ -z $sn ];then export sn=$(cat /data/serial.txt); fi; /usr/bin/udpconfd >/dev/null &")
end

os.execute("/usr/bin/omgiptables")

os.execute("echo \"" .. arg[0] .. " stop: $(date)\" >> /tmp/sequence")
