#! /bin/sh
### BEGIN INIT INFO
# Provides:          mythtv-backend
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs
# Default-Start:     50
# Default-Stop:      S
# Short-Description: Start/Stop the MythTV server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/mythbackend
NAME="mythbackend"
DESC="MythTV server"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

set -e

prime_firewire()
{
    if [ "$ENABLE_FIREWIRE" = "TRUE" ]; then
	log_daemon_msg "Priming Firewire "
        su - $USER -c "/usr/bin/mythprime"
	log_end_msg $?
    fi
}

USER=mythtv
USER_HOME=$(grep ^$USER /etc/passwd | awk -F : '{print $6}')
RUNDIR=/var/run/mythtv
ARGS="--daemon --logfile /var/log/mythtv/mythbackend.log --pidfile $RUNDIR/$NAME.pid"
EXTRA_ARGS=""
NICE=0

if [ -f /etc/default/mythtv-backend ]; then
  . /etc/default/mythtv-backend
fi

ARGS="$ARGS $EXTRA_ARGS"

mkdir -p $RUNDIR
chown -R $USER $RUNDIR

unset DISPLAY
unset SESSION_MANAGER

#create a symbolic link for mysql.txt so it can't be overwritten
mkdir -p $USER_HOME/.mythtv
chown -R $USER $USER_HOME/.mythtv
if [ ! -e $USER_HOME/.mythtv/mysql.txt ]; then
        ln -s /etc/mythtv/mysql.txt $USER_HOME/.mythtv/mysql.txt
fi

case "$1" in
  start)
	if [ -e $RUNDIR/$NAME.pid ]; then
		PIDDIR=/proc/$(cat $RUNDIR/$NAME.pid)
		if [ -d ${RUNDIR} -a "$(readlink -f ${RUNDIR}/exe)" = "${DAEMON}" ]; then
			log_success_msg "$DESC already started; use restart instead."
			exit 1
		else
			log_success_msg "Removing stale PID file $RUNDIR/$NAME"
			rm -f $RUNDIR/$NAME.pid
		fi
	fi
	prime_firewire
	log_daemon_msg "Starting $DESC: $NAME "
	start-stop-daemon --start --pidfile $RUNDIR/$NAME.pid \
		--chuid $USER --nicelevel $NICE --exec $DAEMON -- $ARGS
	log_end_msg $?
	;;
  stop)
	log_daemon_msg "Stopping $DESC: $NAME "
	start-stop-daemon --stop --oknodo --pidfile $RUNDIR/$NAME.pid \
		--chuid $USER --exec $DAEMON -- $ARGS
	log_end_msg $?
	test -e $RUNDIR/$NAME.pid && rm $RUNDIR/$NAME.pid
	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC: $NAME "
	start-stop-daemon --stop --oknodo --pidfile $RUNDIR/$NAME.pid \
                --chuid $USER --exec $DAEMON -- $ARGS
	sleep 3
	prime_firewire
	start-stop-daemon --start --pidfile $RUNDIR/$NAME.pid \
                --chuid $USER --nicelevel $NICE --exec $DAEMON -- $ARGS
    log_end_msg $?
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 2
	;;
esac

exit 0
