#!/bin/bash2

# author: K. M. Gheri
#		 phion IT GmbH
#		 Copyright 2002
#

# PhionVersionString 3.2.0.1

# $Id: swupdate_activate,v 2.2.4.1 2005/05/04 12:43:15 root Exp root $


set -f

if [ -f /etc/phion/phion.sh ]; then
		source /etc/phion/phion.sh
fi

if [ -f /etc/phion/options ]; then
		source /etc/phion/options
fi


if [ -f /etc/phion/functions ]; then
		source /etc/phion/functions
fi

export H=1

umask 177

getopts ":b" opt
case $opt in
		b  )
				shift 1
				;;
esac		

PMODPATH="/opt/phion/modules/box/boxother/swupdate"

[ -f ${PMODPATH}/moddefs.conf ] && source ${PMODPATH}/moddefs.conf  

MYLOGFLE="${logfile_activate:-"box_Release_settings"}"

logerror="LogError $MYLOGFILE"

ARG1=${1:-"/opt/phion/config/active/swupdate.conf"}

ARG0=activate
ARGS=$ARG1
ARGB=$ARG0

DOWNLOAD_CONF="/opt/phion/modules/download/firmwareupdates/download.conf"
MCDOWNLOAD_CONF="/var/phion/mcdownload/firmwareupdates/download.conf"
ROLLOUT="/opt/phion/preserve/swupdate.op"



if [ ! -r $ARG1 ]; then
	$logerror "Error" "activate(): unreadable conf file $ARG1"
	$logerror "Error" "activate($ARG1)->1: cannot parse configuration data" 
	exit 1 
elif [ ! -s $ARG1 ]; then
	$logerror "Error" "activate(): empty conf file $ARG1"
	$logerror "Error" "activate($ARG1)->1: cannot parse configuration data" 
	exit 0
fi




# read in conf-file


function readCONFs
{
	if [ ! -x "$PHION_ROLLOUT" ]; then
			$logerror "Error" "readCONFs(): missing executable $PHION_ROLLOUT"
			$logerror "Error" "readCONFs()->1: cannot parse configuration data"
			return 1
	fi

	ARG1=${ARG1:-"/opt/phion/config/active/swupdate.conf"}

	if [ -r "$ARG1" ]; then
		[[ ! -d /etc/phion/dynconf/ ]] && /bin/mkdir -p /etc/phion/dynconf/
		$PHION_ROLLOUT ${ARG1} /etc/phion/dynconf/swupdate.conf.rollout.$$ &>/dev/null
		[ -f /opt/phion/preserve/swupdate.op ] && cp /opt/phion/preserve/swupdate.op  /opt/phion/preserve/swupdate.op.last
		if [ -s /etc/phion/dynconf/swupdate.conf.rollout.$$ ]; then 
			mv /etc/phion/dynconf/swupdate.conf.rollout.$$ /opt/phion/preserve/swupdate.op
		else
			rm -f /etc/phion/dynconf/swupdate.conf.rollout.$$
		fi

	else
		$logerror "Error" "readCONFs(): unreadable conf file $ARG1"
			$logerror "Error" "readCONFs()->1: cannot parse configuration data" 
		return 1
		fi

	return 0

}

# write CHECKINT, ENABLENOTIFICATIONS, and LOGLEVEL
# to /var/phion/mcdownload/firmwareupdates/download.conf

function writeToDownloadConf
{
	if [ -n "$(grep "^MAINADMIN[[:space:]]*=[[:space:]]*[Yy][[:space:]]*$" "$PHION_BOXCONF")" ]; then # is a managed box
		return 0;
	fi

	if [ -n "$(grep "^PLATFORM[[:space:]]*=[[:space:]]*mc[[:space:]]*$" "$PHION_BOXCONF")" ]; then # is a Control Center
		return 0;
	fi

	[ -r "$ROLLOUT" ] && source "$ROLLOUT"
	if [ -z "$box_ENABLENOTIFICATIONS" ]; then
		box_ENABLENOTIFICATIONS="n"
	fi

	if [ ! -w "$MCDOWNLOAD_CONF" ]; then
		if [ ! -r "$DOWNLOAD_CONF" ]; then
			$logerror "Error" "activate($ARG1): cannot read $DOWNLOAD_CONF"
			return 1
		fi
		cp -f "$DOWNLOAD_CONF" "$MCDOWNLOAD_CONF"
		if [ ! -w "$MCDOWNLOAD_CONF" ]; then
			$logerror "Error" "activate($ARG1): cannot write to $MCDOWNLOAD_CONF"
			return 1
		fi
	fi

	if [ -n "$(grep "^ENABLEDONBOX[[:space:]]*=" "$MCDOWNLOAD_CONF")" ]; then
		sed -i -e "s/ENABLEDONBOX[[:space:]]*=.*$/ENABLEDONBOX = ${box_ENABLENOTIFICATIONS}/" "$MCDOWNLOAD_CONF"
	else
		echo "ENABLEDONBOX = ${box_ENABLENOTIFICATIONS}" >> "$MCDOWNLOAD_CONF"
	fi
	if [ -n "$(grep "^CHECKINT[[:space:]]*=" "$MCDOWNLOAD_CONF")" ]; then
		sed -i -e "s/CHECKINT[[:space:]]*=.*$/CHECKINT = ${box_CHECKINT}/" "$MCDOWNLOAD_CONF"
	else
		echo "CHECKINT = ${box_CHECKINT}" >> "$MCDOWNLOAD_CONF"
	fi
	if [ -n "$(grep "^LOGLEVEL[[:space:]]*=" "$MCDOWNLOAD_CONF")" ]; then
		sed -i -e "s/LOGLEVEL[[:space:]]*=.*$/LOGLEVEL = ${box_LOGLEVEL}/" "$MCDOWNLOAD_CONF"
	else
		echo "LOGLEVEL = ${box_LOGLEVEL}" >> "$MCDOWNLOAD_CONF"
	fi
}

function checkART
{
	[ -r "$ROLLOUT" ] && source "$ROLLOUT"

	if [[ ${box_BUILDARTTREE} = n && -f /boot/art/art.tar.gz ]]; then
		/bin/rm -f /boot/art/art.tar.gz
	elif [[ ${box_BUILDARTTREE} = y && ! -f /boot/art/art.tar.gz && ! -f /opt/phion/preserve/FIRSTBOOT ]]; then
		PID=$(pidof buildarttree)
		[[ -z ${PID} ]] && {
			/boot/art/buildarttree &>/dev/null &
		}
	fi
}


readCONFs ${ARG1}

writeToDownloadConf
checkART


exit 0







