#!/bin/bash

# Look at voxl-modem.conf to determine MODEM and APN
CONF_FILE=$(cat /etc/modalai/voxl-modem.conf)
PLATFORM=$(uname -n)

parse_json() {
    echo "$CONF_FILE" | python3 -c 'import json,sys; obj=json.load(sys.stdin); print(obj["'"$1"'"])'
}


MODEM=$(parse_json modem_type)
APN=$(parse_json apn)
WORLD=$(parse_json world)
DOODLE_IP=$(parse_json doodle_ip)
DTC_IP=$(parse_json dtc_ip)

# Try to find the interface that uses the smsc95xx driver
DOODLE_INTERFACE=$(for iface in $(ls /sys/class/net); do
    driver=$(ethtool -i "$iface" 2>/dev/null | awk '/driver:/ {print $2}')
    if [ "$driver" = "smsc95xx" ]; then
        echo "$iface"
        break
    fi
done)

# Fallback if not found
if [ -z "$DOODLE_INTERFACE" ]; then
    DOODLE_INTERFACE=$(parse_json doodle_interface)
fi

if [ $PLATFORM == "qrb5165-rb5" ] || [ $PLATFORM == "m0104" ] || [ $PLATFORM == "m0054" ] || [ $PLATFORM == "m0052" ]
then
    echo -e "\nqrb5165 based hardware detected..."
    
    if [ $MODEM == "v2" ]
    then

        INTERFACE="wwan0"
        device="Sierra"

        # Check if the interface is up
        while ! ip link show $INTERFACE up >/dev/null 2>&1; do
            echo "Waiting for $INTERFACE to come up..."
            sleep 1
        done

        echo "$INTERFACE is up!"

        # Check if the device is detected in lsusb
        while ! lsusb | grep -q "$device"; do
            echo "Waiting for $device to be detected..."
            sleep 1
        done

        echo "$device detected!"
        sleep 1

        echo -e "\nSetting raw ip..."
        sudo ifconfig wwan0 down
	    echo 'Y' | sudo tee /sys/class/net/wwan0/qmi/raw_ip
	    sudo ifconfig wwan0 up

        sudo qmicli -d /dev/cdc-wdm0 --set-expected-data-format=raw-ip
        sudo ifconfig wwan0 up 

        echo -e "\nStarting qmi network..."
        while true; do
            sudo qmi-network /dev/cdc-wdm0 start
            return_code=$?

            if [ $return_code -eq 0 ]; then
                echo "Command executed successfully."
                break
            else
                echo "Error detected! Retrying..."
                sleep 1
            fi
        done

        echo -e "\Checking qmi network status..."
        sudo qmi-network /dev/cdc-wdm0 status 

        echo -e "\Starting udhcpc..."
        sudo udhcpc -q -f -i wwan0

        echo -e "\nFinished connect to network."
        sudo ip route flush default
        sudo ip route del default dev eth0 2>/dev/null || true
        sudo ip route del default dev usb0 2>/dev/null || true
        sudo ip route replace default dev "$INTERFACE" scope link metric 0
        echo "Default route set to $INTERFACE"
        exit 0
    elif [ $MODEM == "quectel" ]
    then
        # Setting mode preference
        echo -e "\Setting mode preference to LTE / 5G"
        echo -ne "AT+QNWPREFCFG=\"mode_pref\",LTE:NR5G\r" | busybox microcom -t 1000 /dev/ttyUSB2

        # Loop forever trying to get the cellular link up.
        echo -e "\nInitalizing cellular connection..."
        loop=1
        while [ $loop -ne 0 ]; do
            quectel-CM -s $APN
        done
    elif [ $MODEM == "doodle" ]
    then
        echo -e "\nWaiting for $DOODLE_INTERFACE..."
        rc=1
        while [ $rc -ne 0 ]; do
            ifconfig -s | grep $DOODLE_INTERFACE
            rc=$?
            sleep 1
        done

        echo -e "\n$DOODLE_INTERFACE initialized"

        # loop and re-set IP if network interface goes down and back up
        while true
        do
            # grab ip address from $DOODLE_INTERFACE
            doodle_ip_curr=$(ip -4 addr show $DOODLE_INTERFACE | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)

            # if it's not what we expect, fix it
            if [[ "$doodle_ip_curr" != *"$DOODLE_IP"* ]]; then
                echo "setting IP address to: $DOODLE_IP"
                ifconfig $DOODLE_INTERFACE $DOODLE_IP netmask 255.0.0.0 up
            fi
            sleep 1
        done
    
    elif [ $MODEM == "microhard" ]
    then
        echo -e "\nInitializing Microhard modem..."
        voxl-modem --microhard_configure

    elif [ $MODEM == "dtc" ]
    then
        echo -e "\nInitializing DTC modem..."
        echo -e "\nWaiting for eth0..."
        rc=1
        while [ $rc -ne 0 ]; do
            ifconfig -s | grep eth0
            rc=$?
            sleep 1
        done

        echo -e "\neth0 initialized"

        # loop and re-set IP if network interface goes down and back up
        while true
        do
            # grab ip address from eth0
            eth0_ip_curr=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)

            # if it's not what we expect, fix it
            if [[ "$eth0_ip_curr" != *"$DTC_IP"* ]]; then
                echo "setting IP address to: $DTC_IP"
                ifconfig eth0 $DTC_IP netmask 255.0.0.0 up
            fi
            sleep 1
        done
        
    elif [ $MODEM == "em9191" ]
    then

        INTERFACE="wwan0"
        device="Sierra"

        # Check if the interface is up
        while ! ip link show $INTERFACE up >/dev/null 2>&1; do
            echo "Waiting for $INTERFACE to come up..."
            sleep 1
        done

        echo "$INTERFACE is up!"

        # Check if the device is detected in lsusb
        while ! lsusb | grep -q "$device"; do
            echo "Waiting for $device to be detected..."
            sleep 1
        done

        echo "$device detected!"
        sleep 1

        echo -e "\nStarting qmi network..."
        while true; do
            qmicli --device=/dev/cdc-wdm0 --device-open-proxy --wds-start-network="ip-type=4,apn=$APN" --client-no-release-cid
            return_code=$?

            if [ $return_code -eq 0 ]; then
                echo "Command executed successfully."
                break
            else
                echo "Error detected! Retrying..."
                sleep 1
            fi
        done
        
        echo -e "\Starting udhcpc..."
        sudo udhcpc -q -f -n -i $INTERFACE

        echo -e "\nFinished connect to network."
        exit 0
    else
        echo -e "\nUnknown modem type: "$MODEM
        echo "Recognized modems: v2, microhard, doodle, quectel, microhard, em9191"
        exit 0
    fi

elif [ $PLATFORM == "apq8096" ]
then
    echo -e "\napq8096 detected..."

    if [ $MODEM == "v1" ]
    then
        echo -e "\nInitializing v1 modem..."
        voxl-modem --feather_configure

        echo -e "\nWaiting for ttyACM0..."
        rc=1
        while [ $rc -ne 0 ]; do
            ls /dev | grep ttyACM0
            rc=$?
            sleep 1
        done

        echo -e "\nSetting network preference to LTE"                                     
        uqmi -P -d /dev/qcqmi0 --set-network-preference lte
        echo -e "\nFinished setting network preference" 

        if [ $WORLD != "true" ]
        then
            echo -e "Disabling band 5..."
            configband write /dev/ttyUSB0 0x80a
        fi
    
        # Loop forever trying to get the cellular link up.
        echo -e "\nInitalizing cellular connection..."
        rc=1
        while [ $rc -ne 0 ]; do
            voxl-modem --connect_to_network
            rc=$?
            if [[ $rc -eq 0 ]]; then
                echo -e "\nCellular connection complete."
                exit $rc
            fi
            sleep 1
        done

    elif [ $MODEM == "v2" ]
    then
        echo -e "\nInitializing v2 modem..."
        enable-sierra.sh &> /dev/null

        rc=1
        echo -e "\nWaiting for ttyUSB2..."
        while [ $rc -ne 0 ]; do
            ls /dev | grep ttyUSB2
            rc=$?
            sleep 1
        done

        enable-dns.sh

        # Loop forever trying to get the cellular link up.
        echo -e "\nInitalizing cellular connection..."
        rc=1
        while [ $rc -ne 0 ]; do
            sierra.py
            rc=$?
            if [[ $rc -eq 0 ]]; then
                echo -e "\nCellular connection complete."
                exit $rc
            fi
            sleep 1
        done

    elif [ $MODEM == "microhard" ]
    then
        echo -e "\nInitializing Microhard modem..."
        voxl-modem --microhard_configure
    elif [ $MODEM == "doodle" ]
    then
        echo -e "\nWaiting for eth0..."
        rc=1
        while [ $rc -ne 0 ]; do
            ifconfig -a | grep eth0
            rc=$?
            sleep 1
        done

        echo -e "\neth0 initialized"

        # loop and re-set IP if network interface goes down and back up
        while true
        do
            # grab ip address from eth0
            eth0_ip=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)

            # if it's not what we expect, fix it
            if [[ "$eth0_ip" != *"$DOODLE_IP"* ]]; then
                echo "setting IP address to: $DOODLE_IP"
                ifconfig eth0 $DOODLE_IP netmask 255.0.0.0 up
            fi
            sleep 1
        done

    elif [ $MODEM == "quectel" ]
    then
        echo -e "\nInitializing Quectel modem..."

        echo -e "\nWaiting for ttyUSB2..."
        rc=1
        while [ $rc -ne 0 ]; do
            ls /dev | grep ttyUSB2
            rc=$?
            sleep 1
        done

        enable-dns.sh

        # set mode pref to LTE / 5G
        echo -ne "AT+QNWPREFCFG=\"mode_pref\",LTE:NR5G\r" | busybox microcom -t 1000 /dev/ttyUSB2

        # Loop forever trying to get the cellular link up.
        echo -e "\nInitalizing cellular connection..."
        loop=1
        while [ $loop -ne 0 ]; do
            quectel-CM -s $APN
        done

    else
        echo -e "\nUnknown modem type: "$MODEM
        echo "Recognized modems: v1, v2, microhard, doodle"
        exit 0
    fi
else
    echo -e "[ERROR] Platform not recognized: "$PLATFORM
    exit 5
fi
