#!/bin/bash2

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

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

RUNDIR="/var/phion/run/messageboard"
CONFDIR="/var/phion/preserve/messageboard"

[[ ! -d ${RUNDIR} ]] && mkdir -p ${RUNDIR}
[[ ! -d ${CONfDIR} ]] && mkdir -p ${CONFDIR}



function fEXIT {
	$logerror "Info" "${ARG0}[$$]: END, -----------------------------------------------"
	[[ -f ${TMPMOTDFLE} ]] && rm -f ${TMPMOTDFLE} 
	[[ -f ${TMPROLLOUTFLE} ]] &&  MoveOrDelRolloutFile ${TMPROLLOUTFLE} 
	
}

umask 022
BOOT=0

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

PMODPATH="/opt/phion/modules/box/boxother/messageboard"
[ -f ${PMODPATH}/moddefs.conf ] && source ${PMODPATH}/moddefs.conf  
MYLOGFLE="box_Auth_activation"

logerror="LogError $MYLOGFLE"

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

export H=1

ARG0=activate
ARGB=activate
ARGS=ARGS

DOWNLOAD_CONF="/opt/phion/modules/download/producttips/download.conf"
MCDOWNLOAD_CONF="/var/phion/mcdownload/producttips/download.conf"


$logerror "Info" "${ARG0}[$$]: START, starting activation of message board configuration"
if [ $BOOT = 1 ]; then 
	$logerror "Info" "${ARG0}[$$]: ARGS=b,$ARG1"
else
	$logerror "Info" "${ARG0}[$$]: ARGS=$ARG1"
fi


if [ ! -f $ARG1 ]; then
	$logerror "Error" "${ARG0}[$$]: missing conf file $ARG1"
      	exit 1 
fi




# read in conf-file
TMPROLLOUTFLE=${RUNDIR}/messageboard.conf.rollout.$$.tmp
THEROLLOUTFLE=${CONFDIR}/messageboard.conf.rollout

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

	if [ -z "$box_ENABLEPRODUCTTIPS" ]; then
		$box_ENABLEPRODUCTTIPS="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_ENABLEPRODUCTTIPS}/" "$MCDOWNLOAD_CONF"
	else
		echo "ENABLEDONBOX = ${box_ENABLEPRODUCTTIPS}" >> "$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
}

function readCONFs
        {
	
	local ARG0="$(SUB1) readCONFs"
	local ARGS=""
        if [ ! -x "${PHION_ROLLOUT}.net" ]; then
                $logerror "Error" "${ARG0}[$$]: missing executable ${PHION_ROLLOUT}.net"
                return 1
        fi

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


        if [ -f "$ARG1" ]; then
                ${PHION_ROLLOUT}.net ${ARG1} ${TMPROLLOUTFLE} &>/dev/null
        fi
	
	[[ -f ${TMPROLLOUTFLE}  ]] && {
	
		if ! diff --brief ${TMPROLLOUTFLE}  ${THEROLLOUTFLE} &>/dev/null; then
		 	MoveOrDelRolloutFile ${TMPROLLOUTFLE} ${THEROLLOUTFLE}
		else
			MoveOrDelRolloutFile ${TMPROLLOUTFLE}
		fi
	}
}



trap fEXIT EXIT

readCONFs

#set defaults



[[ -f ${THEROLLOUTFLE} ]] && {
	source ${THEROLLOUTFLE}
}

writeToDownloadConf

SHOWGUI=${box_SHOWGUI:-y}
SHOWSYS=${box_SHOWSYS:-y}

DEMOMODE=1
DEMOEXPORT=0

[[ -x /opb/boxinfo ]] && {
	
	DEMOMODE=$(/opb/boxinfo demo_mode 2>/dev/null)
	
	[ ${DEMOMODE:-1} = 1 ] && {
		DEMOEXPORT=1
	}
}

DEMOMESSAGEFLE="/opt/phion/license/demotxt.txt"
if [[ ${DEMOEXPORT:-0} = 1 ]]; then
	$logerror "Notice" "${ARG0}[$$]: DEMO mode, all logins will display DEMO disclaimer"
fi





THEMOTDFLE="/etc/motd"
TMPMOTDFLE="${RUNDIR}/motd.$$.tmp"


echo -n >${TMPMOTDFLE}

if [[ ${DEMOEXPORT} = 1 ]]; then
	if [[ -s ${DEMOMESSAGEFLE} ]]; then
		cat ${DEMOMESSAGEFLE} >> ${TMPMOTDFLE} 
	else
		echo -e "THIS IS AN UNLICENSED DEMO VERSION\n
WITH PUBLICLY KNOWN ROOT PASSWORD ngf1r3wall\n
WHICH IS NOT SUITABLE FOR PRODUCTION USE." >>${TMPMOTDFLE}
	fi					       	
else

	if [ $SHOWSYS = y ]; then
		$logerror "Info" "${ARG0}[$$]: system logins display message board"
	else
		$logerror "Notice" "${ARG0}[$$]: system logins $(MARK DO NOT) display message board"
	fi

	if [ $SHOWGUI = y ]; then
		$logerror "Info" "${ARG0}[$$]: $BASENAMEADMIN logins display message board"
	else
		$logerror "Notice" "${ARG0}[$$]: $BASENAMEADMIN logins $(MARK DO NOT) display message board"
	fi

	if [ $SHOWSYS = y ]; then

		[[ -f ${THEROLLOUTFLE}.MOTD ]] &&   /bin/cat  ${THEROLLOUTFLE}.MOTD >>${TMPMOTDFLE}
		[[ -f ${THEROLLOUTFLE}.MOTDS ]] &&  /bin/cat  ${THEROLLOUTFLE}.MOTDS >>${TMPMOTDFLE}

		if [ -f /etc/motd.tmp ];  then 
			#/bin/cp -af /etc/motd /etc/motd.last
			/bin/mv -f /etc/motd.tmp /etc/motd		
		else
			#/bin/cp -af /etc/motd /etc/motd.last
			echo -n >/etc/motd	
		fi
	else
		echo -n >/etc/motd	
	fi
fi

iconv -f ISO8859-1 < ${TMPMOTDFLE} > ${TMPMOTDFLE}.2
mv ${TMPMOTDFLE}.2 ${TMPMOTDFLE}
MoveOrDelOnDiff ${TMPMOTDFLE}  ${THEMOTDFLE} 

exit 0

