#! /bin/sh
### BEGIN INIT INFO
# Provides:          phidgetsbcwebif
# Required-Start:	 $local_fs
# Required-Stop:
# X-Start-Before:    $network
# Default-Start:     S
# Default-Stop:      
# Short-Description: webif init
# Description:       Applies webif configuration to
#					 system conffiles during boot.
### END INIT INFO

PATH=/sbin:/bin:/usr/bin:/usr/sbin
WEBIFCONFIGSCRIPT=/lib/webif/config.sh

# Exit if the package is not installed
[ -f "$WEBIFCONFIGSCRIPT" ] || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions
. /lib/webif/config.sh

#
# Make sure setting from all webif config files are applied to the system
# Also create any missing config files
#
do_start()
{
	log_action_begin_msg "Setting up system configuration"

	# Make sure config files exist

	# network.conf
	if [ ! -f $SBC_CONFIGDIR/network.conf ]; then
		log_action_cont_msg "creating network.conf"
		create_network_config
	fi
	# packages.conf
	if [ ! -f $SBC_CONFIGDIR/packages.conf ]; then
		log_action_cont_msg "creating packages.conf"
		create_packages_config
	fi
	# phidgetwebservice.conf
	if [ ! -f $SBC_CONFIGDIR/phidgetwebservice.conf ]; then
		log_action_cont_msg "creating phidgetwebservice.conf"
		create_webservice_config
	fi
	# system.conf
	if [ ! -f $SBC_CONFIGDIR/system.conf ]; then
		log_action_cont_msg "creating system.conf"
		create_system_config
	fi
	# webcam.conf
	if [ ! -f $SBC_CONFIGDIR/webcam.conf ]; then
		log_action_cont_msg "creating webcam.conf"
		create_webcam_config
	fi
	# wireless_network.conf
	if [ ! -f $SBC_CONFIGDIR/wireless_network.conf ]; then
		log_action_cont_msg "creating wireless_network.conf"
		create_wireless_network_config
	fi
	
	# Apply config files
	apply_network_config
	apply_webservice_config
	apply_system_config force
	apply_webcam_config
	apply_packages_config
	
	# Setup Avahi services
	setup_avahi_services
	
	log_action_end_msg $?
}

case "$1" in
  start|"")
	do_start
	;;
  restart|reload|force-reload)
	log_failure_msg "Error: argument '$1' not supported"
	exit 3
	;;
  stop)
	# No-op
	;;
  *)
	echo "Usage: phidgetsbcwebif {start|stop}" >&2
	exit 3
	;;
esac
