#!/bin/bash
#
#  Clean-up fwuplog.log at the boot time
#  SLEEP 20 seconds before executing the scripts.
#
#####

# Fixed Value
LOGFILE="/var/log/fwuplog.log"
CLEANUP_LOGFILE="/var/tmp/clean-up-fwuplog-at-boot.log"

#  SLEEP 20 seconds before executing the scripts.
/bin/date > ${CLEANUP_LOGFILE}
/bin/sleep 20
/bin/date >> ${CLEANUP_LOGFILE}

# Leave the latest 10 update logs.
/bin/echo "Clean up /var/log/fwuplog.log" >> ${CLEANUP_LOGFILE}
/bin/echo "=== Before" >> ${CLEANUP_LOGFILE}
/usr/bin/wc ${LOGFILE} >> ${CLEANUP_LOGFILE}
/bin/grep -an "Start iedenchi FW update" ${LOGFILE} >> ${CLEANUP_LOGFILE}
num_of_logs=`/bin/grep -an "Start iedenchi FW update" ${LOGFILE} | /usr/bin/wc -l`
if [ ${num_of_logs} -ge 10 ]; then
	total_lines=`/usr/bin/wc -l ${LOGFILE} | /bin/sed -e "s/ .*$//"`
	latest_lines=`/bin/grep -an "Start iedenchi FW update" ${LOGFILE} | /usr/bin/tail -n 10 | /usr/bin/head -n 1 | /bin/sed -e "s/:.*$//"`
	remaining_lines=$((total_lines-latest_lines+1))
	/usr/bin/tail -n ${remaining_lines} ${LOGFILE} > ${LOGFILE}.updated
	/bin/rm -f ${LOGFILE}
	/bin/mv ${LOGFILE}.updated ${LOGFILE}
fi
/bin/echo "=== After" >> ${CLEANUP_LOGFILE}
/usr/bin/wc ${LOGFILE} >> ${CLEANUP_LOGFILE}
/bin/grep -an "Start iedenchi FW update" ${LOGFILE} >> ${CLEANUP_LOGFILE}

exit 0
