#!/bin/sh

if [ "" == "$MEDIACASTER_DIR" ]; then
    echo "This script must be called from daemon wrapper.  Exiting."
    exit 1
fi

# Directories and files
HOST_DAEMON_LIB_D="${MEDIACASTER_DIR}/sbin/daemon"
HOST_OE_ADMIN_CONF="${MEDIACASTER_DIR}/etc/oe-admin.conf"
HOST_FEATURES_CONF="${MEDIACASTER_DIR}/etc/features.conf"


#-----------#
# Libraries #
#-----------#

# Import each of the daemon library files
LIBS="log.sh main.sh host-mount.sh bind-mount.sh cron.sh media-mount.sh network.sh utils.sh vpn.sh watchdog.sh mode.sh"
for LIB in ${LIBS}; do
	source "${HOST_DAEMON_LIB_D}/${LIB}"
done

#-----------#
# Functions #
#-----------#

# Start the MediaCaster daemon
daemon_start () {
	daemon_status > /dev/null
	STATUS=$?
	if [ "$STATUS" == "0" ]; then
		echo "MediaCaster daemon is already running."
		return 1
	elif [ "$STATUS" == "2" ]; then
		echo "MediaCaster daemon is stopping or dead."
		return 1
	fi

	echo "Starting MediaCaster daemon: "

	# Mount some host directories into the guest chroot tree
	daemon_host_mount_start

	# Mount the guest writable directories
	daemon_bind_mount_start

	# Run daemon_main as a background process
	daemon_main &

	# Save daemon_main() PID so we can kill it later
	echo $! > "$HOST_PIDFILE"

	echo "OK"
	return 0
}

# Query the MediaCaster daemon status
daemon_status () {
	if [ ! -f "$HOST_PIDFILE" ]; then
		echo "MediaCaster is stopped."
		return 1
	fi

	MEDIACASTER_PID=`cat ${HOST_PIDFILE}`

	# Verify mediacaster hasn't crashed
	if ! kill -0 ${MEDIACASTER_PID}; then
		echo "MediaCaster is stopped, but PID file exists: PID [${MEDIACASTER_PID}] : $HOST_PIDFILE"
		return 2
	fi

	echo "Mediacaster is running."
	return 0
}

# Stop the MediaCaster daemon
daemon_stop () {
	daemon_status > /dev/null
	STATUS=$?
	if [ "$STATUS" != "0" ]; then
		echo "MediaCaster daemon is not running."
		return 1
	fi

	# Kill the main daemon loop
	echo "Stopping MediaCaster daemon:"
	local MEDIACASTER_PID=`cat ${HOST_PIDFILE}`

	if ! kill -9 ${MEDIACASTER_PID}; then
		echo "Failed"
		return 1
	fi

	# Run the manager and daemon stop scripts, and unmount the media
	daemon_main_stop

    # Unmount the guest writable directories
    daemon_bind_mount_stop

    # Unmount the host directories from the guest chroot tree
    daemon_host_mount_stop

	# Remove the pid file
	rm -f ${HOST_PIDFILE}

	echo "OK"
	return 0
}


#---------------#
# Configuration #
#---------------#

# Defaults for features.conf
ENABLE_DEBUG=1
ENABLE_TRACE=0
MOUNT_HOST_TMP=1
MOUNT_HOST_MNT=0
ENABLE_TELNET=0

# features.conf
source "${HOST_FEATURES_CONF}"

# mediacaster busybox applets path
if [ "1" == "$isBuildroot" ]; then
	# DO NOT EXPORT THIS PATH
	PATH="${MEDIACASTER_DIR}/sbin:${MEDIACASTER_DIR}/bin:${MEDIACASTER_DIR}/usr/sbin:${MEDIACASTER_DIR}/usr/bin:$PATH"
	daemon_log_debug "Enabled Buildroot PATH."
fi

# debug tracing
if [ "1" == "$ENABLE_TRACE" ]; then
	daemon_log_debug "Enabling tracing."
	set -x
fi

# pid file
if [ "1" == "$isBuildroot" ]; then
	HOST_PIDFILE="/tmp/mediacaster.pid"
else
	HOST_PIDFILE="/var/run/mediacaster/mediacaster.pid"
fi

#------------------#
# Command Dispatch #
#------------------#

case "$1" in
	start)
		daemon_start
		;;
	status)
		daemon_status
		;;
	stop)
		daemon_stop
		;;
    restart)
    	daemon_stop
    	daemon_start
    	;;
	event)
		# Drop the leading 'event' argument
		shift
		case "$1" in
			vpn_up)
				shift
				daemon_vpn_up $@
				;;
			vpn_down)
				shift
				daemon_vpn_down $@
				;;
			bound_status)
				shift
				daemon_mode_set_binding_count $@
				;;
			kwilt_ready)
				shift
				daemon_mode_set_kwilt_ready $@
				;;
			*)
				echo "USAGE: daemon event <name> <args>"
				exit 1;
				;;
		esac
		;;
	*)
		echo "USAGE: daemon {start|stop|restart|status|event <name> <args>}"
		exit 1
		;;
esac

exit $?
