#!/bin/sh
# Starts G7Farnnavigator application on unit boot
# Handle switching between G7Farmnavigator and G7Navi applications in cycle


if [ -z $APPLICATION_MP ]
then
    export APPLICATION_MP=/media/sd-mmc0p4
    echo "Exporting APPLICATION_MP ..."
fi

export SD_CARD_MP=/media/sd-mmc1p1

# output statistics on how well the batching goes, how many batches,
# which batches are retained and which are opaque and not
#export QSG_RENDERER_DEBUG=render

# output a number of useful rendering timing parameters
#export QSG_RENDER_TIMING=1

export FB_MULTI_BUFFER=2
export QT_QPA_EGLFS_PHYSICAL_WIDTH=154
export QT_QPA_EGLFS_PHYSICAL_HEIGHT=90

# System paths
G7FARM_PATH=$PATH
G7NAVI_PATH=$PATH:"${APPLICATION_MP}/Loquendo"

# Setup network
NetSetup() {
    # -b, --background:  Background immediately.  This is useful for startup scripts which
    #                    don't disable link messages for carrier status.
    # -t, --timeout seconds: Timeout after seconds, instead of the default 30.
    #                        A setting of 0 seconds causes dhcpcd to wait forever to get a lease.
    #                        If dhcpcd is working on a single interface then dhcpcd will exit when a
    #                        timeout occurs, otherwise dhcpcd will fork into the background.
    # -A, --noarp:    Don't request or claim the address by ARP.  This also disables IPv4LL.
    # -L, --noipv4ll: Don't use IPv4LL (aka APIPA, aka Bonjour, aka ZeroConf).
    dhcpcd -t 0 -b -A -L eth0
#    dhcpcd -t 0 -b eth1

    # Set up CAN bus for ISOBUS functionality
    ip link set can0 txqueuelen 40 up type can bitrate 250000 restart-ms 5000
}

ConfigureG7Farm() {

    if [ -z $APPLICATION_DATA_MP ]
    then
        export APPLICATION_DATA_MP=/media/sd-mmc0p4
        echo "Exporting APPLICATION_DATA_MP for G7Farmnavigator app ..."
    fi
    
    ## Setup network
    NetSetup

    SWDIR="${APPLICATION_MP}/GS7"
    CLIENT_APPNAME="Geosat7"

    export PATH=$G7FARM_PATH
    export LD_LIBRARY_PATH=$SWDIR
    
    # Scene Graph and Rendering
    # There are three render loop variants available: basic, windows, and threaded.
    # Out of these, 'basic' and 'windows' are single-threaded,
    # while 'threaded' performs scene graph rendering on a dedicated thread. 
    export QSG_RENDER_LOOP=basic
    export QSG_INFO=1

    # Change default name of the bluetooth device
    #hciconfig hci0 name 'G7 Farmnavigator'
    (echo system-alias G7 Farmnavigator; echo quit)|bluetoothctl

    # Change default name of the WiFi device
    hostname G7Farmnavigator
    
    echo "APPLICATION_MP: ${APPLICATION_MP}"
    echo "APPLICATION_DATA_MP: ${APPLICATION_DATA_MP}"
    echo "SWDIR: ${SWDIR}"
    echo "LD_LIBRARY_PATH: ${LD_LIBRARY_PATH}"
    echo "QT_PLUGIN_PATH: ${QT_PLUGIN_PATH}"
    echo "PATH: ${PATH}"
    
    cd $SWDIR
}


ConfigureG7Navi() {

    export APPLICATION_DATA_MP=/media/sd-mmc0p4/G7Navi
    echo "Exporting APPLICATION_DATA_MP for G7Navi app ..."

    SWDIR="${APPLICATION_MP}/G7Navi"
    CLIENT_APPNAME="G7Navi"

    export PATH=$G7NAVI_PATH
    export LD_LIBRARY_PATH=$SWDIR:"${APPLICATION_MP}/Loquendo/bin"

    export QSG_RENDER_LOOP=threaded
    export QSG_INFO=1
    
    echo "APPLICATION_MP: ${APPLICATION_MP}"
    echo "APPLICATION_DATA_MP: ${APPLICATION_DATA_MP}"
    echo "SWDIR: ${SWDIR}"
    echo "LD_LIBRARY_PATH: ${LD_LIBRARY_PATH}"
    echo "QT_PLUGIN_PATH: ${QT_PLUGIN_PATH}"
    echo "PATH: ${PATH}"
    
    cd $SWDIR    
}


rc=177

while [ $rc == 177 ]; do

    echo
    echo "##############################################"
    echo "#    Starting G7Farmnavigator Application    #"
    echo "##############################################"
    echo
    
    ConfigureG7Farm
    
    ./$CLIENT_APPNAME -plugin TuioTouch:udp=3333
    
    rc=$?
    echo "G7Farmnavigator application exited with return code: $rc"

    if [ $rc == 177 ]; then
        echo
        echo "##############################################"
        echo "#         Starting G7Navi Application        #"
        echo "##############################################"
        echo

        # G7Navi has its own path for user data
        unset APPLICATION_DATA_MP

        ConfigureG7Navi
        
        ./$CLIENT_APPNAME
        
        rc=$?
        echo "G7Navi application exited with return code: $rc"
        
        if [ $rc == 177 ]; then
            # G7Farmnavigator has its own path for user data
            unset APPLICATION_DATA_MP
        fi
    fi
done


if [ $rc == 0 ] || [ $rc == 143 ]
then
    ## poweroff in case of normal exit ( 0 exit code ) or SIGTERM ( 143 exit code )
    echo "Application normally terminated. Powering off..."
    poweroff
else
    if [ $rc == 178 ]
    then
        echo
        echo "##############################################"
        echo "#         Starting LCD calibration           #"
        echo "##############################################"
        echo

        rm -f ${PERSISTENT_MP}/hw_touch_calib
        sync

        /etc/rc.d/init.d/hw_touch_calib start
        sync
        sleep 1
        echo "Calibration completed. Rebooting..."
        reboot
    else
        ## Reboot in case of crash,
        echo "Application crashed. Rebooting..."
        reboot
    fi
fi
