#!/bin/sh

DetectionModel() 
{

    model_output=$(getModelName 2>/dev/null | tr -d '\n')
    if [[ $model_output =~ UA-[0-9]{4} ]]; then
        echo "${BASH_REMATCH[0]}"
        return
    fi

    eth_devices=$(ls /sys/class/net 2>/dev/null)
    if echo "$eth_devices" | grep -q "eth1"; then
        echo "UA-2241"
    else
        echo "UA-5231"
    fi
}

FirstSetHostName()
{
	while [ true ]
	do
		HWaddr=`ifconfig | grep "eth0"`
		if [ "$HWaddr" != "" ]; then
			break;
		fi
		sleep 1
	done
	echo "UA-Series-"`ifconfig | grep "eth0" | grep "HWaddr" | awk '{print $5}' | sed 's/://g'` > /etc/hostname
	name=$(cat /etc/hostname)
	hostname $name
        
}

RemoveTmp()
{
	rm -rf /Init/tmp/*;
}

GetConnmanGateway() 
{
    local svc="$1"
   
    connmanctl services "$svc" \
    | grep 'IPv4.Configuration' \
    | sed 's/, /\n/g' \
    | grep 'Gateway=' \
    | cut -d= -f2 \
    | tr -d '[]'
}

DetectionNetWork()
{
    ETH0_MAC_NC=$(cat /sys/class/net/eth0/address 2>/dev/null | tr -d ':')
    ETH1_MAC_NC=$(cat /sys/class/net/eth1/address 2>/dev/null | tr -d ':')

    ETH0_SVC="ethernet_${ETH0_MAC_NC}_cable"
    ETH1_SVC="ethernet_${ETH1_MAC_NC}_cable"

    while true; do
        eth0_ip=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') || eth0_ip=""
        eth1_ip=$(ip -4 addr show eth1 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') || eth1_ip=""

        ppp0_ip=""
        if [ -d "/sys/class/net/ppp0" ]; then
            ppp0_ip=$(ip -4 addr show ppp0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') || ppp0_ip=""
        fi

        eth0_carrier=$(cat /sys/class/net/eth0/carrier 2>/dev/null)
        eth1_carrier=$(cat /sys/class/net/eth1/carrier 2>/dev/null)

        gatewayIface="none"
        gatewayIP="none"
        gw0=""
        gw1=""

        if [ "$eth0_carrier" = "1" ]; then
            gw0=$(GetConnmanGateway "$ETH0_SVC")
        fi


        if [ "$eth1_carrier" = "1" ]; then
            gw1=$(GetConnmanGateway "$ETH1_SVC")
        fi

        if [ -n "$gw0" ] && [ "$gw0" != "0.0.0.0" ]; then
            gatewayIface="eth0"
            gatewayIP="$gw0"
        elif [ -n "$gw1" ] && [ "$gw1" != "0.0.0.0" ]; then
            gatewayIface="eth1"
            gatewayIP="$gw1"
        fi

        
        if [ -f "/tmp/defaultGateway" ]; then
            iface=$(awk 'NR==1' /tmp/defaultGateway)
            ip=$(awk 'NR==2' /tmp/defaultGateway)
            route add default gw ${ip} netmask 0.0.0.0 ${iface}
            route add default gw ${ip} netmask 0.0.0.0 ${iface}
            rm -f /tmp/defaultGateway
        fi

        if [ -z "$eth0_ip" ] && [ "$eth0_carrier" -eq 1 ]; then
            if [ "$gatewayIface" = "eth0" ] && [ -n "$eth1_ip" ]; then
                connmanctl disconnect "$ETH1_SVC"
                connmanctl connect "$ETH0_SVC"
                connmanctl connect "$ETH1_SVC"
            else
                connmanctl connect "$ETH0_SVC"
            fi
        fi

        if [ -z "$eth1_ip" ] && [ "$eth1_carrier" -eq 1 ]; then
            if [ "$gatewayIface" = "eth1" ] && [ -n "$eth0_ip" ]; then
                connmanctl disconnect "$ETH0_SVC"
                connmanctl connect "$ETH1_SVC"
                connmanctl connect "$ETH0_SVC"
            else
                connmanctl connect "$ETH1_SVC"
            fi
        fi

        sleep 0.5

        if [ -n "$ppp0_ip" ]; then
            defaults_ifaces=$(ip -o route show | awk '$1=="default"{for(i=1;i<=NF;i++) if($i=="dev"){print $(i+1)}}')
            first_default=$(echo "$defaults_ifaces" | head -n 1)

            if [ "$first_default" != "ppp0" ]; then
                if echo "$defaults_ifaces" | grep -qx 'ppp0'; then
                    ip route del default dev ppp0 2>/dev/null
                fi
                route add default netmask 0.0.0.0 ppp0
                gatewayIface="ppp0"
                gatewayIP="$ppp0_ip"
            fi
        fi

        sleep 3
    done
}



EnsureEth0Connectivity() 
{
    ETH_INTERFACE="eth0"
    ETH0_MAC_NC=$(cat /sys/class/net/$ETH_INTERFACE/address | tr -d ':')

    while true; do

        # 檢查是否存在任何 tap 或 tun 介面
        VPN_INTERFACE=$(ip link show | grep -oP '\d+: (tap|tun)\d+' | awk '{print $2}' | head -n 1)

        if [ -n "$VPN_INTERFACE" ]; then
            eth0_ip=$(ip -4 addr show "$ETH_INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}') || eth0_ip=""
            eth0_carrier=$(cat /sys/class/net/$ETH_INTERFACE/carrier)

            if [ -z "$eth0_ip" ] && [ "$eth0_carrier" -eq 1 ]; then
                connmanctl connect "ethernet_${ETH0_MAC_NC}_cable" 
            fi
        fi

        sleep 5
    done
}


