#!/bin/sh
###
#       Enable GPIO for GWM VMX53
##

. /usr/local/bin/telem/functions
. /usr/local/bin/telem/modem_functions

GPIO=/usr/local/bin/telem/gpio.sh

GPIOPATH='/usr/local/etc/telem/hwdata/gpio/UNK.0'
REVPATH='/usr/local/etc/telem/hwdata/rev/UNK.0'

ethboard() {
    # set revision variable if not set (mostly for stop option)
    : ${revision:=$(cat '/tmp/telem/board/rev')}
    # test conditions
    test $revision -ne 8 && test -e '/var/local/telem/ordercode/eth_board' && \
    (test -e '/etc/init.d/network_eth1' || test -e '/etc/init.d/network_eth2' || test -e '/etc/init.d/network_eth3')
}

ioboard() {
    test -e '/var/local/telem/ordercode/io_board'
}

updateVars() {
    revName="${boardname}.${revision}"
    GPIOPATH="/usr/local/etc/telem/hwdata/gpio/$revName"
    REVPATH="/usr/local/etc/telem/hwdata/rev/$revName"
}

linkf() {
    # $1: source path
    # $2: target path
    rm "$2" &> /dev/null
    ln -sf "$1" "$2"
}

detectHW() {
    boardname="$(/usr/local/bin/telem/boardname.sh)"
    revision=4
    updateVars

    linkf "$GPIOPATH" '/usr/local/etc/telem/gpio'
    linkf "$REVPATH"  '/usr/local/etc/telem/hw'

    # use rev1 io list to detect initial revision
    #$GPIO -e REV_BIT0_OLD
    #$GPIO -e REV_BIT1_OLD
    $GPIO -e REV_BIT0
    $GPIO -e REV_BIT1
    $GPIO -e REV_BIT2

    rev_bit0=$($GPIO -v REV_BIT0)
    rev_bit1=$($GPIO -v REV_BIT1)
    rev_bit2=$($GPIO -v REV_BIT2)

    [ $rev_bit0 = 1 ] && [ $rev_bit1 = 0 ] && [ $rev_bit2 = 0 ] && revision=4
    # revision 9 has same bits as revision 8, so effectively revision 8 is not supported (2 prototypes only)
    [ $rev_bit0 = 0 ] && [ $rev_bit1 = 0 ] && [ $rev_bit2 = 1 ] && revision=9 # change to 8 if you need to use rev8

    updateVars
    linkf "$GPIOPATH" '/usr/local/etc/telem/gpio'
    linkf "$REVPATH"  '/usr/local/etc/telem/hw'

    linkf "$REVPATH/hw.xml"        '/usr/local/etc/telem/hw-run.xml'

    mkdir -p '/tmp/telem/board'
    echo "$boardname" > '/tmp/telem/board/board'
    echo "$revision"  > '/tmp/telem/board/rev'
}

start() {
    logg "Initializing GPIOs"

    # remove unused
    rm -r "/usr/local/etc/telem/gpio.2"        2> /dev/null
    rm -r "/usr/local/etc/telem/"TX6[UD]L.[49] 2> /dev/null
    rm -r "/usr/local/etc/telem/"VMX25.[2489]  2> /dev/null
    rm -r "/usr/local/etc/telem/"VMX53.[1489]  2> /dev/null

    detectHW

    $GPIO -e RUN_LED
    $GPIO -e ALERT_LED

    if [ $revision -gt 3 ]; then
        $GPIO -e EN_USB_PWR
        $GPIO -e REV_BAT

        rev_bat=$($GPIO -v REV_BAT)
        if [ $rev_bat = "1" ]; then
            touch '/var/local/telem/rtc-charger-disabled'
        else
            rm    '/var/local/telem/rtc-charger-disabled'
        fi
    fi

    $GPIO -e IO_BOARD_EXEC
    $GPIO -e IO_BOARD_PWR

    modemInstalled && modemConfigured && {
        # RST_MODEM:
        # rev4  : modem HW SHUTDOWN pin ?
        # rev8+ : reset
        $GPIO -e RST_MODEM
        if [ $revision -ge 8 ]; then
            $GPIO -e MODEM_SIM1
            $GPIO -e MODEM_SIM2
        else
            $GPIO -e MODEM_USB_PWR
            $GPIO -e MODEM_PWR_MON
        fi
        # MODEM_PWR:
        # rev4  : modem ON/OFF pin
        # rev8  : no function
        # rev9+ : power switch
        $GPIO -e MODEM_PWR
        $GPIO -e MODEM_SIM_SELECT
    }

    $GPIO -e IO_BOARD_EXEC
    if [ "${revision}" -gt 8 ]; then
        # io board and lan share same power GPIO
        # always turn it on
        $GPIO -es PERIF_PWR
    else
        ioboard  && $GPIO -es IO_BOARD_PWR  || $GPIO -ec IO_BOARD_PWR
        ethboard && $GPIO -es LAN_BOARD_PWR || $GPIO -ec LAN_BOARD_PWR
    fi

    if [ $revision -gt 3 ]; then
        ioboard && $GPIO -s IO_BOARD_EXEC
    fi
}


stop() {
    $GPIO -c RUN_LED
    $GPIO -s ALERT_LED

    # IO-board exec relay must be disabled before removing USB power
    $GPIO -c IO_BOARD_EXEC
    $GPIO -c EN_USB_PWR

    modemInstalled && modemConfigured && modem_turnOFF
    ethboard && $GPIO -c LAN_BOARD_PWR
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|reload)
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1

esac

exit $?
