#!/bin/sh

# This script is the Policy scheduler, it is a
# very important script.
# This script has implement a simple framework
# for you to add handler for new defined events.


# Log event to the log file
# args: "event msg"
log_event()
{
	echo "`date "+%y/%m/%d %H:%M:%S"` $1" 
}

# restart the whole system
# this is usually used by sig db upgrade
upgrade_sigdb()
{
	# stop datamon, xping
	cp -f ${DATAPATH}/upgrade/floweye ${PGPATH}/bin/
	cp -f ${DATAPATH}/upgrade/ipe_datamon ${PGPATH}/bin/
	cp -f ${DATAPATH}/upgrade/panaos ${PGPATH}/bin/
  
	errmsg=`cp -f ${PGETC}/version ${PGETC}/version.old 2>&1`
	errmsg=`cp -f ${DATAPATH}/upgrade/version ${PGETC}/`
	sync

	# stop releated services
	${PGPATH}/bin/ipectrl stop datamon
	${PGPATH}/bin/ipectrl stop xping
	${PGPATH}/bin/ipectrl stop panaos
	# sleep for 200ms
	sleep .2
	# start releated services
	${PGPATH}/bin/ipectrl start panaos
	${PGPATH}/bin/ipectrl start datamon
	${PGPATH}/bin/ipectrl start xping

	rm -f ${DATAPATH}/upgrade/version
	rm -f ${DATAPATH}/upgrade/floweye
	rm -f ${DATAPATH}/upgrade/ipe_datamon
	rm -f ${DATAPATH}/upgrade/panaos
	mkdir -p ${DATAPATH}/tmp
	touch ${DATAPATH}/tmp/sigdb_up_success
  
	sync
}

import_config()
{
	# stop panaos
	${PGPATH}/bin/ipectrl stop panaos
	${PGPATH}/bin/ipectrl stop panaos
	sleep 1

	# upgrade config
	cp -f ${DATAPATH}/config/panabit.conf ${PGETC}/

	# start panaos
	${PGPATH}/bin/ipectrl start panaos

	rm -f ${DATAPATH}/config/panabit.conf

	sync
}

ifreload_eve()
{
	# stop panaos
  #echo "${PGPATH}/bin/ipectrl stop" > /tmp/t.txt
	#${PGPATH}/bin/ipectrl stop
	errmsg=`${PGPATH}/bin/ipectrl stop datamon`
	sleep 1
	${PGPATH}/bin/ipectrl stop mgd
	${PGPATH}/bin/ipectrl stop xping
	${PGPATH}/bin/ipectrl stop httpd
	${PGPATH}/bin/ipectrl stop panaos
	sleep 1
  #echo "kldunload if_em" >> /tmp/t.txt
  pfctl -d
  kldunload if_em
	sleep 1

	# upgrade config
	# cp -f ${DATAPATH}/config/panabit.conf ${PGETC}/

	# start
  #echo "${PGPATH}/bin/ipectrl start" >> /tmp/t.txt
	${PGPATH}/bin/ipectrl start

	#rm -f ${DATAPATH}/config/panabit.conf
  if [ -f ${PGPATH}/bin/ifload.sh ]; then
      if [ ! -x ${PGPATH}/bin/ifload.sh ]; then   
        chmod +x ${PGPATH}/bin/ifload.sh
      fi
      ${PGPATH}/bin/ifload.sh
  fi
  
	touch ${DATAPATH}/tmp/ifreload_eve_success

	sync
}

ifreload_config()
{
	# stop panaos
  #echo "${PGPATH}/bin/ipectrl stop" > /tmp/t.txt
	#${PGPATH}/bin/ipectrl stop
	errmsg=`${PGPATH}/bin/ipectrl stop datamon`
	sleep 1
	${PGPATH}/bin/ipectrl stop mgd
	${PGPATH}/bin/ipectrl stop xping
	${PGPATH}/bin/ipectrl stop httpd
	${PGPATH}/bin/ipectrl stop panaos
	sleep 1
  #echo "kldunload if_em" >> /tmp/t.txt
  pfctl -d
  kldunload if_em
	sleep 1

	# upgrade config
	cp -f ${DATAPATH}/config/panabit.conf ${PGETC}/

	# start
  #echo "${PGPATH}/bin/ipectrl start" >> /tmp/t.txt
	${PGPATH}/bin/ipectrl start

	rm -f ${DATAPATH}/config/panabit.conf

	sync
}

clear_log()
{
	${PGPATH}/bin/ipectrl stop datamon
	${PGPATH}/bin/ipectrl stop datamon
	sleep 1

	rm -rf ${DATAPATH}/*.rrd
	${PGPATH}/bin/ipectrl start datamon
	rm -f ${DATAPATH}/config/clearlog
}

# update the whole system
upgrade_system()
{
	# Get path of package
	dir1=`find ${DATAPATH}/sysupgrade -name kernel`
	dir2=`find ${DATAPATH}/sysupgrade -name admin`
	dir3=`find ${DATAPATH}/sysupgrade -name bin`
	if [ "${dir1}" = "" -o "${dir2}" = "" -o "${dir3}" = "" ]; then
		echo "updage_system: invalid package dir!"
		return 1;
	fi

	# verify the package.
	if [ ! -f ${dir1}/joskm.ko ]; then
		echo "joskm.ko not exist!"
		return 1
	fi

	if [ ! -f ${dir1}/if_em.ko ]; then
		echo "if_em.ko not exist!"
		return 1
	fi

	if [ ! -f ${dir3}/panaos ]; then
		echo "panaos not exist!"
		return 1
	fi

	if [ ! -f ${dir3}/floweye ]; then
		echo "floweye not exist!"
		return 1
	fi

	# Step 1: upgrade kernel part
	cp -f ${dir1}/if_em.ko /boot/kernel/

	# Step 2: stop all services
	errmsg=`${PGPATH}/bin/ipectrl stop datamon`
	sleep 1
	errmsg=`${PGPATH}/bin/ipectrl stop mgd`
	errmsg=`${PGPATH}/bin/ipectrl stop xping`
	errmsg=`${PGPATH}/bin/ipectrl stop httpd`
	errmsg=`${PGPATH}/bin/ipectrl stop panaos`
	sleep 1

	# Setp 3: copy packages
	# should save .htpasswd
	cp -f ${PGPATH}/admin/.htpasswd ${DATAPATH}/.htpasswd
	cp -Rf ${dir1}/../* ${PGPATH}/
	mv ${DATAPATH}/.htpasswd ${PGPATH}/admin/.htpasswd

	# Step 4: remove the package.
	rm -rf ${DATAPATH}/sysupgrade/*

	# Step 5: restart the whole system
	${PGPATH}/bin/ipectrl start
	
	# Step 6: exit
	echo "Upgrade ok, monitor exit!"

	# tell the success flag.
	mkdir -p ${DATAPATH}/tmp
	touch ${DATAPATH}/tmp/sys_up_success
	sync
	exit 0
}

# update the whole system
upgrade_system2()
{
	# Get path of package
	dir1=`find ${DATAPATH}/sysupgrade -name kernel`
	dir2=`find ${DATAPATH}/sysupgrade -name admin`
	dir3=`find ${DATAPATH}/sysupgrade -name bin`
	if [ "${dir1}" = "" -o "${dir2}" = "" -o "${dir3}" = "" ]; then
		echo "updage_system: invalid package dir!"
		return 1;
	fi

	# verify the package.
	if [ ! -f ${dir1}/joskm.ko ]; then
		echo "joskm.ko not exist!"
		return 1
	fi

	if [ ! -f ${dir1}/if_em.ko ]; then
		echo "if_em.ko not exist!"
		return 1
	fi

	if [ ! -f ${dir3}/panaos ]; then
		echo "panaos not exist!"
		return 1
	fi

	if [ ! -f ${dir3}/floweye ]; then
		echo "floweye not exist!"
		return 1
	fi

	# Step 1: upgrade kernel part
	cp -f ${dir1}/if_em.ko /boot/kernel/


	# Setp 2: save .htpasswd and copy packages
	cp -f ${PGPATH}/admin/.htpasswd ${DATAPATH}/.htpasswd
	cp -Rf ${dir1}/../* ${PGPATH}/
	mv ${DATAPATH}/.htpasswd ${PGPATH}/admin/.htpasswd
	rm -rf ${DATAPATH}/sysupgrade/*
	sync

	# Step 3: stop all services except monitor
	errmsg=`${PGPATH}/bin/ipectrl stop datamon`
	sleep .1
	errmsg=`${PGPATH}/bin/ipectrl stop mgd`
	sleep .1
	errmsg=`${PGPATH}/bin/ipectrl stop xping`
	sleep .1
	errmsg=`${PGPATH}/bin/ipectrl stop httpd`
	sleep .1
	errmsg=`${PGPATH}/bin/ipectrl stop panaos`

	# sleep for 300ms and restart the whole system
	sleep .3
	# Step 5: restart the whole system
	${PGPATH}/bin/ipectrl start
	sleep .3
	
	# Step 5: exit
	echo "Upgrade ok, monitor exit!"
	# tell the success flag.
	mkdir -p ${DATAPATH}/tmp
	touch ${DATAPATH}/tmp/sys_up_success
	sync

	# exit this monitor as the new monitor has 
	# been running
	exit 0
}

# handle all events
handle_events()
{
	local evtfile=$1
	local event
	local arg1
	local arg2

	exec 0<${evtfile}
	while read event arg1 arg2
	do
		case ${event} in
		"upgrade_sigdb")
			upgrade_sigdb
			;;

		"upgrade_system")
			upgrade_system2
			;;

		"import_config")
			import_config
			;;

		"ifreload_eve")
			ifreload_eve
			;;
      
		"ifreload_config")
			ifreload_config
			;;

		"clear_log")
			clear_log
			;;

		*)
			log_event "WARNNING Unknown event \"${event}\"!"
			;;
		esac
	done
}


# Assign all interrupts to CPU0
irq_bind()
{
        osver=`uname -r | cut -d'.' -f1`
        [ ${osver} -lt 8 ] && return

        for irqno in `vmstat -i | grep "^irq" | cut -d':' -f1 | sed -e "s/irq//g"`
        do
                cpuset -l0 -x ${irqno}
        done
}

. /etc/PG.conf
ADMIN=${PGPATH}/admin
FLOWEYE=${PGPATH}/bin/floweye
HTTPD=${PGPATH}/bin/ipe_httpd
pending_event_file=${PGETC}/log/pending_events
current_event_file=${PGETC}/log/current_events
irqbind_count=0
canbindirq=0
osver=`uname -r | cut -d'.' -f1`
[ ${osver} -ge 8 ] && canbindirq=1

while [ 1 -eq 1 ]; do
	sleep 6

	# check healthy of httpd
	httpd_running=0
	for cmd in `ps -ax | awk '{print $5}'`; do
		if [ "${cmd}" = "${HTTPD}" ]; then
			httpd_running=1
			break
		fi
	done

	if [ ${httpd_running} -eq 0 ]; then
		${HTTPD} -u root -T gb2312 -S -E ${ADMIN}/admin.pem -d ${ADMIN} -M 1
	fi

	# Process pending events
	if [ -f ${pending_event_file} ]; then
		mv ${pending_event_file} ${current_event_file}
		handle_events ${current_event_file}
		rm -f ${current_event_file}
	fi

	if [ ${canbindirq} -ne 0 -a ${irqbind_count} -le 5 ]; then
		irq_bind
		irqbind_count=`expr ${irqbind_count} + 1`
	fi

	# show led information
        if [ "${MACHINE}" = "LH7536" ]; then
                summary=`${FLOWEYE} app stat showsum app=group | grep total`
                bpsup=`echo ${summary} | cut -d' ' -f13`
                bpsdown=`echo ${summary} | cut -d' ' -f14`
                ${PGPATH}/bin/lcdshow cuau1 ${bpsup} ${bpsdown}
        fi
done
