#!/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

# Gateway executable name
GW_APP=telem-gw

STATS_DIR='/var/local/telem/stats'

SDD='/mnt/sd'

###############################################################################
#                                                                             #
#                             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                                   #
#                                                                             #
###############################################################################


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
    mountSDp1

    # check if we have new configuration
    /etc/init.d/S11telem-config apply-new

    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 3
done

exit 0
