#!/bin/sh
#
# ld10k1	Startup script for the ALSA emu10k1/2 patch loader
#
# Copyright (C) 2005 Mikael Magnusson <mikma@users.sourceforge.net>
#

# Don't use set -e.  Check return status instead.

NAME=ld10k1
MYNAME=/etc/init.d/$NAME
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DAEMON=/usr/sbin/ld10k1
LO10K1=/usr/bin/lo10k1
DESC="emu10k1/2 patch loader"
STATE_FILE=/var/lib/alsa/ld10k1.state
RUN_DIR=/var/run/ld10k1
PIPE_NAME=${RUN_DIR}/ld10k1.socket
PID_FILE=${RUN_DIR}/ld10k1.pid
LOG_FILE=/var/log/ld10k1


. /lib/lsb/init-functions
print_warning_msg() { log_warning_msg "$1" ; }
print_error_msg() { log_warning_msg "$1" ; }
print_action_msg() { log_begin_msg "${1}..." ; }
print_daemon_msg() { log_daemon_msg "$1" $2 ; }
print_progress_msg() { log_progress_msg "$1" ; }
print_completion_msg_and_exit() { [ "$1" = "0" ] && log_progress_msg "done" ; log_end_msg "$1" ; exit $1 ; }
print_end_msg_and_exit() { log_end_msg "$1" ; exit $1 ; }

# Default overrides
if [ -r /etc/default/ld10k1 ] ; then
	. /etc/default/ld10k1 || { print_error_msg "${MYNAME}: Error: Failed reading /etc/default/ld10k1" ; exit 1 ; }
fi

[ "$CARD" ] || exit 0
[ -x "$DAEMON" ] || exit 0
[ -x "$LO10K1" ] || exit 0

check_run_dir()
{
	[ -d "$RUN_DIR" ] && return 0
	mkdir "$RUN_DIR" || return 1
	[ -d "$RUN_DIR" ] || return 1
	chgrp audio "$RUN_DIR" || return 1
	chmod 750 "$RUN_DIR" || return 1
	return 0
}

daemon_is_running()
{
	# Return
	#   0 if daemon is running
	#   1 if daemon is not running
	start-stop-daemon --start --quiet --pidfile "$PID_FILE" --exec "$DAEMON" --test > /dev/null \
		|| return 0
	return 1
}

start_daemon()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	daemon_is_running && return 1
	start-stop-daemon --start --quiet --pidfile "$PID_FILE" --exec "$DAEMON" -- \
		--pipe_name "$PIPE_NAME" \
		--daemon "$DAEMON_OPTS" \
		--pidfile "$PID_FILE" \
		--logfile "$LOG_FILE" \
		--card "$CARD" \
		|| return 2
}

stop_daemon()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	daemon_is_running && start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 --pidfile "$PID_FILE" --name "$NAME"
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too
	start-stop-daemon --stop --quiet --oknodo --retry=0/10/KILL/5 --exec "$DAEMON"
	[ "$?" = 2 ] && return 2
	rm -f "$PID_FILE"
	return "$RETVAL"
}

restore_state()
{
	$LO10K1 --restore "$STATE_FILE" >/dev/null 2>&1
}

store_state()
{
	$LO10K1 --store "$STATE_FILE" >/dev/null 2>&1
}

case "$1" in
  start)
	print_daemon_msg "Starting $DESC"
	check_run_dir || print_end_msg_and_exit 1 "" "(failed creating run directory)"
	print_progress_msg "$NAME"
	start_daemon
	case "$?" in
	  0) : ;;
	  1) print_progress_msg "(already running)" ; print_end_msg_and_exit 0 ;;
	  *) print_end_msg_and_exit 1 ;;
	esac
	[ -r "$STATE_FILE" ] || { print_progress_msg "(no state to restore)" ; print_end_msg_and_exit 0 ; }
	if restore_state ; then
		print_progress_msg "(state restored)"
		print_end_msg_and_exit 0
	else
		print_progress_msg "(state restore failed)"
		print_end_msg_and_exit 1
	fi
	;;
  stop)
	print_daemon_msg "Stopping $DESC"
	if daemon_is_running; then
		if store_state; then
			print_progress_msg "(state stored)"
		else
			print_progress_msg "(state store failed)"
		fi
	fi
	print_progress_msg "$NAME"
	stop_daemon
	case "$?" in
	  0) print_end_msg_and_exit 0 ;;
	  1) print_progress_msg "(not running)" ; print_end_msg_and_exit 0 ;;
	  *) print_end_msg_and_exit 1 ;;
	esac
	;;
  restart)
	print_daemon_msg "Restarting $DESC"
	print_progress_msg "$NAME"
	stop_daemon
	case "$?" in
	  0) : ;;
	  1) print_progress_msg "(not running)" ;;
	  *) print_progress_msg "(failed on stop)" ;;
	esac
	start_daemon
	case "$?" in
	  0) : ;;
	  1) print_progress_msg "(old process is still running)" ; print_end_msg_and_exit 1 ;;
	  *) print_progress_msg "(failed to start)" ; print_end_msg_and_exit 1 ;;
	esac
	[ -r "$STATE_FILE" ] || { print_progress_msg "(no state to restore)" ; print_end_msg_and_exit 0 ; }
	if restore_state ; then
		print_progress_msg "(state restored)"
		print_end_msg_and_exit 0
	else
		print_progress_msg "(state restore failed)"
		print_end_msg_and_exit 1
	fi
	;;
  reload|force-reload)
	print_action_msg "Reloading $DESC state"
	daemon_is_running || { print_progress_msg "(not running)" ; print_completion_msg_and_exit 0 ; }
	[ -r "$STATE_FILE" ] || { print_progress_msg "(no state to restore)" ; print_completion_msg_and_exit 0 ; }
	restore_state
	print_completion_msg_and_exit "$?"
	;;
  store)
	print_action_msg "Storing $DESC state"
	daemon_is_running || { print_progress_msg "(not running)" ; print_completion_msg_and_exit 0 ; }
	store_state
	print_completion_msg_and_exit "$?"
	;;
  *)
	print_warning_msg "Usage: $MYNAME {start|stop|restart|reload|force-reload|store}"
	exit 3
	;;
esac

exit 0
