#!/bin/bash
#
# VERSION=3
# CHANGES="change back SBCFS to AppFS"

# global variables #
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local/bin:/usr/local/sbin:/usr/fallback"

BEROCONF=/usr/fallback/beroconf

APPS_HOME=/home/admin
APPS_ROOT=/apps

###### clean new userapp after updated to new rootfs
#CLEAN_NEWUSERAPP=$($BEROCONF get root clean_oldrootfs | grep -v failed)
#if [ "$CLEAN_NEWUSERAPP" == "1" ]; then
#	mount -o remount,rw /home/admin
#	rm -rf /home/admin/*
#	mount -o remount,ro /home/admin
#	$BEROCONF delete root clean_oldrootfs
#fi

##### get APPS version
if [ -f ${APPS_HOME}/VERSION ]; then
	APPS_VERS=$(expr match "$(cat ${APPS_HOME}/VERSION)" ".*ERSION=\([0-9]*\)")
else
	APPS_VERS=1
fi

# functions #
function log {
	prefix="[preinit_userapp]"

	if [ -z "${2}" ]; then
		echo "${prefix} ${1}"
	else
		echo "${prefix} ${1}" >> ${2}
	fi
}

function set_userapp_status {

	if [ -z "${1}" ]; then
		1="unknown"
	fi

	if [ -z "${2}" ]; then
		2="unknown"
	fi

	${BEROCONF} set root userapp-status ${1} &> /dev/null
	${BEROCONF} set root userapp-image ${2} &> /dev/null
}

function create_etc_var {

	if [ ! -d /usr/conf/userapp ]; then
		mkdir -p /usr/conf/userapp
	fi

	if [ ! -d /tmp/userapp/log ]; then
		mkdir -p /tmp/userapp/log
	fi

	if [ ! -d /tmp/userapp/run ]; then
		mkdir -p /tmp/userapp/run
	fi

	if [ ! -d /tmp/userapp/spool ]; then
		mkdir -p /tmp/userapp/spool
	fi

	chown -R admin:admin /usr/conf/userapp
	chown -R admin:admin /tmp/userapp

	# make sure cron-dir is writeable for admin too!
	cron_dir=/usr/conf/cron
	cron_tab=${cron_dir}/admin
	chmod 0777 ${cron_dir}

	# make sure admin's crontab has the right permissions
	if [ ! -f ${cron_tab} ]; then
		touch ${cron_tab}
	fi
	chown root:admin ${cron_tab}
	chmod 0664 ${cron_tab}
}

function apps_script_exec {

	script=${1}
	mode=${2}

	${script} ${mode}
}

function apps_start {

	for app in /apps/??*; do

		if [ ! -d ${app}/preinit ]; then
			continue
		fi

		for script in ${app}/preinit/S??*; do
			[ ! -x "${script}" ] && continue
			log "Starting ${script}."
			apps_script_exec ${script} start
		done
	done
}

function apps_stop {

	for app in /apps/??*; do
		declare -a services

		for script in ${app}/preinit/S??*; do
			if [ ! -d ${app}/preinit ]; then
				continue
			fi

			[ ! -x "${script}" ] && continue

			services[${#services[@]}]=${script}
		done

		num=${#services[@]}
		let num="${num}-1"

		for (( i = num ; i >= 0 ; i-- )) ; do
			log "Stopping ${services[${i}]}..."
			apps_script_exec ${services[${i}]} stop
		done
		unset services
	done
}

function apps_apply {

	for app in /apps/??*; do
		for script in ${app}/init/S??*; do
			[ ! -x "${script}" ] && continue
			apps_script_exec ${script} apply
		done
	done
}

# main #
BOOT_UPDATE=$(${BEROCONF} get root boot_fwupdate | grep -v failed)
if [ "${BOOT_UPDATE}" = "1" ]; then
	log "Not starting AppFS-services cause of Firmware-Update-Boot."
	exit 0
fi

# check if userapp directory exists
if [ -z "${APPS_VERS}" ]; then
	log "UserApp-infrastructure not installed, leaving."
	set_userapp_status "not installed" "unknown"
	exit 0
fi

# create /apps-symlink if it does not exist
if [ ! -L /apps ] && [ -d ${APPS_HOME}/apps ]; then
	mount -o remount,rw /
	ln -s ${APPS_HOME}/apps /apps
	mount -o remount,ro /
fi

userapp_enabled=$(${BEROCONF} get root userapp-enabled | grep -v failed)
case "${1}" in
	start)
		if [ "${userapp_enabled}" != "1" ]; then
			log "Services disabled in root.db, leaving."
			set_userapp_status "disabled" "${APPS_VERS}"
			exit 0
		fi

		create_etc_var

		log "Starting Services."
		apps_start
		set_userapp_status "running" "${APPS_VERS}"
		;;
	stop)
		log "Stopping Services."
		apps_stop
		set_userapp_status "stopped" "${APPS_VERS}"
		;;
	apply)
		log "Applying settings."
		apps_apply
		;;
	restart)
		${0} stop
		${0} start
		;;
	*)
		echo "Usage: ${0} {start|stop|apply|restart}" >&2
		exit 1
		;;
esac
