#!/bin/bash

DAEMON_MAIN_D="$MEDIACASTER_DIR/etc/daemon.d"
DAEMON_MAIN_START_D="$DAEMON_MAIN_D/start.d"
DAEMON_MAIN_STOP_D="$DAEMON_MAIN_D/stop.d"
DAEMON_MAIN_EXE_ON_HOST_SH="$DAEMON_MAIN_D/EXE-ON-HOST.sh"

DAEMON_MANAGER="chroot ${MEDIACASTER_DIR} /manager"

daemon_main_start () {
	daemon_log_info "Starting Media Connect"

	# Mount media
	daemon_media_mount_start

    # Start the command daemon
	daemon_watchdog_start_process ${DAEMON_WATCHDOG_HOST} /sbin/host/init.d/kwilt-command

    # Run the daemon.d/start.d parts (target-specific)
    daemon_log_info "Running target-specific daemon start scripts"
    if [ "`ls ${DAEMON_MAIN_START_D}`" != "" ]; then
        for each in ${DAEMON_MAIN_START_D}/* ; do
		    daemon_log_debug "Running $each"
            sh ${each}
        done
    fi

    # Run the manager.d/start.d parts (target-specific)
    daemon_log_info "Running /manager start"
	${DAEMON_MANAGER} start
}

daemon_main_switch_full () {
	# Mount media
	daemon_media_mount_start

        #Instruct manager to go to full mode
        ${DAEMON_MANAGER} switch_full

	daemon_mode_set "$MODE_FULL"
}

daemon_main_switch_lite () {
        #Instruct manager to go to lite mode
        ${DAEMON_MANAGER} switch_lite

	# Unmount media
	daemon_media_mount_stop

	daemon_mode_set "$MODE_LITE"
}

daemon_main_stop () {
	daemon_log_info "Stopping Media Connect"

    # Run the manager.d/stop.d parts (target-specific)
    ${DAEMON_MANAGER} stop &
	wait

    # Run the daemon.d/stop.d parts (target-specific)
    if [ "`ls ${DAEMON_MAIN_STOP_D}`" != "" ]; then
        for each in ${DAEMON_MAIN_STOP_D}/* ; do
            sh ${each}
        done
    fi

    # Stop the command daemon
	daemon_watchdog_stop_process ${DAEMON_WATCHDOG_HOST} /sbin/host/init.d/kwilt-command

    # Unmount media
    daemon_media_mount_stop
}

daemon_main () {
	# Mount media, run daemon and manager start target-specific scripts
    daemon_main_start

	# Store current network state as the previous state.
	daemon_network_get_state "prev"

	# Main daemon loop
	while true
	do
		# TODO: Remove this
		#If you want to run a command from Chroot on the Host
		#put the commands in /etc/daemon.d/EXE-ON-HOST.sh
		#This file will be deleted after execution.
		if [ -f ${DAEMON_MAIN_EXE_ON_HOST_SH} ]; then
			sh ${DAEMON_MAIN_EXE_ON_HOST_SH}
			rm ${DAEMON_MAIN_EXE_ON_HOST_SH}
		fi

		# Check for and process mode changes.
		daemon_mode_compare_state
		if [ "$?" != "0" ]; then
			daemon_log_info "Mode has changed"
			if [ "$(daemon_mode_get)" == "$MODE_LITE" ]; then
				daemon_main_switch_full
			else
				daemon_main_switch_lite
			fi
		fi

		if [ "$(daemon_mode_get)" == "$MODE_FULL" ]; then
			# Check for and process mount changes.
			daemon_media_mount_compare_state
			if [ "$?" != "0" ]; then
				daemon_log_info "Mounts have changed"
				daemon_media_mount_state_changed
			fi

			# Check for and process network changes.
			# This runs in the host environment since we may need host networking apps/libs.
			daemon_network_compare_state
			if [ "$?" != "0" ]; then
				daemon_log_info "Network state has changed"
				daemon_network_state_changed
			fi

			# TODO: Move this to kwilt-index
			# Check if daemons are still running.
			daemon_network_check_connectivity > /dev/null
			if [ "$?" == "0" ] ; then
				daemon_watchdog_run
			fi
		fi

		# TODO: Remove this when R6220 firewall is tested with VPNs
		# Check if the VPN has come up
		#daemon_vpn_compare_state
		#if [ "$?" != "0" ]; then
		#	daemon_log_info "VPN state has changed"
		#	daemon_vpn_state_changed
		#fi

		# Tick the cron jobs
		daemon_cron_tick

		# Wait a little bit for changes to occur
		sleep 2
	done
}
