#!/bin/bash
#
# srcpd  This shell script takes care of starting and stopping
#        srpcd on RedHat 5,6 or other chkconfig-based system.
#
# chkconfig: 2345 80 30
# processname: srcpd
# config: /etc/srcpd.conf
# pidfile: /var/run/srcpd.pid
#
# description: The srcpd is a gateway between any kind of model railway \
#              systems and user interface programs that support the Simple \
#              Railroad Command Protocol (SRCP)

### BEGIN INIT INFO
# Provides: srcpd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop srcpd
# Description: The srcpd is a gateway between any kind of model railway \
#              systems and user interface programs that support the Simple \
#              Railroad Command Protocol (SRCP)
### END INIT INFO

# Written by Denis Fateyev (denis@fateyev.com)
# 2014.02.08

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

# Source networking configuration
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

start() {
	[ "$EUID" != "0" ] && exit 4
	[ "${NETWORKING}" = "no" ] && exit 1
	[ -f /usr/sbin/srcpd ] || exit 5

        # Start daemon
        echo -n $"Starting srcpd: "
        /usr/sbin/srcpd && success || failure
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/srcpd
}

stop() {
	[ "$EUID" != "0" ] && exit 4

        # Stop daemon
        echo -n $"Shutting down srcpd: "
        if [ -n "`pidfileofproc srcpd`" ] ; then
	    killproc /usr/sbin/srcpd
	else
	    failure $"Shutting down srcpd"
	fi
	RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/srcpd
}

restart() {
	stop
	start
}

# See how we were called
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  condrestart|try-restart)
	status srcpd > /dev/null || exit 0
	restart
	;;
  status)
	status srcpd
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|status|condrestart|try-restart}"
	exit 2
esac

exit $RETVAL
