#!/bin/sh
#/**
# * \file  supervisor
# * \brief supervisor  file 
# * \details \n
# * This file is for or continuous monitoring of server and dependency application whether it is alive or not, if not alive invokes application
# * \note \n
# * KCCM\n
# * COPYRIGHT (C) ALL RIGHTS RESERVED.\n
# * \n
# * The reproduction, transmission or use of this document or its contents\n
# * is not permitted without express written authority. Offenders will be\n
# * liable for damages. All rights, including rights created by patent\n
# * grant or registration of a utility model or design, are reserved.\n
# * \n
# * \version n/a
# *
# */

#last=$(busybox date -D '%s' +"%d-%m-%Y" -d "$(( `busybox date +%s`-(7*86400)))")
#iotfile=$last'-aws-iot.txt'
#cd /home/log/
#if [[ -f $iotfile ]]; then
#	rm -rf $iotfile
#fi

find /home/log/ -type f -mtime +7 -exec rm {} \;

while true
do

now="$(date +'%d-%m-%Y')"
start="000000"
end="040000"
curtime=$(date +"%H%M%S")

if [[ $curtime -le $end ]] && [[ $curtime -ge $start ]]; then
	now=$(busybox date -D '%s' +"%d-%m-%Y" -d "$(( `busybox date +%s`-(1*86400)))")
fi

pidof  aws-iot >/dev/null
if [[ $? -ne 0 ]] ; then
cd /kccm/aws/
chmod +x aws-iot
filename="/home/log/$now-aws-iot.txt"
#restarting the aws application if not running
./aws-iot>> "$filename" &
fi

pidof  netlog >/dev/null
if [[ $? -ne 0 ]] ; then
cd /kccm/netlog/
chmod +x netlog
#restarting the network_log application if not running
./netlog  &
fi
sleep 1;
done;
