#!/bin/sh

    # Set hubID
    model=$(cat /proc/device-tree/model)
    modules=$(lsmod | grep -e smsc9500 -e cp210x )
    hubID=$(cat /etc/platform/sys/hubID 2> /dev/null )

    if [ -z "$modules" ]; then
       usb_current_speed_file="/sys/class/udc/ci_hdrc.0/current_speed"
       usb_speed=$(cat "$usb_current_speed_file" | grep speed )
       vbus=$(grep VBUS /root/.BOOT_LOG)

       usb0_present=$( (ip link | grep usb0 | grep -v NO-CARRIER) || (ls /dev/ttyGS0 2> /dev/null) )

       if [ "$usb0_present" == "/dev/ttyGS0" ] && [ -z "$usb_speed" ] && [ "$hubID" != "developer" ] && [ "$hubID" != "11" ]
       then
           usb0_present=""
       fi

       eth0_present=$( ip link | grep eth0 | grep -v NO-CARRIER )

       if [ ! -z "$usb0_present" ] && [ ! -z "$eth0_present" ]; then
           if [ "$hubID" != "developer" ]; then
              rm -rf /etc/platform/sys/hubID
           fi
       fi

       if [ -z "$usb0_present" ] && [ ! -z "$eth0_present" ]; then
           echo 01 > /etc/platform/sys/hubID
       fi

       if [ ! -z "$usb0_present" ] && [ -z "$eth0_present" ]; then
           if [ "$model" == "phoenix" ]; then
               if [ "$hubID" != "11" ] && [ -z "$vbus" ]; then
                   echo 02 > /etc/platform/sys/hubID
               fi
           else
              echo 11 > /etc/platform/sys/hubID
           fi
       fi

       if [ -z "$usb0_present" ] && [ -z "$eth0_present" ]; then
           if [ "$model" == "phoenix" ]; then
              if [ "$hubID" != "11" ] && [ "$hubID" != "03" ]; then
                   rm -rf  /etc/platform/sys/hubID
	      fi
           else
              if [ "$hubID" != "18" ]; then
                  rm -rf  /etc/platform/sys/hubID
              fi
           fi
       fi
   else
        interface="eth0"
        model=$(cat /proc/device-tree/model)
        if [ "$model" == "phoenix" ]; then
           interface="eth1"
        fi
        if [ ! -z "$hubID" ] && [ "$hubID" == 13 ]; then
           tranc=$(ethtool "$interface" | grep Transceiver | awk -F ": " '{ print $2 }' )
           if [ "$tranc" == "internal" ]
           then
              echo 12 > /etc/platform/sys/hubID
           fi
        fi
   fi
