#!/bin/sh
### BEGIN INIT INFO
# Provides:          minidlna
# Required-Start:    $syslog $remote_fs $network
# Should-Start:      $time ypbind smtp
# Required-Stop:     $syslog $remote_fs $network
# Should-Stop:       ypbind smtp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Minidlna server
# Description:       Start minidlna
### END INIT INFO

S="minidlna"
P="/var/run/minidlna.pid"
S_BIN=/usr/sbin/minidlna
S_CONF=/etc/minidlna.conf
test -x "$S_BIN" || { echo "$S_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }
test -r "$S_CONF" || { echo "$S_CONF not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

DB_DIR=""
case "$1" in
    start|try-restart|condrestart|restart|force-reload)
        DB_DIR=$(perl -ne 'print $1,"\n" if /^db_dir=(.*)\s*$/' <"$S_CONF")
        test -n "$DB_DIR" || { echo "$S_CONF not configured: db_dir is not set"; 
            if [ "$1" = "stop" ]; then exit 0;
            else exit 6; fi; }
        test -d "$DB_DIR" || /usr/bin/install -d -o root -g root -m 0700 "$DB_DIR"
        ;;
esac

. /etc/rc.status
rc_reset

case "$1" in
    start)
	echo -n "Starting $S "
	"$S_BIN" -f "$S_CONF"
	rc_status -v
	;;
    stop)
	echo -n "Shutting down $S "
	/sbin/killproc "$S_BIN"
	rc_status -v
	;;
    try-restart|condrestart)
	## Do a restart only if the service was active before.
	## Note: try-restart is now part of LSB (as of 1.9).
	## RH has a similar command named condrestart.
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	# Remember status and be quiet
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service $S "
	$0 try-restart
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service $S "
	/sbin/checkproc "$S_BIN"
	rc_status -v
	;;
    probe)
	test "$S_CONF" -nt "$P" && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
