#!/bin/sh

# For Redhat or Wind River Linux 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`
DAEMON_ARGS="-d -L- -l0 -s SSL:"

#
# Determine which kind of configuration we're using
#
system=unknown
if [ -f /etc/redhat-release -o -f /etc/fedora-release ]; then
    system=redhat
fi

if [ -f /etc/wrs-release ]; then
    system=wrlinux
fi

if [ -f /etc/SuSE-release ]; then
    system=suse
fi

if [ -f /etc/os-release ]; then
    # Tested on Raspberry PI: "2013-02-09-wheezy-raspbian"
    system=debian
fi

if [ $system = unknown ]; then
    echo "$0: Unknown system, please port and contact tcf-dev@eclipse.org" 1>&2
    exit 1
fi

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

if [ $system = wrlinux ]; then
    . /etc/rc.d/init.d/functions
    DAEMON_PATH=/usr/bin/tcf-agent
    DAEMON_ARGS="-d -L- -l0"
fi

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

if [ $system = debian ]; then
    #set -e
    #umask 022
    . /lib/lsb/init-functions
    DAEMON_ARGS="-d -L- -l0"
    PIDFILE=/var/run/$DAEMON_NAME.pid
fi

test -e $DAEMON_PATH || exit 0

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

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

#
# See how we were called.
#
case "$1" in
  start)
    echo -n $"Starting $DAEMON_NAME:"
    $DAEMON_PATH $DAEMON_ARGS
    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 "$DAEMON_NAME shutdown"
    fi
    echo
    ;;
  restart)
    redhat stop
    redhat start
    ;;
  status)
    if [ -n "`/sbin/pidof $DAEMON_PATH`" ] ; then
      echo "$DAEMON_NAME is running"
    else
      echo "$DAEMON_NAME is not running"
    fi
    ;;
  condrestart)
    [ -f /var/lock/subsys/$DAEMON_NAME ] && redhat restart
    ;;
  *)
  echo $"Usage: $0 {start|stop|restart|condrestart|status}"
esac
}

# SuSE start/stop function.
#
suse()
{

#
# See how we were called.
#
case "$1" in
    start)
    echo -n "Starting TCF agent "
    /sbin/startproc -l /var/log/rctcfagent.log $DAEMON_PATH $DAEMON_ARGS
    rc_status -v
    ;;
    stop)
    echo -n "Shutting down TCF agent "
    /sbin/killproc -TERM $DAEMON_PATH
    rc_status -v
    ;;
    try-restart|condrestart)
    ## Do a restart only if the service was active before.
    ## Note: try-restart is now part of LSB (as of 1.9).
    ## RH has a similar command named condrestart.
    if test "$1" = "condrestart"; then
        echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
    fi
    suse status
    if test $? = 0; then
        suse restart
    else
        rc_reset    # Not running is not a failure.
    fi

    rc_status
    ;;
    restart)
    suse stop
    suse start
    rc_status
    ;;
    force-reload)
    echo -n "Reload service TCF agent "
    /sbin/killproc -HUP $DAEMON_PATH
    rc_status -v
    ;;
    reload)
    echo -n "Reload service TCF agent "
    /sbin/killproc -HUP $DAEMON_PATH
    rc_status -v
    ;;
    status)
    echo -n "Checking for service TCF agent "
    /sbin/checkproc $DAEMON_PATH
    rc_status -v
    ;;
    *)
    echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
    exit 1
    ;;
esac
}

# Debian start/stop function.
#
debian()
{

#
# See how we were called.
#
case "$1" in
  start)
    log_daemon_msg "Starting TCF agent" "$DAEMON_NAME" || true
    pid=$( pidofproc $DAEMON_PATH )
    if [ -n "$pid" ]; then
      log_begin_msg "Already running."
      log_end_msg 0
      exit 0
    fi
    if start-stop-daemon --start --quiet --oknodo --exec $DAEMON_PATH -- $DAEMON_ARGS ; then
        pid=$( pidofproc $DAEMON_PATH )
        echo -n "$pid" > "$PIDFILE"
        log_end_msg 0 || true
    else
        log_end_msg 1 || true
    fi
    ;;
  stop)
    log_daemon_msg "Shutting down TCF agent" "$DAEMON_NAME" || true
    if start-stop-daemon --stop --quiet --oknodo --exec $DAEMON_PATH ; then
        rm -f "$PIDFILE"
        log_end_msg 0 || true
    else
        log_end_msg 1 || true
    fi
    ;;
  restart)
    log_daemon_msg "Restarting TCF agent" "$DAEMON_NAME" || true
    start-stop-daemon --stop --quiet --oknodo --retry 30 --exec $DAEMON_PATH
    rm -f "$PIDFILE"
    if start-stop-daemon --start --quiet --oknodo --exec $DAEMON_PATH -- $DAEMON_ARGS ; then
        pid=$( pidofproc $DAEMON_PATH )
        echo -n "$pid" > "$PIDFILE"
        log_end_msg 0 || true
    else
        rm -f "$PIDFILE"
        log_end_msg 1 || true
    fi
    ;;
  status)
    status_of_proc -p "$PIDFILE" $DAEMON_PATH $DAEMON_NAME && exit 0 || exit $?
    ;;
  *)
    log_action_msg "Usage: /etc/init.d/tcf-agent {start|stop|status|restart}" || true
    exit 1
    ;;
esac
}


if [ $system = suse ]; then
  suse "$@"
  rc_exit
fi

RETVAL=0

if [ $system = redhat -o $system = wrlinux  ]; then
  redhat "$@"
fi

if [ $system = debian ]; then
  debian "$@"
fi

exit $RETVAL
