function routes_main()
{
    $MetricCount = 0;
    $unix = new unix();
    LoadProcNetDev();
    $GLOBALS["ifconfig"] = $unix->find_program("ifconfig");
    $GLOBALS["routebin"] = $unix->find_program("route");
    $GLOBALS["echobin"] = $unix->find_program("echo");
    $GLOBALS["ipbin"] = $unix->find_program("ip");
    $GLOBALS["vconfigbin"] = $unix->find_program("vconfig");
    $GLOBALS["moprobebin"] = $unix->find_program("modprobe");
    $sock = new sockets();
    $OVHNetConfig = $sock->GET_INFO("OVHNetConfig");
    if (!is_numeric($OVHNetConfig)) {
        $OVHNetConfig = 0;
    }
    $NetWorkBroadCastAsIpAddr = $sock->GET_INFO("NetWorkBroadCastAsIpAddr");
    $EnableChilli = $sock->GET_INFO("EnableChilli");
    if (!is_numeric($EnableChilli)) {
        $EnableChilli = 0;
    }
    if ($EnableChilli == 1) {
        $ChilliConf = unserialize(base64_decode($sock->GET_INFO("ChilliConf")));
        echo "Starting......: " . date("H:i:s") . " Will skip {$ChilliConf["HS_LANIF"]} for HotSpot config\n";
        $eth_SKIP[$ChilliConf["HS_LANIF"]] = true;
    }
    $route = $unix->find_program("route");
    $ip = $unix->find_program("ip");
    $types[1] = "{network_nic}";
    $types[2] = "{host}";
    $endcmdsline = array();
    $q = new mysql();
    $NetBuilder = new system_nic();
    $NetBuilder->LoadTools();
    $GLOBALS["SCRIPTS_ROUTES"][] = "# [" . __LINE__ . "]";
    $GLOBALS["SCRIPTS_ROUTES"][] = "# [" . __LINE__ . "] *******************************";
    $GLOBALS["SCRIPTS_ROUTES"][] = "# [" . __LINE__ . "] ****     MAIN ROUTES       ****";
    $GLOBALS["SCRIPTS_ROUTES"][] = "# [" . __LINE__ . "] *******************************";
    $GLOBALS["SCRIPTS_ROUTES"][] = "# [" . __LINE__ . "]";
    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["ipbin"]} route add 127.0.0.1 dev lo";
    //$GLOBALS["SCRIPTS_ROUTES"][]="{$GLOBALS["routebin"]} add -net 127.0.0.0 netmask 255.255.255.0 lo";
    $sql = "SELECT * FROM  `nics` WHERE defaultroute=1 ORDER BY Interface";
    $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
    $eth = trim($ligne["Interface"]);
    $metric = $ligne["metric"];
    $SourceBasedRouting = intval($ligne["SourceBasedRouting"]);
    $eth = str_replace("\r\n", "", $eth);
    $eth = str_replace("\r", "", $eth);
    $eth = str_replace("\n", "", $eth);
    if (isset($eth_SKIP[$eth])) {
        $eth = null;
    }
    if ($ligne["GATEWAY"] == null) {
        $eth = null;
    }
    if ($ligne["GATEWAY"] == "0.0.0.0") {
        $eth = null;
    }
    if ($ligne["NETMASK"] == "0.0.0.0") {
        $eth = null;
    }
    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["echobin"]} \"\" > /etc/iproute2/rt_tables";
    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["echobin"]} \"255\tlocal\" >> /etc/iproute2/rt_tables";
    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["echobin"]} \"254\tmain\" >> /etc/iproute2/rt_tables";
    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["echobin"]} \"253\tdefault\" >> /etc/iproute2/rt_tables";
    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["echobin"]} \"0\tunspec\" >> /etc/iproute2/rt_tables";
    if ($eth != null) {
        if (!isset($GLOBALS["DEFAULT_ROUTE_SET"])) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] is set as default route Source Based Routing={$SourceBasedRouting}";
            $GLOBALS["DEFAULT_ROUTE_SET"] = $eth;
            $NETMASK = $ligne["NETMASK"];
            $CDIR = $NetBuilder->GetCDIRNetwork($ligne["IPADDR"], $ligne["NETMASK"]);
            $md5net = md5($CDIR);
            $GLOBALS["MD5NET"][$md5net] = true;
            $metric = $ligne["metric"];
            $metric_text = null;
            if ($metric > 0) {
                $metric_text = " metric {$metric}";
            }
            $MetricCount++;
            $GLOBALS["SCRIPTS_ROUTES"][] = "#[{$eth}/" . __FUNCTION__ . "/" . __LINE__ . " IP:{$ligne["IPADDR"]}/{$CDIR} gateway:{$ligne["GATEWAY"]} Source Based Routing={$SourceBasedRouting}";
            //$GLOBALS["SCRIPTS_ROUTES"][]="{$GLOBALS["ifconfig"]} ".$NetBuilder->NicToOther($eth)." down";
            //$GLOBALS["SCRIPTS_ROUTES"][]="{$GLOBALS["ifconfig"]} ".$NetBuilder->NicToOther($eth)." up";
            if ($SourceBasedRouting == 0) {
                if (isGatewayGood($ligne["GATEWAY"])) {
                    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -host {$ligne["GATEWAY"]} dev " . $NetBuilder->NicToOther($eth);
                    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -net 0.0.0.0 gw {$ligne["GATEWAY"]} dev " . $NetBuilder->NicToOther($eth) . " metric 1";
                } else {
                    $GLOBALS["SCRIPTS_ROUTES"][] = "#[{$eth}/" . __FUNCTION__ . "/" . __LINE__ . " {$ligne["GATEWAY"]} is a bad gateway but enabled as default";
                    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add default dev " . $NetBuilder->NicToOther($eth);
                }
            }
            if ($SourceBasedRouting == 1) {
                routes_source_add($NetBuilder->NicToOther($eth), $ligne["IPADDR"], $ligne["GATEWAY"], $CDIR, __LINE__);
            }
        }
    }
    if (!isset($GLOBALS["DEFAULT_ROUTE_SET"])) {
        $GLOBALS["SCRIPTS_ROUTES"][] = "# [eth0] is set as default route metric `{$metric}`.";
        $GLOBALS["SCRIPTS_ROUTES"][] = "#[{$eth}/" . __FUNCTION__ . "/" . __LINE__ . " IP:{$ligne["IPADDR"]}/{$CDIR} gateway:{$ligne["GATEWAY"]} Source Based Routing={$SourceBasedRouting}";
        $nic = new system_nic("eth0");
        if ($nic->GATEWAY != null) {
            $eth = "eth0";
            $GLOBALS["DEFAULT_ROUTE_SET"] = "eth0";
            $CDIR = $NetBuilder->GetCDIRNetwork($nic->IPADDR, $nic->NETMASK);
            $SourceBasedRouting = $nic->SourceBasedRouting;
            $md5net = md5($CDIR);
            $GLOBALS["MD5NET"][$md5net] = true;
            $metric = $nic->metric;
            $metric_text = null;
            if ($metric > 0) {
                if ($MetricCount == 0) {
                    $MetricCount++;
                    $metric = 1;
                }
                $metric_text = " metric {$metric}";
            }
            if ($SourceBasedRouting == 0) {
                if (isGatewayGood($nic->GATEWAY)) {
                    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -host {$nic->GATEWAY} dev " . $NetBuilder->NicToOther($eth);
                    $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -net 0.0.0.0 gw {$nic->GATEWAY} dev " . $NetBuilder->NicToOther($eth) . $metric_text;
                }
            }
            if ($SourceBasedRouting == 1) {
                routes_source_add($NetBuilder->NicToOther($eth), $nic->IPADDR, $nic->GATEWAY, $CDIR, __LINE__);
            }
        }
    }
    $GLOBALS["rt_tables_number"] = 0;
    $sql = "SELECT * FROM `nics` WHERE enabled=1 ORDER BY Interface";
    $q = new mysql();
    $results = $q->QUERY_SQL($sql, "artica_backup");
    if (!$q->ok) {
        echo "Starting......: " . date("H:i:s") . " Mysql error : {$q->mysql_error}\n";
        return;
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $eth = trim($ligne["Interface"]);
        $eth = str_replace("\r\n", "", $eth);
        $eth = str_replace("\r", "", $eth);
        $eth = str_replace("\n", "", $eth);
        $eth = trim($eth);
        if ($eth == null) {
            continue;
        }
        $SourceBasedRouting = intval($ligne["SourceBasedRouting"]);
        $IPADDR = $ligne["IPADDR"];
        $GLOBALS["SCRIPTS_ROUTES"][] = "#";
        $GLOBALS["SCRIPTS_ROUTES"][] = "#";
        if (!isset($GLOBALS["PROC_NET_DEV"][$eth])) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] Not found in PROC_NET_DEV Hardware error";
            continue;
        }
        $ROUTES_ARRAY = unserialize($ligne["routes"]);
        $metric = $ligne["metric"];
        $metric_text = null;
        if ($metric > 0) {
            if ($MetricCount == 0) {
                $MetricCount++;
                $metric = 1;
            }
            $metric_text = " metric {$metric}";
        }
        if (isset($GLOBALS["DEFAULT_ROUTE_SET"])) {
            if ($GLOBALS["DEFAULT_ROUTE_SET"] == $eth) {
                continue;
            }
        }
        $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] SourceBasedRouting:{$SourceBasedRouting} Main route {$eth} gateway {$ligne["GATEWAY"]} netmask {$ligne["NETMASK"]} ipaddr: {$ligne["IPADDR"]}";
        if (isset($eth_SKIP[$eth])) {
            echo "Starting......: " . date("H:i:s") . " {$eth} skipping\n";
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}] skipped";
            continue;
        }
        if ($ligne["GATEWAY"] == null) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] GATEWAY = null skipped";
            continue;
        }
        if ($ligne["GATEWAY"] == "0.0.0.0") {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] GATEWAY = 0.0.0.0 skipped";
            continue;
        }
        if ($ligne["NETMASK"] == "0.0.0.0") {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] NETMASK = 0.0.0.0 skipped";
            continue;
        }
        if (trim($ligne["NETMASK"]) == null) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] NETMASK = null skipped";
            continue;
        }
        $CDIR = trim($NetBuilder->GetCDIRNetwork($ligne["IPADDR"], $ligne["NETMASK"]));
        $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] CDIR:{$CDIR} GATEWAY = {$ligne["GATEWAY"]} add in table (default route {$ligne["defaultroute"]})";
        if ($ligne["defaultroute"] == 0) {
            if (!isset($GLOBALS["GATEWAYADDED"][$eth][$ligne["GATEWAY"]])) {
                if ($SourceBasedRouting == 0) {
                    routes_default_add($NetBuilder->NicToOther($eth), $IPADDR, $ligne["GATEWAY"], $CDIR, $metric_text, __LINE__);
                }
                if ($SourceBasedRouting == 1) {
                    routes_source_add($NetBuilder->NicToOther($eth), $IPADDR, $ligne["GATEWAY"], $CDIR, __LINE__);
                }
                $GLOBALS["GATEWAYADDED"][$eth][$ligne["GATEWAY"]] = true;
            }
        }
        if ($CDIR == null) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# GetCDIRNetwork ({$ligne["IPADDR"]},{$ligne["NETMASK"]} ) return null";
        }
        if (isset($ALREADYNETS[$CDIR])) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}] {$CDIR} already added skip it";
            continue;
        }
        $ALREADYNETS[$CDIR] = true;
        $eth = $NetBuilder->NicToOther($eth);
        $md5net = md5($CDIR);
        if (isset($GLOBALS["MD5NET"][$md5net])) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [" . __LINE__ . "] [{$eth}] MD5NET already added skip it";
            continue;
        }
        if ($ligne["defaultroute"] == 0) {
            if (is_array($ROUTES_ARRAY)) {
                $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] " . count($ROUTES_ARRAY) . " Additionnal route(s)";
                if (count($ROUTES_ARRAY) > 0) {
                    while (list($ip, $ip_array) = each($ROUTES_ARRAY)) {
                        $NETMASK = $ip_array["NETMASK"];
                        $GATEWAY = $ip_array["GATEWAY"];
                        $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] {$ip}/{$NETMASK} -> {$GATEWAY} Table {$GLOBALS["RT_TABLES"][$eth]}/{$eth}";
                        if (isGatewayGood($GATEWAY)) {
                            $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -host {$GATEWAY} dev {$eth}";
                            $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -net {$ip} netmask {$NETMASK} gw {$GATEWAY} dev {$eth}";
                        }
                    }
                }
            }
            if (!isset($GLOBALS["GATEWAYADDED"][$eth][$ligne["GATEWAY"]])) {
                $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] {$CDIR} Gateway:{$ligne["GATEWAY"]} SourceBasedRouting:{$SourceBasedRouting}";
                if ($SourceBasedRouting == 0) {
                    routes_default_add($NetBuilder->NicToOther($eth), $IPADDR, $ligne["GATEWAY"], $CDIR, $metric_text, __LINE__);
                }
                if ($SourceBasedRouting == 1) {
                    routes_source_add($NetBuilder->NicToOther($eth), $IPADDR, $ligne["GATEWAY"], $CDIR, __LINE__);
                }
            }
        } else {
            if (is_array($ROUTES_ARRAY)) {
                if (count($ROUTES_ARRAY) > 0) {
                    while (list($ip, $ip_array) = each($ROUTES_ARRAY)) {
                        $NETMASK = $ip_array["NETMASK"];
                        $GATEWAY = $ip_array["GATEWAY"];
                        $CDIR = $NetBuilder->GetCDIRNetwork($ip, $NETMASK);
                        if (isset($ALREADYNETS[$CDIR])) {
                            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}] {$ip}/{$NETMASK} {$CDIR} already added skip it";
                            continue;
                        }
                        $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$eth}/" . __LINE__ . "] {$ip}/{$NETMASK}/{$CDIR} -> {$GATEWAY} main route";
                        $GLOBALS["SCRIPTS_ROUTES"][] = "{$GLOBALS["routebin"]} add -net {$ip} netmask {$NETMASK} gw {$GATEWAY} dev " . $NetBuilder->NicToOther($eth);
                        $ALREADYNETS[$CDIR] = true;
                    }
                }
            }
        }
        $GLOBALS["MD5NET"][$md5net] = true;
    }
    $sql = "SELECT * FROM nic_routes ORDER BY `zOrder`";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    $types[1] = "{network_nic}";
    $types[2] = "{host}";
    $GLOBALS["SCRIPTS_ROUTES"][] = "#";
    $GLOBALS["SCRIPTS_ROUTES"][] = "# nic_routes " . mysql_num_rows($results) . " elements..";
    $GLOBALS["SCRIPTS_ROUTES"][] = "#";
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $type = $ligne["type"];
        $nicz = new system_nic($ligne["nic"]);
        $ligne["SourceBasedRouting"] = intval($nic->SourceBasedRouting);
        if ($type == 2) {
            $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$ligne["nic"]}/" . __LINE__ . "] Route host {$ligne["pattern"]}";
            $data = routes_main_host($ligne);
            if ($data != null) {
                $GLOBALS["SCRIPTS_ROUTES"][] = $data;
            }
            continue;
        }
        $GLOBALS["SCRIPTS_ROUTES"][] = "# [{$ligne["nic"]}/" . __LINE__ . "] Route network {$ligne["pattern"]} gw {$ligne["gateway"]}";
        $data = routes_main_network($ligne);
        if ($data != null) {
            $GLOBALS["SCRIPTS_ROUTES"][] = $data;
        }
    }
    if (count($endcmdsline) > 0) {
        while (list($index, $line) = each($endcmdsline)) {
            $GLOBALS["SCRIPTS_ROUTES"][] = $line;
        }
    }
}