#!/bin/sh
# MediaCaster daemon

# Dependencies:
# 		/manager
# 		/etc/features.conf
# 		/etc/oe-admin.conf
# 		/etc/daemon.d/functions
# 		/etc/daemon.d/start.d/*
# 		/etc/daemon.d/stop.d/*
# 		/etc/daemon.d/start-on-change.d/*
# 		/etc/daemon.d/stop-on-change.d/*

# - To start services: /path/to/hipplay/daemon start
# - To stop services: /path/to/hipplay/daemon stop

# Calculate MEDIACASTER_DIR and pass it along to any child processes
DAEMON_NAME="$0"
MEDIACASTER_DIR="${DAEMON_NAME%/*}"
MEDIACASTER_DIR=`${MEDIACASTER_DIR}/bin/busybox readlink -f ${MEDIACASTER_DIR}`
export MEDIACASTER_DIR

# Convenient locations
isBuildroot=`cat ${MEDIACASTER_DIR}/etc/features.conf | grep "isBuildroot" | awk -F= '{ print $2 }'`
if [ "1" == "$isBuildroot" ]; then
	MEDIACASTER_BUSYBOX_SH="$MEDIACASTER_DIR/bin/busybox sh"
	DAEMON_INIT="$MEDIACASTER_DIR/sbin/daemon/daemon.sh"
	DAEMON="$MEDIACASTER_BUSYBOX_SH $DAEMON_INIT"
else
	DAEMON_INIT="$MEDIACASTER_DIR/sbin/daemon/daemon.sh"
	DAEMON="/bin/ash $DAEMON_INIT"
fi

case "$1" in
	start)
		${DAEMON} start
		;;
	status)
		${DAEMON} status
		;;
	stop)
		${DAEMON} stop
		;;
	event)
		${DAEMON} $@
		;;
	*)
		echo "USAGE: $0 {start|status|stop|event <args>}"
		exit 1
		;;
esac	

exit $?	
