#! /bin/sh
#
# Author:       martin huter <m.huter@phion.com>
#		Copyright phion AG 2001-2009
#
### BEGIN INIT INFO
# Provides: phion
# Required-Start: $local_fs $remote_fs $time
# Should-Start: 
# Required Stop: $local_fs $remote_fs
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: phion base system
# Description: start the phion base sysem layer
# END INIT INFO 

# Source function library.
. /etc/rc.d/init.d/functions

ARG0=phionrc
ARGB=phionrc
ARGS="$1"

MYLASTLOG=/var/log/phionstart
MYAUTODETECTLOG=/var/log/phionautodetect


PMODPATH="/opt/phion/modules/box/boxsys"
[ -f ${PMODPATH}/moddefs.conf ] && source ${PMODPATH}/moddefs.conf  
MYLOGFLE="${logfile_phion:-"box_System_phionrc"}"
#MYLOGFLE=box_boot_phion
STATUS=0




# Source phion environment and phion options
[ -f /etc/phion/phion.sh ] &&  source /etc/phion/phion.sh
[ -f /etc/phion/options ]  &&  source /etc/phion/options
if [ -f /etc/phion/functions ]; then
  	source /etc/phion/functions
	logerror="LogError $MYLOGFLE"
else
	logerror=echo
fi


$logerror "Info" "${ARG0}[$$]: BEGIN, executing /etc/init.d/phion script ${1} "


# Check whether networking is required.

[ "_${BOX_NETWORK}" = "_no" ] && {
	echo -n "Starting CloudGen Firewall base layer: "
	echo_passed
	echo
	exit 0
}

# See how we were called.

case "_$1" in
  _start|_recover)

	# process autoinstallation-stick post-install
	if [ -f /opt/phion/preserve/autoinstallation-stick.firstboot ]; then
		# disable watchdog repair stuff: watchdog will probably be started (S60)
		touch /var/run/watchdog.state.service
		$logerror "Info" "${ARG0}[$$]: execute /opt/phion/bin/autoinstallation-stick > $MYAUTODETECTLOG 2>&1"
		rm -f /opt/phion/preserve/autoinstallation-stick.firstboot
		/opt/phion/bin/autoinstallation-stick > $MYAUTODETECTLOG 2>&1
		# autoinstallation-stick schedules a reboot sequence. therefore we do not proceed in booting
		exit 0
	fi

	# recreate keys if needed
	if [ -f /opt/phion/preserve/create-new-boxkeys ]; then
		/opt/phion/modules/box/boxkey/bin/activate -N -s -b
		rm -f /opt/phion/preserve/create-new-boxkeys
	fi

	# default core pattern (might get overwritten by core collector)
	mkdir -p "/var/phion/crash"
	echo "/var/phion/crash/core.%e" > /proc/sys/kernel/core_pattern
	echo "0" > /proc/sys/kernel/core_uses_pid

	if [ -n "${CORE_COLLECTOR}" -a -x "${CORE_COLLECTOR}" ]; then
		echo -n "Enable Core Collector: "
		${CORE_COLLECTOR} install &>/dev/null
		if [ $? -eq 0 ]; then
			echo_passed
			echo
		else 
			echo_failure
			echo
		fi
	fi

	#always start with sysctrls!
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOXSYS -b "
 	$PHION_MAKEBOXSYS -b > $MYLASTLOG 2>&1

	$logerror "Info" "${ARG0}[$$]: execute /etc/phion/rc.d/bootrc"
	echo -n "Starting CloudGen Firewall: "
	export H=1
	rm -f /boot/grub/grub.conf.phionsave
	/etc/phion/rc.d/bootrc >> $MYLASTLOG 2>&1
	UPDATE=$?

	# bootrc may have unpacked a par file which may have altered grub.conf
	[ -f /boot/grub/grub.conf.phionsave ] && {
		diff -bBwq /boot/grub/grub.conf.phionsave /boot/grub/grub.conf
		if [ $? -ne 0 ]; then
			rm -f /boot/grub/grub.conf.phionsave
			echo_passed
			echo -n "Reboot due to changed grub configuration"
			touch /var/run/watchdog.state.service
			reboot
			exit 0
		fi
		rm -f /boot/grub/grub.conf.phionsave
	}
	export H=""

	$logerror "Info" "${ARG0}[$$]: execute $PHION_CONTROLD box start bsyslog "
	$PHION_CONTROLD box start bsyslog >> $MYLASTLOG 2>&1

	# phionflash is started in /etc/rc.d/rc.sysinit 
	# FLASHRAM_PANIC will be set in /etc/phion/options 
	if [ ${FLASHRAM_PANIC:-N} != "Y" ]; then
		$logerror "Info" "${ARG0}[$$]: execute $PHION_CONTROLD box start psyslog "
		$PHION_CONTROLD box start psyslog >> $MYLASTLOG 2>&1
	fi

	# if we did not have a par file, run this. because otherwise it would have been called by procpar.
	if [ "_$UPDATE" = "_1" ]; then
		$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOXADM -b "
 		$PHION_MAKEBOXADM -b >> $MYLASTLOG 2>&1
	
		# ensure that message texts are correctly reset at each startup
		[ -x /opt/phion/modules/box/boxother/messageboard/bin/activate ] && {
			/opt/phion/modules/box/boxother/messageboard/bin/activate >> $MYLASTLOG 2>&1 &
		}
	fi

	# note that with -b device dependent sysctrls are not activated!!
	# FD: this runs twice if we have a par file. Do not know why.
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOX -b "
 	$PHION_MAKEBOX -b >> $MYLASTLOG 2>&1

	#if [ "_$UPDATE" = "_1" ]; then
	# means NO  new par has been found nor been unpacked (activates would have been run with -b option) 
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOXSYS -d "
 	$PHION_MAKEBOXSYS -d >> $MYLASTLOG 2>&1
	# required to clean up device specific settings after network has been brought up in boot mode!!
	#fi

	# determine appliance model and read serial number
	if [ -x $PHION_ROOT/bin/hwtool ]; then
	        $PHION_ROOT/bin/hwtool -e > $PHION_ROOT/run/appliance.state 2>> ${MYLASTLOG}
	        $logerror "Info" "${ARG0}[$$]: execute  $PHION_ROOT/bin/hwtool -e > $PHION_ROOT/run/appliance.state 2>> ${MYLASTLOG}"
	fi

	# retrieve geoip information (in the background)
	/usr/bin/systemd-run --quiet /opt/phion/bin/geoip

	# start the NG layer
	if [ $PHION_START = "Y" ] && [ "${FLASHRAM_PANIC:-N}" != "Y" ] && [ -x $PHION_CONTROLD ]; then
		$logerror "Info" "${ARG0}[$$]: execute $PHION_CONTROLD startup"
		$PHION_CONTROLD startup >> ${MYLASTLOG} 2>&1
		[[ -f /var/run/watchdog.state.service ]] && rm -f /var/run/watchdog.state.service
		
		if [[ -f /opt/phion/preserve/network_ospf_startup ]]; then			# remedies effects of a crash
			TMP=$(/bin/cat /opt/phion/run/network_ospf_startup 2>/dev/null)
			[[ "${TMP:-0}" = "1" ]] && {	
				/opt/phion/bin/phionctrl module start ospf >> ${MYLASTLOG} 2>&1	# control and servers must still be running!
				rm -f /opt/phion/preserve/network_ospf_startup	# make sure that  startup is not used again
			}
		fi
	fi

	# wait until controld has started (see BNNGF-60931)
	while ! (kill -0 "$(cat /var/run/control.pid)") &> /dev/null; do sleep 1; done

	if [ -f /opt/phion/config/active/boxnetha.conf ]; then
		$PHION_ROOT/modules/box/boxsrv/dist/bin/dist2client -o ha -a '-e' -p exec -f /opt/phion/bin/hwtool -P > $PHION_ROOT/run/applianceha.state 2>> ${MYLASTLOG}
		$logerror "Info" "${ARG0}[$$]: execute $PHION_ROOT/modules/box/boxsrv/dist/bin/dist2client -o ha -a '-e' -p exec -f /opt/phion/bin/hwtool -P > $PHION_ROOT/run/applianceha.state 2>> ${MYLASTLOG}"
	fi

	if [ ${FLASHRAM_PANIC:-N} = "Y" ]; then
		echo -n "Check flash log file! "
		$logerror "Fatal" "${ARG0}[$$]: Entering flash disk panic mode. See flash log file for details."
		/opt/phion/bin/acpfctrl start
		/opt/phion/bin/acpfrule install
		echo_failure
		echo
	else
		echo_success
		echo
	fi

	if [ -x $PHION_ROOT/bin/boxkey ]; then
		$PHION_ROOT/bin/boxkey >> ${MYLASTLOG} 2>&1
		$logerror "Info" "${ARG0}[$$]: execute  $PHION_ROOT/bin/boxkey"
	fi

	if [ -f /usr/share/dbus-1/system-services/org.freedesktop.ConsoleKit.service ]; then
		rm /usr/share/dbus-1/system-services/org.freedesktop.ConsoleKit.service
	fi

	touch /var/lock/subsys/phion
	[[ -f /var/run/watchdog.state.service ]] && rm -f /var/run/watchdog.state.service

	if [ -f /var/phion/preserve/getpar/cleartoken -o -f /var/phion/preserve/getpar/boxlic.conf ]; then 
		/opt/phion/bin/getpar -C 
	fi 

	;;
  _stop)
	if [ -x $PHION_CONTROLD ]; then
		echo -n "Stopping CloudGen Firewall: "
		$logerror "Info" "${ARG0}[$$]: execute $PHION_CONTROLD shutdown"
		touch  /var/run/watchdog.state.service
		$PHION_CONTROLD shutdown >> $MYLASTLOG 2>&1
		echo_success
		echo
	fi
	echo -n "Stopping CloudGen Firewall networking: "
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOX -s"
 	$PHION_MAKEBOX -s >> $MYLASTLOG 2>&1
	$logerror "Info" "${ARG0}[$$]: execute  $PHION_MAKEBOXADM -s"
 	$PHION_MAKEBOXADM -s >> $MYLASTLOG 2>&1
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOXSYS -s" 
 	$PHION_MAKEBOXSYS -s >> $MYLASTLOG 2>&1
	# make sure that ospf service comes up again on restart/reboot when it has been stopped previously by boxnet
	if [[ -f /opt/phion/run/network_ospf_startup ]]; then			# remedies actions of makebox -s
		TMP=$(/bin/cat /opt/phion/run/network_ospf_startup)
		[[ "${TMP:-0}" = "1" ]] && {	
			/opt/phion/bin/phionctrl module stop ospf >> YLASTLOG 	# control and servers must still be running!
			rm -f  /opt/phion/run/network_ospf_startup
			rm -f /opt/phion/preserve/network_ospf_startup	# make sure that  startup is not used again
		}
	fi
	
	/opt/phion/bin/acpfctrl stop >> $MYLASTLOG 2>&1
	rm -f /var/lock/subsys/phion
	killall phionflash 2>/dev/null

	#remove ips timestamp file
	[ -f /var/phion/mcdownload/ips/ruleuptime.txt ] && rm -f /var/phion/mcdownload/ips/ruleuptime.txt
	echo_success
	echo
	;;

  _restart)
 	echo -n "Stopping CloudGen Firewall processes: "
	if [ $PHION_START = "Y" ] && [ -x $PHION_CONTROLD ]; then
		$logerror "Info" "${ARG0}[$$]: execute $PHION_CONTROLD shutdown"
		$PHION_CONTROLD shutdown >> $MYLASTLOG 2>&1
		echo_success
		echo
	fi
 	echo -n "Refreshing box status: "
 	$PHION_MAKEBOXADM -r >> $MYLASTLOG 2>&1
 	$PHION_MAKEBOX -r >> $MYLASTLOG 2>&1
 	$PHION_MAKEBOXSYS -r >> $MYLASTLOG 2>&1
	if [ $PHION_START = "Y" ] && [ -x $PHION_CONTROLD ]; then
		$logerror "Info" "${ARG0}[$$]: execute $PHION_CONTROLD startup"
		$PHION_CONTROLD startup >> $MYLASTLOG 2>&1
	fi
	echo_success
	echo
	;;

  _adm_refresh)
 	echo -n "Refreshing box administration part: "
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOXSYS -a" 
 	$PHION_MAKEBOXSYS -a >> $MYLASTLOG 2>&1
	$logerror "Info" "${ARG0}[$$]: execute $PHION_MAKEBOXADM -a" 
 	$PHION_MAKEBOXADM -a >> $MYLASTLOG 2>&1
	echo_success
	echo
	;;

  _status)
	if [ -f /var/lock/subsys/phion ]; then
	   	echo "CloudGen Firewall system is running ..."
		exit 0
	else
	   	echo "CloudGen Firewall system is stopped"
		exit 3
	fi
	;;

  *)
	STATUS=1
	echo "Usage: phion {start|stop|recover|restart|adm_refresh}"
	$logerror "Error" "${ARG0}[$$]: value of ARG1 out of scope -->${1}<--"
	exit $STATUS 
esac

$logerror "Info" "${ARG0}[$$]: END, /etc/init.d/phion script ${1}"
exit $STATUS
