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

# Source function library.
. /etc/rc.d/init.d/functions
#load webui functions
. /etc/profile.d/webuiprofile.sh

PRODUCT_NAME=bfw
# note INSTALLPENDINGFILE is used in adm_box script as well! changing the name here, requires a change in adm_box as well!
INSTALLPENDINGFILE="/home/${PRODUCT_NAME}/code/firmware/webui-install-init.pending"
UPDATEPENDINGFILE="/home/${PRODUCT_NAME}/code/firmware/webui-update-init.pending"
#SUPPORTED_MODELS="F18 F18A F80 F80A F180 F180A F280 F280B F380 F380A F400 F400B F400.F20 F400.F20B VF10 VF25 VF50 VF100 VF250 VF500 VF1000 VF2000 VF4000 VF8000 VM"
export PATH=/home/product/code/firmware/current/bin:$PATH
NG_WEBUI_SERVICES="mariadb nginx fcgi"
bfwstatus(){

	iswebui=$(/opt/phion/bin/boxinfo config /opt/phion/config/configroot/box.conf / ismanagedbywebui)
	if [ x"$iswebui" != x"y" ]; then
		return 3
	fi

	if [ -f ${INSTALLPENDINGFILE} ]; then
		return 2
	elif [ -f ${UPDATEPENDINGFILE} ]; then
		return 1
	else
		return 0
	fi
}


resetstatus(){
	/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - reset status"
	[ -f ${INSTALLPENDINGFILE} ] && rm -f ${INSTALLPENDINGFILE}
	[ -f ${UPDATEPENDINGFILE} ] && rm -f ${UPDATEPENDINGFILE}
}

stopwebuiservices(){
    for service in $NG_WEBUI_SERVICES; do
            if [ "`systemctl is-active $service`" == "active" ]; then
                systemctl stop $service
            fi
    done
}
startwebuiservices(){
    for service in $NG_WEBUI_SERVICES; do
            if [ "`systemctl is-active $service`" != "active" ]; then
                systemctl start $service
            fi
    done
    systemctl enable ng-webui
}



start() {
	_make_webui_env
	echo -ne "Starting configuration setup\n"
	/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Starting configuration setup"
	echo -ne "Cleaning up transactions\n"
	/bin/rm -f /mail/transactions/*
	echo -ne "Mounting tmpfs for transactions on '/phion0/mail/transactions'\n"
	if [ $(mountpoint /phion0/mail/transactions -q ; echo $?) -ne 0 -a -d /phion0/mail/transactions ]; then mount -t tmpfs -o defaults,size=200M tmpfs /phion0/mail/transactions/; fi

	# wait fo gconfd to be ready
	i=0
	while [ -f /home/${PRODUCT_NAME}/code/firmware/webui-config-init.pending ]; do
		sleep 1
		let i=$i+1
		[ $i -ge 45 ] && {
			# there is something wrong in the stat of denmark
			echo -ne "Fatal ${0}[$$]: Could not apply initial configuration!\n"
			/etc/phion/bin/phionlog_api box_System_webui Error "webuisetup - Fatal ${0}[$$]: Could not apply initial configuration"
			return
		}
	done

	bfwstatus
	case $? in
		4) echo -ne "Unsupported model detected. Nothing to be done.\n"
			/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Unsupported model detected. Nothing to be done"
			stopwebuiservices
			echo_passed
			echo
			exit 0
			;;
		3) echo -ne "This firewall isn't WebUI enabled. Disabling webui services.\n"
			/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - This firewall isn't WebUI enabled. Disabling webui services."
			stopwebuiservices
			echo_passed
			echo
			exit 0
			;;
		2) echo -ne "Preparing WebUI for first start\n"
			/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Preparing WebUI for first start [START]"
			# finish installation
			echo "starting mariadb"
                        if [ "`systemctl is-active mariadb`" != "active" ]
                        then
                                systemctl enable mariadb
                                systemctl start mariadb
                        fi

			/home/${PRODUCT_NAME}/code/firmware/commitNGInstallation >/root/commitinstallation.log 2>&1
			/home/${PRODUCT_NAME}/code/firmware/current/bin/set_login_machine_id
			echo "Initialization finished" >> /root/commitinstallation.log
			startwebuiservices
			echo
			/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Preparing WebUI for first start [END]"
			;;
		1) echo -ne "Preparing WebUI after update\n"
			/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Preparing WebUI after update [START]"
                        /etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Updating FW Host rules"
                        echo "y" | cp  /opt/phion/modules/box/boxsrv/boxfw/default.lfwrule7 /opt/phion/config/active/boxfw.lfwrule7
                        echo "y" | cp  /opt/phion/modules/box/boxsrv/boxfw/default.lfwrule7 /opt/phion/config/configroot/boxsrv/boxfw.lfwrule7
                        /opt/phion/bin/acpfrule install
                        /etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - FW Host rules updated"

			# finish update
			echo "starting mariadb"
                        if [ "`systemctl is-active mariadb`" != "active" ]
                        then
                                systemctl enable mariadb
                                systemctl start mariadb
                        fi
			/opt/phion/bin/create_database.sh
			mysql -u root -e "use config; delete from config where variable like '%__md5';"
			/opt/phion/bin/webuicmd db2conf revsql 2> /dev/null | mysql -t config
			/home/${PRODUCT_NAME}/code/firmware/current/bin/set_login_machine_id
			/home/${PRODUCT_NAME}/code/firmware/current/bin/migration.pl
			/opt/phion/bin/webuicmd db2conf deltaactivate - - - force
			echo "activating boxnet"
			if [ -f /opt/phion/preserve/boxnet.conf ]; then
				/opt/phion/bin/webuicmd activateboxnet
			fi
			echo "starting webui services"
			echo
    		        startwebuiservices
			/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Preparing WebUI after update [END]"
			;;
		0)
			echo "Starting webui"
	         	startwebuiservices
			;;
		*)
			;;
	esac

	resetstatus

	echo -n "Starting WebUI"
	/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Starting WebUI"
	echo
}

stop() {
	echo -n "Stopping WebUI"
        stopwebuiservices
	/etc/phion/bin/phionlog_api box_System_webui Info "webuisetup - Stopping WebUI"
	echo
}

case "$1" in 
        start)  
			start   
			;;      
        stop)   
			stop    
			;;      
	restart)
			stop
			start
			;;
        status) 
			bfwstatus
			case $? in
				4) echo "It's a Control Center. Nothing to be done." ;;
				3) echo "It's not WebUI enabled. Nothing to be done." ;;
				2) echo "Installation initialisation pending" ;;
				1) echo "Update initialisation pending" ;;
				*) echo "All initialisation done." ;;
			esac
			exit 0
			;;      
		*)
			echo "Unknown parameter $1"
			exit -1
			;;
esac


