#!/bin/bash
#

DELAY=15
DIR=/home/zonetest/ZT
NAME=ZT
START=./ZT
LOGFILE=./LOG/CrashReport.txt
ESCAPEFILE=./Escape.ZT
OUTPUTCONSOLE=/dev/tty1

cd $DIR

echo "Starting ZT for the first time" > $OUTPUTCONSOLE
#echo "Checking for escape file" > $OUTPUTCONSOLE

if [ -e "$ESCAPEFILE" ]; then
    #echo "Deleting escape file" > $OUTPUTCONSOLE
    rm -f $ESCAPEFILE
fi

$START > $OUTPUTCONSOLE 2>&1 &

sleep 30

while true ; do

if [ -e "$ESCAPEFILE" ]; then
    echo "Escaping script" > $OUTPUTCONSOLE
    exit
fi

case "$(pidof $NAME | wc -w)" in

0)  echo "ZT crashed $(date)" >> $LOGFILE
    echo "ZT crashed $(date).  Restarting" > $OUTPUTCONSOLE
    $START > /dev/tty1 2>&1 &
    ;;
1)  # all ok
#    echo "Running"
    ;;
*) # echo "double"
 #   kill $(pidof amadeus.x86 | awk '{print $1}')
    ;;
esac

    sleep $DELAY
done

exit
