-----LUA-----
DESIGNATOR="icom_data_suite"
LICENCE=""

math.randomseed(os.time())
math.random(255)
math.random(255)
math.random(255)

function ip_to_int(ip_address_str)
    local octet4, octet3, octet2, octet1 = ip_address_str:match('(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)');
    return (octet4 << 24) + (octet3 << 16) + (octet2 << 8) + octet1;
end

function int_to_ip(ip_address_int)
    local octet4 = (ip_address_int & 0xff000000) >> 24
    local octet3 = (ip_address_int & 0xff0000) >> 16
    local octet2 = (ip_address_int & 0xff00) >> 8
    local octet1 =  ip_address_int & 0xff
    return (octet4 .. "." .. octet3 .. "." .. octet2 .. "." .. octet1);
end

function container_exists ()
    for i = 1, tonumber(cli("administration.container.container.size")) do
		cur_des = cli("administration.container.container[" .. i .. "].designator")
        if cur_des == DESIGNATOR then
            return i
        end
	end
    return 0
end

function licence_exists ()
    licences = cli("administration.licence.list")
    return 0
end

function net_is_active_local_net(net)
    return ("local" == cli("interfaces." .. net .. ".mode")) and ("1" == cli("interfaces." .. net .. ".active"))
end

function find_first_local_net()
    for i = 1, 5 do
        if net_is_active_local_net("net" .. i) then
            if tonumber(cli("interfaces.net" .. i .. ".ip_address.size")) > 0 then
                return "net" .. i
            end
        end
    end
end

function generate_mac()
    local mac = {}
    for i=1,6 do
        mac[#mac + 1] = (("%2x"):format(math.random(255))):gsub(' ', '0');
        end
    return table.concat(mac,':')
end

function get_valid_mac()
    while true do
        new_mac = generate_mac()
        cli_log("testing mac " .. new_mac)
        if (tonumber(new_mac:sub(2,2), 16) % 2 == 0) then
            cli_log("mac ok")
            return new_mac
        end
    end
end

if string.len(LICENCE) > 0 then
    licences = cli("administration.licences.list")
    if not string.match(licences,LICENCE) then
        cli_log("Licence '" .. LICENCE .. "' is not available -> aborting")
        cli("exit")
    end
end

container_index = container_exists()

-- container does not exist yet -> create and configure it
if container_index == 0 then
-- get configuration of device
    container_net = find_first_local_net()
    container_netmask = cli("interfaces." .. container_net .. ".ip_address[1].netmask")
    net_ip = cli("interfaces." .. container_net .. ".ip_address[1].ip_address")
    domainname = cli("administration.hostnames.domainname")

-- generate ip and mac address
    container_ip = int_to_ip(ip_to_int(net_ip) + 9)
    container_mac = get_valid_mac()

-- general container settings
    cli("administration.container.sms_forward=1")
    cli("administration.container.input_forward=1")

-- add and configure this container
    cli("administration.container.container.add")
    cli("administration.container.container[last].active=1")
    cli("administration.container.container[last].cli_group=readwrite")
    cli("administration.container.container[last].designator=" .. DESIGNATOR)
    cli("administration.container.container[last].ip_net=" .. container_net)
    cli("administration.container.container[last].ipv4_address=" .. container_ip)
    cli("administration.container.container[last].netmask=" .. container_netmask)
    cli("administration.container.container[last].mac=" .. container_mac)
    if string.len(LICENCE) > 0 then
        cli("administration.container.container[last].licence=" .. LICENCE)
    end

-- add and configure forwarding rule
    cli("netfilter.ip_filter.rule.add")
    cli("netfilter.ip_filter.rule[last].rule_active=1")
    cli("netfilter.ip_filter.rule[last].rule_description=")
    cli("netfilter.ip_filter.rule[last].rule_direction=forward")
    cli("netfilter.ip_filter.rule[last].rule_protocol=all")
    cli("netfilter.ip_filter.rule[last].rule_input_if=" .. container_net)
    cli("netfilter.ip_filter.rule[last].rule_output_if=" .. container_net)

-- add and set hostname
    cli("administration.hostnames.hostnames.add")
    cli("administration.hostnames.hostnames[last].host_active=1")
    cli("administration.hostnames.hostnames[last].host_description=")
    cli("administration.hostnames.hostnames[last].host_ip_address=" .. container_ip)
    cli("administration.hostnames.hostnames[last].host_hostname=ids")

-- add and set hostname including the domainname
    cli("administration.hostnames.hostnames.add")
    cli("administration.hostnames.hostnames[last].host_active=1")
    cli("administration.hostnames.hostnames[last].host_description=")
    cli("administration.hostnames.hostnames[last].host_ip_address=" .. container_ip)
    cli("administration.hostnames.hostnames[last].host_hostname=ids." .. domainname)

    cli_log("Created new container '" .. DESIGNATOR .. " (" .. container_net .. ": IP " .. container_ip .. ", MAC " .. container_mac .. ")")

-- activate profile
    cli("administration.profiles.activate")
else
-- container already exists
    if string.len(LICENCE) > 0 then
    -- update the licence
        cli("administration.container.container[" .. container_index .. "].licence=" .. LICENCE)
        cli_log("Updated licence of container '" .. DESIGNATOR .. "' to '" .. LICENCE .. "'")
        cli("administration.profiles.activate")
    else
    -- nothing to do
        cli_log("Container '" .. DESIGNATOR .. "' alread exists in profile -> Config is left unmodified")
    end
end
-----LUA-----

