#!/bin/sh

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

count=0
file=/kccm/aws/awsConfig

IFS="="
while read -r line val
do
	if [ $line = "NETLOG_PERIODICITY" ]; then
		time=$val
	fi
done <"$file"

time=$(eval echo ${time})

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
filename="/home/netlog/$now-netlog.csv"
date>>$filename
ifconfig>>$filename

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
if [[ $curtime -eq $end ]]; then
	echo "Netlog Application need to restart"
	exit
fi
count=$((count+1))
if [ $time -eq $count ];then
count=0
filename="/home/netlog/$now-netlog.csv"
date>>$filename
ifconfig>>$filename
fi
sleep 1;
done;

