#!/bin/bash
# VERSION=15
# CHANGES=set right permissions for /usr/conf/cron/admin

if [ -x /usr/local/bin/beroconf ]; then
	BEROCONF=/usr/local/bin/beroconf
else
	BEROCONF=/usr/fallback/beroconf
fi

if [ -f  /home/admin/FILENAME ] ; then
    USERAPP_VERS=`cat /home/admin/FILENAME`
else
    USERAPP_VERS=none
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 configure_user {

	user_locked=0

	if [ ! -z $(grep admin /etc/shadow | grep "\!") ]; then
		user_locked=1
	fi

	if [ "`${BEROCONF} get root userapp-user-enabled | grep -v failed`" = "1" ]; then
		if [ ${user_locked} -eq 1 ]; then
			mount -oremount,rw /
			/usr/bin/passwd -u admin
			mount -oremount,ro /
		fi
	else
		if [ ${user_locked} -eq 0 ]; then
			mount -oremount,rw /
			/usr/bin/passwd -l admin
			mount -oremount,ro /
		fi
	fi
}

if [ -z "${USERAPP_VERS}" ]; then
	echo "No User-App installed, leaving."
	set_userapp_status "not installed" "unknown"
	exit 0
fi

start_apps() {
    APPHOME=/home/admin/apps
    if [ -d /home/admin/apps ] ; then
        for i in $(ls $APPHOME) ; do
            if [ -f $APPHOME/$i/VERSION ] ; then
                . $APPHOME/$i/VERSION
                echo "starting \"$NAME\""

		if [ -x $APPHOME/$i/init/rcS ]; then
			echo "Starting User-App-Services..."
			su admin -c $APPHOME/$i/init/rcS
		fi
            fi
        done
    fi
}

stop_apps() {
    APPHOME=/home/admin/apps
    if [ -d /home/admin/apps ] ; then
        for i in $(ls $APPHOME) ; do
            if [ -f $APPHOME/$i/VERSION ] ; then
                . $APPHOME/$i/VERSION
                echo "stopping \"$NAME\""

		if [ -x $APPHOME/$i/init/rcK ]; then
			echo "Stopping User-App-Services..."
			su admin -c $APPHOME/$i/init/rcK
		fi
            fi
        done
    fi
}

if [ ! -L /apps ] && [ -d /home/admin/apps ]; then
	mount -o remount,rw /
	ln -s /home/admin/apps /apps
	mount -o remount,ro /
fi

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

		configure_user
		create_etc_var

		if [ -x /home/admin/init/rcS ]; then
			echo "Starting User-App-Services..."
			create_etc_var
			set_userapp_status "running" "${USERAPP_VERS}"
			su admin -c /home/admin/init/rcS
		fi

                start_apps
		;;
	stop)
		if [ -x /home/admin/init/rcK ]; then
			echo "Stopping User-App-Services..."
			set_userapp_status "stopped" "${USERAPP_VERS}"
			su admin -c /home/admin/init/rcK
		fi

               stop_apps
		;;
	restart)
		${0} stop
		${0} start
		;;
	update-user)
		echo -n "Configuring user-account: "
		configure_user
		echo -en "\n"
		;;
	*)
		echo "Usage: ${0} [start|stop|restart|update-user]" >& 2
		exit 1
		;;
esac
