#!/bin/bash
#
# VERSION=11
# CHANGES="downgrade the PKG_VERSION value from the VERSION.* files in recovery mode"

# 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

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

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

function get_need_prod {

	np_info=$(${BEROCONF} get root need_prod)

	echo ${np_info} | grep "failed" > /dev/null
	if [ ${?} -eq 0 ]; then
		echo "0"
	else
		echo "${np_info}"
	fi

}

function set_app_status {
	${BEROCONF} set root app-status ${1}
	${BEROCONF} set root app-image ${2}
}

function check_app_integrity () {

	# if appfs is empty its empty
	if [ -z "$(/bin/ls /usr/local)" ]; then
		echo 1 > /tmp/enable_recovery
		return
	fi

	# since rootfs updated during an downgrade. if the file is missing, it's an issue
	# booting in recovery mode
	if [ ! -f /usr/local/integrity.file_list ]; then
		echo 1 > /tmp/enable_recovery
		return
	fi

	# otherwise we check if the files listed are actually there
	for item in $(cat /usr/local/integrity.file_list); do
		if [ ! -f /usr/local/${item} ]; then
			echo "/usr/local/${item} is missing!"
			echo 1 > /tmp/enable_recovery
		fi
	done
}

function downgrade_pkginfo () {
	/bin/mount -o remount,rw /
	for file in $(ls /pkginfo/VERSION.*); do
		/bin/sed -i 's/PKG_VERSION=.*/PKG_VERSION=1/g' $file
	done
	/bin/mount -o remount,ro /
}

# main #
if [ "$(${BEROCONF} get root boot_recoverymode | grep -v failed)" = "1" ]; then
	echo 4 > /sys/class/beronet/gateway/led_program
	exit 0
fi

BOOT_UPDATE=$(${BEROCONF} get root boot_fwupdate | grep -v failed)
if [ "${BOOT_UPDATE}" = "1" ]; then
	echo 2 > /sys/class/beronet/gateway/led_program
	exit 0
fi

APP_IMG_VERS=$(cat /usr/local/FILENAME)
NEED_PROD=$(get_need_prod)

case "${1}" in
	start)
		check_app_integrity
		if [ -f /tmp/enable_recovery ]; then
			log "Integrity of AppFS is compromised, enabling Recovery-Mode."
			downgrade_pkginfo
			echo 4 > /sys/class/beronet/gateway/led_program
			exit 0
		fi

		if [ -f /usr/local/FILENAME -a -x /usr/local/init/rcS ] && [ ${NEED_PROD} -eq 0 ]; then
			log "Starting AppFS-Services."
			set_app_status "running" "${APP_IMG_VERS}"
			/usr/local/init/rcS
		else
			log "Starting AppFS-Services disabled. (no app-image installed)"
			if [ -f /usr/local/modules/berofix.ko ] && [ ${NEED_PROD} -eq 0 ]; then
				insmod /usr/local/modules/berofix.ko
			fi
			set_app_status "stopped" ""
		fi
		## apiusers.conf does not exit. we generate it
		if [ ! -f /usr/conf/apiusers.conf ]; then
			/usr/bin/env -i bash -c "/usr/bin/php-cgi /usr/local/php/apiusers.php admin >/dev/null"
		fi
		;;
	stop)
		if [ -f /usr/local/FILENAME -a -x /usr/local/init/rcK ] && [ ${NEED_PROD} -eq 0 ]; then
			log "Stopping AppFS-Services."
			set_app_status "stopped" "${APP_IMG_VERS}"
			/usr/local/init/rcK
		else
			log "Stopping AppFS-Services disabled. (no app-image installed)"
			if [ -f /usr/local/modules/berofix.ko ] && [ ${NEED_PROD} -eq 0 ]; then
				rmmod berofix
			fi
			set_app_status "stopped" ""
		fi
		;;
	*)
		echo "Usage: ${0} [start|stop]" >&2
		exit 1
		;;
esac
