#!/bin/sh
###############################################################################
#                                                                             #
# Script which downloads a new version of Gateway 6, backs up the old one     #
# and starts the new one                                                      #
#                                                                             #
# This is called during bootup, or when the program closes/crashes.           #
#                                                                             #
# /etc/inittab is responsible for starting/restarting this script             #
#                                                                             #
# Author:	Roland Uuesoo ( roland@martem.ee )                            #
# Date:		08 Sept 2009                                                  #
#                                                                             #
###############################################################################

. /usr/local/bin/telem/functions
. /usr/local/bin/archiving/telem-gw-stderr-checker.sh # throws in STDERR_FILE varaible

###############################################################################
#                                                                             #
#                         Gateway6 files and folders                          #
#                                                                             #
###############################################################################

# Hardware configuration
GW_CONF_HARDWARE=/usr/local/etc/telem/hw-run.xml

GW_APP=telem-gw		# Gateway executable name

FOLDER_TEMP=/tmp		# Temporary files
NET_UPD=/root/upd/


GW_SETUP_FOLDER=/usr/local/etc/telem
SETUP_FILE=$GW_SETUP_FOLDER/gwSetup.bin

STATS_DIR=/var/local/telem/stats

SDD=/mnt/sd
UPDATE_FOLDER=notset
UPD_ROOT=$FOLDER_TEMP/gwupd
UPD_LOG=$FOLDER_TEMP/res.txt
ERR_LOG=$FOLDER_TEMP/err.txt

###############################################################################
#                                                                             #
#                       Gateway6 application constants                        #
#                                                                             #
###############################################################################

# Value returned by Gateway app when requesting shutdown
GW_CONST_REQUEST_SHUTDOWN=13



###############################################################################
#                                                                             #
#                             Script functions                                #
#                                                                             #
###############################################################################

LOG_FOLDER="/var/log/telem/"

# Run gateway program
# $1 - hardware configuration file
# $2 - user configuration file
# $3 - log folder
# $4 - app name
run_gateway()
{

cd /root

    # remove config.xml if file empty
#     test -s /usr/local/etc/telem/config.xml || rm -f /usr/local/etc/telem/config.xml

    test -e /var/local/telem/new_binary_setup && {
        local _new_setup='--new-setup'
        ! rm -f /var/local/telem/new_binary_setup
    }

    prog_cmd="$GW_APP --hw-conf $1 $_new_setup"

    logger -s -p "user.info" -t "$0" "telem-gw cmd: '$prog_cmd'"

    /usr/local/bin/snmp/send_trap.sh "MARTEM-MIB::appStart" "telem-gw: application start"
    
    # Start the program
    eval $prog_cmd ">> ${LOG_FOLDER}/telem-gw-stderr 2>&1"

    # Value returned by $GW_APP
    result=$?
    if [ "$result" != "0" ]; then
        logger -t"telem-gw" -p"user.err" -s "telem-gw Program exited with: ${result}" 2>> "${LOG_FOLDER}/telem-gw-stderr"
    fi  

    return 0
}



###############################################################################
#                                                                             #
#                                The Script                                   #
#                                                                             #
###############################################################################



#
# $1 - log file location
#
updlog()
{
    echo -e $1 >> $UPD_LOG
}



##
# $1 - message
#
errlog()
{
    echo -e "$1" >> $ERR_LOG
}



APP_START_FILE=/var/local/telem/stats/app-start-time

if [ ! -e "$APP_START_FILE" ]; then
    touch "$APP_START_FILE"
fi

! rm $STATS_DIR/no-restart-telemgw

export TZ=$(cat /etc/TZ)

check_telem_gw_stderr_file &

while true; do
    # mount SD card if connected
    if [ -e /dev/mmcblk0p1 ]; then
        mount /dev/mmcblk0p1 /mnt/sd
    fi

    /etc/init.d/S11telem-config start

    test -f /etc/init.d/S12telem-config-bin && /etc/init.d/S12telem-config-bin start

    mv "$APP_START_FILE" "$APP_START_FILE.bak"
    tail -19 "$APP_START_FILE.bak" > "$APP_START_FILE"
    date >> "$APP_START_FILE"
    
    test -e $STATS_DIR/reboot-before-setup && exit 0

    run_gateway $GW_CONF_HARDWARE
    
    test -e $STATS_DIR/no-restart-telemgw && exit 0
    
    sleep 1
done
exit 0

