#! /bin/sh
# Copyright (c) 2000-2001, AEP Ltd. 

# All rights reserved.

# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, 
# this list of conditions and the following disclaimer.

# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation 
# and/or other materials provided with the distribution.

# 3. Neither the name of AEP Ltd. nor the names of its contributors 
# may be used to endorse or promote products derived from this software 
# without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

# *****************************************************************************
#****************************************************************************/

#/**********************************************************************
#*                                                                     *
#* AEP Ltd.                                                            *
#*                                                                     *
#* This software is supplied under the terms of the license included   *
#* above.  All use of this software must be in accordance with the     *
#* terms of that license.                                              *
#*                                                                     *
#**********************************************************************/
 

DAEMON="aepdaemon"
DRIVER="paep"

#AEPC- Arguments to the daemon (if any)
ARGS=""

PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:${PATH}
export PATH

#AEPC- Check user is root
if id | grep "uid=0(root)" > /dev/null 2>&1 ; then
	: 
else
	echo "ERROR:   You must be root to run this program. " 2>&1 
	exit 1
fi

#AEPC-==========================================================
#AEPC- Check platform OK and set some platform-specific stuff...
#AEPC-==========================================================
PLATFORM=`(uname -s) 2>/dev/null`  || PLATFORM="unknown"     

case $PLATFORM in
	  Linux)
		ps_command="ps -e -o pid -o comm"
		DRV=paep
     		;;
	     *)
	  	echo "ERROR:    This script is for Linux only." 2>&1
		exit 1
		;;
esac


#AEPC-=========================================================
#AEPC- Some useful functions : 
#AEPC-=========================================================


#AEPC- A function to return the pid's of all instances of a named process 
#AEPC--------------------------------------------------------------------       
pidsofproc() {
    # Test syntax.
    if [ $# -eq 0 ] ; then
            echo "ERROR:   Usage: pidsofproc {program}" 2>&1
            return 1
    fi

    # Extract from ps...returns multiple hits separated by spaces
    ${ps_command} | grep $1 | grep -v 'grep'| awk '{ORS=" "; print $1}'
}

#AEPC- CheckIfRunning : Returns 0 if a given process IS running, 1 otherwise.
#AEPC------------------------------------------------------------------------

CheckIfRunning() {
	
	pidlist=`pidsofproc $1`

	if [ -n "$pidlist" ] ; then 
		return 0
	else
		return 1
	fi
}

#AEPC-===========================================================

#AEPC-======================================================
#AEPC- "Main" of script:
#AEPC-======================================================

#AEPC- Get home dir of this script 
ScriptName=`basename $0`
CurrentDirectory=`pwd`
ScriptDirectory=`dirname $0`
case "${ScriptDirectory}" in
  /*) ;;
  .) ScriptDirectory="$CurrentDirectory" ;;
  *) ScriptDirectory="$CurrentDirectory"/"$ScriptDirectory" ;;
esac

DAEMON=$ScriptDirectory/${DAEMON}
daemon_name=`basename ${DAEMON}`


#AEPC- Check syntax and see how we were called...
case "$1" in
  start)

	#AEPC- See if it's already running....
       	if CheckIfRunning $daemon_name ; then
		echo "WARNING:  AEP daemon \"${daemon_name}\" is already running." 2>&1
		exit 2
	fi

	#AEPC- Make sure it doesn't core dump anywhere
        ulimit -S -c 0 >/dev/null 2>&1

	#AEPC- Check that the driver is loaded 
	if grep "${DRIVER}" /proc/modules > /dev/null 2>&1 ; then
	    :
	else
	    echo "ERROR:    Cannot continue.  AEP driver \"${DRIVER}\" not installed." 2>&1	
	    exit 1
	fi
    	
        #AEPC- Start up the daemon....
        if ${daemon_name} "${ARGS}" ; then
		:
	else
		echo "Error:    Failed to start \"${daemon_name}\" " 2>&1
		exit 1
        fi
	
	#AEPC- Give time to initialise and make sure it's running OK.
	sleep 1
	if CheckIfRunning $daemon_name ; then
		exit 0
	else
		echo "Error:    Failed to start AEP daemon \"${daemon_name}\" " 2>&1
		exit 1
	fi
	;;

  stop)

	#AEPC- Get the pid(s)....
        pidlist=`pidsofproc $daemon_name`        
	
	#AEPC- Stop it.
	if CheckIfRunning $daemon_name 2>&1; then
		#AEPC- TERM first, then KILL if not dead
		kill -TERM $pidlist > /dev/null 2>&1 
	   	sleep 1
	   	if CheckIfRunning $daemon_name 2>&1; then
			kill -KILL $pid > /dev/null 2>&1 	
	   	fi
	else
			exit 0
	fi
       	 			
        ;;

 restart)
        cd $CurrentDirectory
        if $0 stop ; then 
	    if $0 start ; then
		exit 0
	    else
		echo "ERROR:    AEP daemon restart failed! " 2>&1
		exit 1
	    fi
	else
	  echo "ERROR:    AEP daemon restart failed!" 2>&1
	  exit 1
	fi
        ;;
  start_msg)
	echo "Starting AEP daemon" 2>&1
	;; 
    
  stop_msg)
	echo "Stopping AEP daemon" 2>&1
	;;
  *)
        echo "ERROR:    Usage: $0 {start|stop|restart|start_msg|stop_msg}" 2>&1
        exit 1
	;;
esac                      

exit 0

#/*AEPCPB								   	*/
#/* COPYRIGHT (c) 1999-2001 BY ACCELERATED ENCRYPTION PROCESSING LTD (AEP).  	*/
#/* 39/40 UPPER MOUNT STREET,DUBLIN 2,IRELAND				   	*/
#/* ALL RIGHTS RESERVED.						   	*/
#/* THIS SOFTWARE OR DOCUMENTATION IS THE CONFIDENTIAL AND PROPRIETARY  	*/
#/* INFORMATION OF AEP ("CONFIDENTIAL INFORMATION") YOU SHALL NOT DISCLOSE	*/
#/* SUCH CONFIDENTIAL INFORMATION AND SHALL USE IT ONLY IN ACCORDANCE WITH THE  */
#/* TERMS OF WRITTEN LICENCE AGREEMENTS AND/OR NON-DISCLOSURE AGREEMENTS   	*/
#/* ENTERED INTO WITH AEP.						   	*/
#/* AEP PROVIDES NO WARRANTY AS TO THE QUALITY OF THIS SOFTWARE OR		*/
#/* DOCUMENTATION OR ITS SUITABILITY FOR ANY PARTICULAR PURPOSE AND TO THE	*/
#/* EXTENT PERMITTED BY LAW YOU HEREBY WAIVE ANY RIGHTS TO COMPENSATION FOR	*/
#/* DAMAGES WHETHER DIRECT OR CONSEQUENTIAL CAUSED BY THIS SOFTWARE. NO PART	*/
#/* OF THIS SOFTWARE OR DOCUMENTATION MAY BE REPRODUCED; TRANSMITTED;		*/
#/* TRANSCRIBED; STORED IN A RETRIEVAL SYSTEM; OR TRANSLATED INTO ANY		*/
#/* LANGUAGE OR COMPUTER  LANGUAGE IN ANY FORM OR BY ANY MEANS ELECTRONIC;	*/
#/* MECHANICAL; MAGNETIC; OPTICAL; CHEMICAL; MANUAL OR OTHERWISE, EXCEPT IN	*/
#/* ACCORDANCE WITH LICENSE AGREEMENTS YOU HAVE ENTERED INTO, WITHOUT THE 	*/
#/* PRIOR WRITTEN PERMISSION OF AEP.						*/
#/*									  AEPCPE*/

