#! /bin/sh

# chkconfig: 345 98 16
# description: Fedora Startup/shutdown script for MiniDLNA daemon

# If you have chkconfig, simply:
# chkconfig --add minildna

# Proper init scripts on Linux systems normally require setting lock
# and pid files under /var/run as well as reacting to network
# settings, so you should treat this with care.

# Original author:  Perry Clark <omfgppc (at) gmail.com>
# modified by IWAI, Masaharu <iwaim.sub@gmail.com>

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/minidlna ]; then
        . /etc/sysconfig/minidlna
fi

RETVAL=0
LOCK_FILE=/var/lock/subsys/minidlna

# Installation details
MINIDLNA="/usr/sbin/minidlnad"
CONF="/etc/minidlna/minidlna.conf"

# Where to keep a log file
MINIDLNA_LOG="/var/log/minidlna/minidlna.log"

# Where the PID lives
PID_FILE="/var/run/minidlnad.pid"

# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

set -e

# Only start if we can find the minidlna.conf.
test -x $MINIDLNA || exit 0

start() {
    echo -n "Starting MiniDLNA: "
    $MINIDLNA -f $CONF -P $PID_FILE  >> $MINIDLNA_LOG 2>&1
    RETVAL=$?
    [ $RETVAL -eq 0 ] && success || failure
    echo
    [ $RETVAL -eq 0 ] && touch $LOCK_FILE
    return $RETVAL
}

stop() {
    pid=0
    if [ -f $PID_FILE ]; then
        pid=`cat $PID_FILE`
        echo -n "Stopping MiniDLNA: "
        kill $pid
        RETVAL=$?
        [ $RETVAL -eq 0 ] && success || failure
        echo
        [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
        return $RETVAL
    fi
}

# Parse command line parameters.
case $1 in
  start)
        start
	;;
  stop)
        stop
        ;;

  restart|reload|force-reload)
	echo "Restarting MiniDLNA: "
	
	stop
	sleep 2
	start
	
	;;

  *)
	# Print help
	echo "Usage: /etc/init.d/minidlna {start|stop|restart|reload|force-reload}"
	exit 1
	;;
esac

exit $?
