#!/bin/sh
# Application
APP_NAME="PAM360Agent"
APP_LONG_NAME="PAM360Agent"
rootuser="false"
uid=`id -u`
if [ ${uid} = 0 ]
then
  rootuser="true"
else
  rootuser="false"
fi

if [ "${rootuser}" == "false" ]
then
 echo " "
 echo "start/stop operations can be performed only by Root User"
 echo " "
 exit 1
fi


# Wrapper
#WRAPPER_CMD="./wrapper"
#WRAPPER_CONF="../conf/wrapper.conf"

# Priority at which to run the wrapper.  See "man nice" for valid priorities.
#  nice is only used if a priority is specified.
PRIORITY=

# Location of the pid file.
PIDDIR="./"

RUN_AS_USER=`stat -c%U ../mysql/bin/mysqld 2> /dev/null`
# Do not modify anything beyond this point
#-----------------------------------------------------------------------------

# Get the fully qualified path to the script
case $0 in
    /*)
        SCRIPT="$0"
        ;;
    *)
        PWD=`pwd`
        SCRIPT="$PWD/$0"
        ;;
esac

# Change spaces to ":" so the tokens can be parsed.
SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
# Get the real path to this script, resolving any symbolic links
TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'`
REALPATH=
for C in $TOKENS; do
    REALPATH="$REALPATH/$C"
    while [ -h "$REALPATH" ] ; do
        LS="`ls -ld "$REALPATH"`"
        LINK="`expr "$LS" : '.*-> \(.*\)$'`"
        if expr "$LINK" : '/.*' > /dev/null; then
            REALPATH="$LINK"
        else
            REALPATH="`dirname "$REALPATH"`""/$LINK"
        fi
    done
done
# Change ":" chars back to spaces.
REALPATH=`echo $REALPATH | sed -e 's;:; ;g'`

# Change the current directory to the location of the script
cd "`dirname "$REALPATH"`"

# Process ID
PIDFILE="$PIDDIR/$APP_NAME.pid"
pid=""

# Resolve the location of the 'ps' command
PSEXE="/usr/bin/ps"
if [ ! -x $PSEXE ]
then
    PSEXE="/bin/ps"
    if [ ! -x $PSEXE ]
    then
        echo "Unable to locate 'ps'."
        echo "Please report this with the location on your system."
        exit 1
    fi
fi

# Build the nice clause
if [ "X$PRIORITY" = "X" ]
then
    CMDNICE=""
else
    CMDNICE="nice -$PRIORITY"
fi

getpid() {
    if [ -f $PIDFILE ]
    then
        if [ -r $PIDFILE ]
        then
            pid=`cat $PIDFILE`
            if [ "X$pid" != "X" ]
            then
                # Verify that a process with this pid is still running.
                pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
                if [ "X$pid" = "X" ]
                then
                    # This is a stale pid file.
                    rm -f $PIDFILE
                    echo "Removed stale pid file: $PIDFILE"
                fi
            fi
        else
            echo "Cannot read $PIDFILE."
            exit 1
        fi
    fi
}

testpid() {
    pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
    if [ "X$pid" = "X" ]
    then
        # Process is gone so remove the pid file.
        rm -f $PIDFILE
    fi
}

console() {
    echo "Running $APP_LONG_NAME..."
    getpid
    if [ "X$pid" = "X" ]
    then
        exec $CMDNICE $WRAPPER_CMD $WRAPPER_CONF wrapper.pidfile=$PIDFILE
    else
        echo "$APP_LONG_NAME is already running."
        exit 1
    fi
}
 
start() {
	echo -n "Starting PAM360 Agent ....  "
	pid=`ps -C PAM360Agent -o pid | grep -v PID 2>/dev/null`
	if [ "x$pid" != "x" ]; then
		echo "already started!"
	else
		nohup ./PAM360Agent >> nohup.txt 2>&1 &
		echo "started!"
	fi

}

stopit() {
	echo -n "Stopping PAM360 Agent ....  "
	pid=`ps -C  PAM360Agent -o pid | grep -v PID 2>/dev/null`
	if [ "x$pid" != "x" ]; then
		kill -9 $pid 2>/dev/null
		echo "stopped!"
	else
		echo "already stopped!"
	fi
}

status() {
    pid=`ps -aef | grep -v grep | grep PAM360Agent | awk {'print $2'}`
    if [ "X$pid" = "X" ]
    then
        echo "$APP_LONG_NAME is not running."
        exit 1
    else
        echo "$APP_LONG_NAME is running."
        exit 0
    fi
}

dump() {
    echo "Dumping $APP_LONG_NAME..."
    getpid
    if [ "X$pid" = "X" ]
    then
        echo "$APP_LONG_NAME was not running."

    else
        kill -3 $pid

        if [ $? -ne 0 ]
        then
            echo "Failed to dump $APP_LONG_NAME."
            exit 1
        else
            echo "Dumped $APP_LONG_NAME."
        fi
    fi
}

install(){
   echo "Installing $APP_LONG_NAME..."
   echo INSTALLKEY=$1 | tr -d '\r' >/etc/pam360agent.config
   chmod 700 /etc/pam360agent.config
}

reinstall(){
   echo "Installing $APP_LONG_NAME..."
   echo REINSTALLKEY=$1 | tr -d '\r' >/etc/pam360agent.config
   chmod 700 /etc/pam360agent.config
}


case "$1" in

    'console')
        console
        ;;

    'start')
        start
        ;;

    'stop')
        stopit
        ;;

    'restart')
        stopit
        start
        ;;

    'status')
        status
        ;;

    'dump')
        dump
        ;;

    'install')
        install $2
        ;;

     'reinstall')
        reinstall $2
        ;;


    *)
        echo "Usage: $0 { start | stop | restart }"
        exit 1
        ;;
esac

exit 0
