#! /bin/sh
# $Header: /usr/groups/ecad/altera/init.d_jtagd/jtagd,v 1.8 2010/01/26 15:33:19 swm11 Exp $
##############################################################################
# Debian/Ubuntu header
##############################################################################
### BEGIN INIT INFO
# Provides:          jtagd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Altera jtagd
# Description:       /etc/init.d script to start and stop the Altera jtagd
#                    server
### END INIT INFO
# Author: Simon Moore

##############################################################################
# Readhat/Fedora/CentOS header
##############################################################################
# Init file for Altera JTAGD
#
# chkconfig: 2345 60 25
# description: JTAG daemon
#
# processname: jtagd
# config: /etc/jtagd

##############################################################################
# Notes
##############################################################################
# Install in /etc/init.d/jtagd
# Enable using:
#   on Redhat:
#      "/sbin/chkconfig jtagd on" or "cl-asuser chkconfig jtagd on"
#   on Ubuntu/Debian:
#      "/usr/sbin/update-rc.d jtagd defaults"
# It can then be started (avoiding a reboot) using:
#    "cl-asuser service jtagd start".

FLAGS="defaults 21"

DESC="Altera JTAGD service"
NAME=jtagd
# SWM: specified location of jtagd
JTAGD=/usr/groups/ecad/altera/current/quartus/bin/jtagd
ALTERA_DIR=/usr/groups/ecad/altera
QUARTUS_ROOTDIR=$ALTERA_DIR/current/quartus
DAEMON=$QUARTUS_ROOTDIR/bin/$NAME
DAEMON_ARGS="--config $ALTERA_DIR/jtagd_config"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
RETVAL=0
ARG=$1

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

#
# Determine which kind of configuration we're using
#
system=unknown
if [ -f /etc/debian_version ]; then
        system=debian
elif [ -f /etc/redhat-release ]; then
        system=redhat
else
        echo "$0: Unknown system - Exiting" 1>&2
        exit 1
fi

# Read configuration variable file if it is present
# [ -r /etc/default/$NAME ] && . /etc/default/$NAME

if [ "$system" = "redhat" ]; then
    # Redhat initd functions
    . /etc/rc.d/init.d/functions
else
    # Load the VERBOSE setting and other rcS variables
    . /lib/init/vars.sh
    # Debian - Define LSB log_* functions.
    # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
    . /lib/lsb/init-functions
fi

mount_usbfs()
{
# ensure usbfs is mounted
    if [ ! -f /proc/bus/usb/devices ]; then
	mount -t usbfs none /proc/bus/usb
    fi
}

#
# Debian function that starts the daemon/service
#
start_debian()
{
# ensure usbfs is mounted
    mount_usbfs
# Return
#   0 if daemon has been started
#   1 if daemon was already running
#   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test  /dev/null \
	|| return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
	$DAEMON_ARGS \
	|| return 2
    # SWM: PID file isn't been created - force this to happen:
    pidofproc $NAME > $PIDFILE
}

#
# Debian function that stops the daemon/service
#
stop_debian()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	rm -f $PIDFILE
	return "$RETVAL"
}

#
# Redhat function to start daemon
#
start_redhat()
{
# ensure USBFS is mounted
        mount_usbfs

        echo -n $"Starting $NAME: "
        $JTAGD && success || failure
        RETVAL=$?
        pidofproc $NAME > $PIDFILE
        echo
}

#
# Redhat function to stop daemon
#
stop_redhat()
{
        echo -n $"Stopping $NAME: "
        if [ -n "`pidfileofproc $JTAGD`" ] ; then
            killproc $JTAGD
        else
            failure $"Stopping $NAME"
        fi

        echo
}


#
# Redhat handler
#
handle_case_redhat()
{
    case "$ARG" in
        start)
            start_redhat
            ;;
        stop)
            stop_redhat
            ;;
        restart)
            stop_redhat
            start_redhat
            ;;
        *)
            echo $"Usage: $0 {start|stop|restart}"
            RETVAL=1
    esac
    exit $RETVAL
}

#
# Debian handler
#
handle_case_debian()
{
    case "$ARG" in
	start)
	    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	    start_debian
	    case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	    esac
	    ;;
	stop)
	    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	    stop_debian
	    case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	    esac
	    ;;
	status)
	    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
	    ;;
	reload|force-reload|restart)
	    log_daemon_msg "Restarting $DESC" "$NAME"
	    stop_debian
	    case "$?" in
		0|1)
		    start_debian
		    case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		    esac
		    ;;
		*)
	  	# Failed to stop
		    log_end_msg 1
		    ;;
	    esac
	    ;;
	*)
	    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	    exit 3
	    ;;
    esac
}

if [ "$system" = "redhat" ]; then
    handle_case_redhat
elif [ "$system" = "debian" ]; then
    handle_case_debian
else
    echo "$0: Unknown system - Exiting" 1>&2
    exit 1
fi

exit 0
