#!/bin/bash2
#
# Script executed by /etc/init.d/phion at boot time

#######################################################################################################

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/etc/phion/bin:/opt/phion/bin"


#MYLOGFLE=box_boot_phion

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

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


[ _$H = _ ] && eval H=0 

let H++
PREFIX=""

for ((k=1 ; H-k ; k++)); do
	PREFIX="${PREFIX} $(SUB)"
done
export H=$H

ARG0="${PREFIX} bootrc"




ARGB=bootrc
ARGS="$$"

function startACPF 
{
	${PHION_ROOT}/bin/acpfctrl start

	MMAP=/proc/phion/acpf_mmap
	DEV=/dev/acpf

	[ ! -c /dev/acpf -a -f ${MMAP} ] && {
		set -- $(cat ${MMAP})
		mknod ${DEV} c ${1} ${2}
		chmod 0600 ${DEV}
	}       
}

function initConf {

	[ ! -d /opt/phion/config/configroot/boxsrv ] && /bin/mkdir -p /opt/phion/config/configroot/boxsrv
	[ ! -d /opt/phion/config/active ] && /bin/mkdir -p /opt/phion/config/active

	if [ -f /opt/phion/config/configroot/boxsrv/boxfw.lfwrule7 ]; then 
		:
	elif [ -f /opt/phion/config/active/boxfw.lfwrule7 ]; then
		/bin/cp -f /opt/phion/config/active/boxfw.lfwrule7 /opt/phion/config/configroot/boxsrv/boxfw.lfwrule7
	else
		/bin/cp -f /opt/phion/modules/box/boxsrv/boxfw/default.lfwrule7 /opt/phion/config/configroot/boxsrv/boxfw.lfwrule7
		/bin/cp -f /opt/phion/modules/box/boxsrv/boxfw/default.lfwrule7 /opt/phion/config/active/boxfw.lfwrule7
	fi

	# this should be obsolete because the module is loaded with full ruleset later on.
	# at the moment it only issues an error message, because acpf is not loaded when initConf is called
	#[ -x /opt/phion/bin/acpfrule ] && /opt/phion/bin/acpfrule i /opt/phion/config/configroot/boxsrv/boxfw.lfwrule7


	if [ -f /opt/phion/config/configroot/boxnet.conf ]; then 
		:
	elif [ -f /opt/phion/config/active/boxnet.conf ]; then
		/bin/cp -f /opt/phion/config/active/boxnet.conf /opt/phion/config/configroot/boxnet.conf
	else
		/bin/cp -f /opt/phion/modules/box/boxnet/default.conf /opt/phion/config/configroot/boxnet.conf
		/bin/cp -f /opt/phion/modules/box/boxnet/default.conf /opt/phion/config/active/boxnet.conf
	fi


	if [ -f /opt/phion/config/configroot/boxadm.conf ]; then 
		:
	elif [ -f /opt/phion/config/active/boxadm.conf ]; then
		/bin/cp -f /opt/phion/config/active/boxadm.conf /opt/phion/config/configroot/boxadm.conf
	else
		/bin/cp -f /opt/phion/modules/box/boxadm/default.conf /opt/phion/config/configroot/boxadm.conf
		/bin/cp -f /opt/phion/modules/box/boxadm/default.conf /opt/phion/config/active/boxadm.conf
	fi
}

$logerror "Info" "${ARG0}[$$]: START, CloudGen Firewall startup preparations"  
	
########################################
# do install things
########################################
touch  /var/run/watchdog.state.service


[ -f "$PHION_ROOT"/run/autodetect.conf ] && cp "$PHION_ROOT"/run/autodetect.conf /tmp
[ -d "$PHION_ROOT"/run ] && rm -rf "$PHION_ROOT"/run/*
[ -f /tmp/autodetect.conf ] && mv /tmp/autodetect.conf "$PHION_ROOT"/run/
[ -f /opt/phion/config/active/firewall.path ] && rm -f /opt/phion/config/active/firewall.path
[ -f /opt/phion/config/active/firewall2.path ] && rm -f /opt/phion/config/active/firewall2.path
$logerror "Info" "${ARG0}[$$]: cleared out ${PHION_ROOT}/run/"


if [ ! -d "$PHION_CONF"/active/data ]; then
	mkdir -p "$PHION_CONF"/active/data
        touch  "$PHION_CONF"/active/data/admin.dbconf 
elif [ ! -f "$PHION_CONF"/active/data/admin.dbconf ]; then
	touch  "$PHION_CONF"/active/data/admin.dbconf 
fi  


########################################
# finally put existing configuration archive into right place
########################################

if [ -f "$PHION_ROOT"/update/box.par ]; then

	$logerror "Security" "${ARG0}[$$]: new configuration archive box.par found"
	if [ ! -d "$PHION_CONF"/active ]; then 
		mkdir -p "$PHION_CONF"/active
	else
		rm -rf "$PHION_CONF"/active/*
		$logerror "Notice" "${ARG0}[$$]: old configuration erased"
	fi

	mkdir -p "$PHION_CONF"/active/data
	touch  "$PHION_CONF"/active/data/admin.dbconf
	
	if [ -x "$PHION_ROOT"/bin/procpar ]; then
		touch /etc/phion/INSTALL
		$logerror "Info" "${ARG0}[$$]: execute ${PHION_ROOT}/bin/procpar -b ${PHION_ROOT}/update/box.par "
		#touch  /var/run/watchdog.state.service
		"$PHION_ROOT"/bin/procpar -b "$PHION_ROOT"/update/box.par 
		$logerror "Security" "${ARG0}[$$]: new configuration deployed"
	else
		$logerror "Error" "${ARG0}(${ARGS}): missing executable ( $PHION_ROOT/bin/procpar )"
	fi

	cp -f "$PHION_ROOT"/update/box.par "$PHION_ROOT"/update/box.par.last 
	$logerror "Notice" "${ARG0}[$$]: preserved configuration archive box.par as "$PHION_ROOT"/update/box.par.last " 
	rm -f "$PHION_ROOT"/update/box.par 
	$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.par" 

	if [ -f "$PHION_ROOT"/update/box.ppar ]; then
		rm -f "$PHION_ROOT"/update/box.ppar
		$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.ppar"
	fi

	# rename remaining config archives as well, otherwise we would trigger a re-configuration next time
	if [ -f "$PHION_ROOT"/update/box.pgz ]; then
		mv -f "$PHION_ROOT"/update/box.pgz "$PHION_ROOT"/update/box.pgz.last 
		$logerror "Notice" "${ARG0}[$$]: renamed configuration archive $PHION_ROOT/update/box.pgz as $PHION_ROOT/update/box.pgz.last"
	fi
	if [ -f "$PHION_ROOT"/update/box.pca ]; then
		mv -f "$PHION_ROOT"/update/box.pca "$PHION_ROOT"/update/box.pca.last 
		$logerror "Notice" "${ARG0}[$$]: renamed configuration archive $PHION_ROOT/update/box.pca as $PHION_ROOT/update/box.pca.last"
	fi

	initConf
	startACPF
		
	$logerror "Info" "${ARG0}[$$]: END"
	exit 0  # from here 

elif [ -f "$PHION_ROOT"/update/box.pgz ]; then

	$logerror "Security" "${ARG0}[$$]: new configuration archive box.pgz found"
	if [ ! -d "$PHION_CONF"/active ]; then 
		mkdir -p "$PHION_CONF"/active
	else
		rm -rf "$PHION_CONF"/active/*
		$logerror "Notice" "${ARG0}[$$]: old configuration erased"
	fi

	mkdir -p "$PHION_CONF"/active/data
	touch  "$PHION_CONF"/active/data/admin.dbconf
	
	$logerror "Info" "${ARG0}[$$]: decompressing ${PHION_ROOT}/update/[box.pgz -> box.par] "
	gunzip -cf "$PHION_ROOT"/update/box.pgz > "$PHION_ROOT"/update/box.par
	
	if [ -x "$PHION_ROOT"/bin/procpar ]; then
		touch /etc/phion/INSTALL
		$logerror "Info" "${ARG0}[$$]: execute ${PHION_ROOT}/bin/procpar -b ${PHION_ROOT}/update/box.par "
		#touch  /var/run/watchdog.state.service
		"$PHION_ROOT"/bin/procpar -b "$PHION_ROOT"/update/box.par 
		$logerror "Security" "${ARG0}[$$]: new configuration deployed"
	else
		$logerror "Error" "${ARG0}(${ARGS}): missing executable ( $PHION_ROOT/bin/procpar )"
	fi

	cp -f "$PHION_ROOT"/update/box.pgz "$PHION_ROOT"/update/box.pgz.last 
	$logerror "Notice" "${ARG0}[$$]: preserved configuration archive box.pgz as "$PHION_ROOT"/update/box.pgz.last " 
	rm -f "$PHION_ROOT"/update/box.pgz "$PHION_ROOT"/update/box.par
	$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.pgz" 

	if [ -f "$PHION_ROOT"/update/box.ppar ]; then
		rm -f "$PHION_ROOT"/update/box.ppar
		$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.ppar"
	fi

	# rename remaining config archives as well, otherwise we would trigger a re-configuration next time
	if [ -f "$PHION_ROOT"/update/box.pca ]; then
		mv -f "$PHION_ROOT"/update/box.pca "$PHION_ROOT"/update/box.pca.last 
		$logerror "Notice" "${ARG0}[$$]: renamed configuration archive $PHION_ROOT/update/box.pca as $PHION_ROOT/update/box.pca.last"
	fi

	initConf
	startACPF

	$logerror "Info" "${ARG0}[$$]: END"
	exit 0  # from here 

elif [ -f "$PHION_ROOT"/update/box.pca ]; then

	$logerror "Security" "${ARG0}[$$]: new configuration archive box.pca found"

	if [ ! -x "$PHION_ROOT"/bin/phionar ]; then
		$logerror "Error" "${ARG0}(${ARGS}): missing executable ( $PHION_ROOT/bin/phionar )"
	else
		$logerror "Info" "${ARG0}[$$]: decrypting ${PHION_ROOT}/update/[box.pca -> box.par] "
		SERIAL=$(boxinfo config <(hwtool -e) / sn)
		if [ -z "$SERIAL" ]; then
			$logerror "Error" "${ARG0}(${ARGS}): missing serial number: cannot decrypt box.pca"
		else
			OUTPUT=$(phionar D "$PHION_ROOT"/update/box.pca "$PHION_ROOT"/update/box.par -P "$SERIAL")
			if [ -n "$OUTPUT" ]; then
				$logerror "Error" "${ARG0}(${ARGS}): error while decrypting box.pca"
			elif [ -x "$PHION_ROOT"/bin/procpar ]; then
				touch /etc/phion/INSTALL

				if [ ! -d "$PHION_CONF"/active ]; then
					mkdir -p "$PHION_CONF"/active
				else
					rm -rf "$PHION_CONF"/active/*
					$logerror "Notice" "${ARG0}[$$]: old configuration erased"
				fi

				mkdir -p "$PHION_CONF"/active/data
				touch  "$PHION_CONF"/active/data/admin.dbconf

				$logerror "Info" "${ARG0}[$$]: execute ${PHION_ROOT}/bin/procpar -b ${PHION_ROOT}/update/box.par "
				#touch  /var/run/watchdog.state.service
				"$PHION_ROOT"/bin/procpar -b "$PHION_ROOT"/update/box.par
				$logerror "Security" "${ARG0}[$$]: new configuration deployed"
			else
				$logerror "Error" "${ARG0}(${ARGS}): missing executable ( $PHION_ROOT/bin/procpar )"
			fi
		fi
	fi

	cp -f "$PHION_ROOT"/update/box.pca "$PHION_ROOT"/update/box.pca.last
	$logerror "Notice" "${ARG0}[$$]: preserved configuration archive box.pca as "$PHION_ROOT"/update/box.pca.last "
	rm -f "$PHION_ROOT"/update/box.pca "$PHION_ROOT"/update/box.par
	$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.pca"

	if [ -f "$PHION_ROOT"/update/box.ppar ]; then
		rm -f "$PHION_ROOT"/update/box.ppar
		$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.ppar"
	fi
	
	initConf
	startACPF
		
	$logerror "Info" "${ARG0}[$$]: END"
	exit 0  # from here

elif [ -f "$PHION_ROOT"/update/box.ppar ]; then

	$logerror "Security" "${ARG0}[$$]: new configuration archive box.ppar found"
	if [ ! -d "$PHION_CONF"/active ]; then
		mkdir -p "$PHION_CONF"/active
	fi

	mkdir -p "$PHION_CONF"/active/data
	touch  "$PHION_CONF"/active/data/admin.dbconf

	if [ -x "$PHION_ROOT"/bin/procpar ]; then
		touch /etc/phion/INSTALL
		$logerror "Info" "${ARG0}[$$]: execute ${PHION_ROOT}/bin/procpar -b -p ${PHION_ROOT}/update/box.ppar "
		#touch  /var/run/watchdog.state.service
		"$PHION_ROOT"/bin/procpar -b -p "$PHION_ROOT"/update/box.ppar
		$logerror "Security" "${ARG0}[$$]: new configuration deployed"
		else
		$logerror "Error" "${ARG0}(${ARGS}): missing executable ( $PHION_ROOT/bin/procpar )"
		fi

	cp -f "$PHION_ROOT"/update/box.ppar "$PHION_ROOT"/update/box.ppar.last
	$logerror "Notice" "${ARG0}[$$]: preserved configuration archive box.ppar as "$PHION_ROOT"/update/box.ppar.last "
	rm -f "$PHION_ROOT"/update/box.ppar
	$logerror "Notice" "${ARG0}[$$]: removed configuration archive "$PHION_ROOT"/update/box.ppar"

	initConf
	startACPF

	$logerror "Info" "${ARG0}[$$]: END"
	exit 0  # from here

elif [ -f "$PHION_ROOT"/update/boxpriv.pem ]; then 

	$logerror "Security" "${ARG0}[$$]: found new box certificate in update dir" 
	[ -x "$PHION_ROOT"/modules/box/boxkey/bin/activate ] && {
		"$PHION_ROOT"/modules/box/boxkey/bin/activate 
		RET=$?
		[ $RET = 0 ] && $logerror "Security" "${ARG0}[$$]: activated new box certificate" 
	}	
	rm  -f "$PHION_ROOT"/update/boxpriv.pem 
	$logerror "Notice" "${ARG0}[$$]: removed new box certificate from update dir" 
fi


initConf
startACPF


$logerror "Info" "${ARG0}[$$]: END"
	

exit 1 # exit code 1 means the config did not change


