#! /bin/sh
#
# Startup script for the sas_shim Daemon
# (C) Copyright 2003-2004,2016 Fred Gleason <fredg@paravelsystems.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2 as
#   published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

### BEGIN INIT INFO
# Provides:          sas_shim
# Required-Start:    $remote_fs mysql rivendell
# Required-Stop:     $remote_fs
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Description:       Start the SAS Shim Daemon
### END INIT INFO

. /lib/lsb/init-functions

#
# Package Definitions
#
PACKAGE_BIN=/usr/local/sbin/sas_shim
PACKAGE_CONFIG=/etc/rd.conf
PACKAGE_PID=/var/run/sas_shim.pid
PACKAGE_NAME="SAS Shim Daemon"

# Check for missing binaries
if [ ! -x $PACKAGE_BIN ] ; then
  echo "$PACKAGE_BIN not installed"
  exit 5
fi

# Check for existence of needed config file and read it
if [ ! -r $PACKAGE_CONFIG ] ; then
  echo "Missing $PACKAGE_CONFIG"
  exit 6}
fi

case "$1" in
    start)
	if test ! -f $PACKAGE_PID ; then
	    $PACKAGE_BIN
	    sleep 1
	else 
	    if test ! -d /proc/`cat $PACKAGE_PID` ; then
		$PACKAGE_BIN
		sleep 1
	    fi
	fi
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_success_msg "Starting $PACKAGE_NAME"
		exit 0
	    else
		log_failure_msg "Starting $PACKAGE_NAME"
		exit 1
	    fi
	else
	    log_failure_msg "Starting $PACKAGE_NAME"
	    exit 1
	fi
	;;
    stop)
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		kill `cat $PACKAGE_PID` 2> /dev/null
		sleep 1
	    fi
	fi
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_failure_msg "Shutting down $PACKAGE_NAME"
		exit 1
	    else
		log_success_msg "Shutting down $PACKAGE_NAME"
		exit 0
	    fi
	else
	    log_success_msg "Shutting down $PACKAGE_NAME"
	    exit 0
	fi
	;;
    restart)
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		kill `cat $PACKAGE_PID`
		sleep 1
	    fi
	fi
	$PACKAGE_BIN
	sleep 1
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_success_msg "Restarting $PACKAGE_NAME"
		exit 0
	    else
		log_success_msg "Restarting $PACKAGE_NAME"
		exit 1
	    fi
	else
	    echo "failed."
	    exit 1
	fi
	;;
    force-reload)
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		kill `cat $PACKAGE_PID`
		sleep 1
	    fi
	fi
	$PACKAGE_BIN
	sleep 1
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		log_success_msg "Reloading $PACKAGE_NAME"
		exit 0
	    else
		log_failure_msg "Restarting $PACKAGE_NAME"
		exit 1
	    fi
	else
	    log_failure_msg "Restarting $PACKAGE_NAME"
	    exit 1
	fi
	;;
    status)
	echo -n "Checking for $PACKAGE_NAME..."
	if test -f $PACKAGE_PID ; then
	    if test -d /proc/`cat $PACKAGE_PID` ; then
		echo "running."
		exit 0
	    else
		echo "stale PID file."
		exit 1
	    fi
	else
	    echo "stopped."
	    exit 3
	fi
	;;
esac


# End of rc.sas_shim
