#!/bin/sh

# For Redhat systems
#
# chkconfig: 345 29 71
# processname: /usr/sbin/tcf-agent
# description: Target Communication Framework

# For openSUSE system
#
### BEGIN INIT INFO
# Provides:          tcf-agent
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Target Communication Framework agent
# Description: Target Communication Framework agent
### END INIT INFO

#
# Location of the TCF daemon and the init directory
#
DAEMON_PATH=/usr/sbin/tcf-agent
DAEMON_NAME=`basename $DAEMON_PATH`

#
# Determine which kind of configuration we're using
#
system=unknown
if [ -f /etc/redhat-release -o -f /etc/fedora-release ]; then
    system=redhat
else
    if [ -f /etc/SuSE-release ]; then
        system=suse
    else
        echo "$0: Unknown system, please port and contact dsdp-tcf-dev@eclipse.org" 1>&2
        exit 1
    fi
fi

if [ $system = redhat ]; then
    . /etc/init.d/functions
fi

if [ $system = suse ]; then
    . /etc/rc.status
    rc_reset
fi

test -e $DAEMON_PATH || exit 0

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# Redhat start/stop function.
#
function redhat()
{

#
# See how we were called.
#
case "$1" in
  start)
    echo -n $"Starting $DAEMON_NAME:"
    $DAEMON_PATH -d -L- -l0 -s SSL:
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
      success "$DAEMON_NAME startup"
    else
      failure "$DAEMON_NAME startup"
    fi
    [ $RETVAL = 0 ] && touch /var/lock/subsys/$DAEMON_NAME
    echo
    ;;
  stop)
    echo -n $"Stopping $DAEMON_NAME:"
    count=0
    while [ -n "`/sbin/pidof $DAEMON_PATH`" -a $count -lt 10 ] ; do
      killproc $DAEMON_PATH -USR2 >& /dev/null
      sleep 1
      RETVAL=$?
      if [ $RETVAL != 0 -o -n "`/sbin/pidof $DAEMON_PATH`" ] ; then
        sleep 3
      fi
      count=`expr $count + 1`
    done
    rm -f /var/lock/subsys/$DAEMON_NAME
    if [ -n "`/sbin/pidof $DAEMON_PATH`" ] ; then
      failure "$DAEMON_NAME shutdown"
    else
      success