#! /bin/sh

# chkconfig: 345 99 10
# 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>
# Edited by: David Gleich <wister.geo (at) yahoo.com>

## EDIT FROM HERE

# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network


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

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

# Where the PID & Lockfile lives
PID_FILE="/var/run/minidlna/minidlnad.pid"
LOCKFILE="/var/lock/subsys/minidlnad"
RETVAL=0

## STOP EDITING HERE

# 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 $ARGS -P $PID_FILE  >> $MINIDLNA_LOG 2>&1
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
        echo_success
        echo
}

stop () {
        echo -n "Stopping MiniDLNA: "
        RETVAL=$?
           for pidf in `/bin/ls $PID_FILE 2>/dev/null`; do
            if [ -s $pidf ]; then
                kill `cat $pidf` >/dev/null 2>&1 || :
                RETVAL=$?
                [ $RETVAL -eq 0 ] && echo_success
                [ $RETVAL -ne 0 ] && echo_failure
            fi
                        rm -rf $PIF_FILE
           [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
           done
        echo
}


# 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/minidlnad {start|stop|restart|reload|force-reload}"
        exit 1
        ;;
esac

exit $RETVAL
