#!/bin/sh
#
# description: Starts and stops the Pervasive mkded and sqlmgr daemons

# chkconfig: 2345 92 92

# Include source function library

### BEGIN INIT INFO
# Provides:       pervasive.sql
# Required-Start: $network $remote_fs
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:   
# Description:    Start the Pervasive.SQL master daemon
### END INIT INFO

. /etc/rc.status

status() {
        local base=${1##*/}
        local pid

        # Test syntax.
        if [ $# = 0 ] ; then
                echo $"Usage: status {program}"
                return 1
        fi

        # First try "pidof"
        pid=`pidof -o $$ -o $PPID -o %PPID -x $1 || pidof -o $$ -o $PPID -o %PPID -x ${base}`
        if [ "$pid" != "" ] ; then
                echo $"${base} (pid $pid) is running..."
                return 0
        fi

        # Next try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ] ; then
                read pid < /var/run/${base}.pid
                if [ "$pid" != "" ] ; then
                        echo $"${base} dead but pid file exists"
                        return 1
                fi
        fi
        # See if /var/lock/subsys/ exists
        if [ -f /var/lock/subsys/${base} ]; then
                echo $"${base} dead but subsys locked"
                return 2
        fi
        echo $"${base} is stopped"
        return 3
}


start_psql(){
	echo "Starting Pervasive services: "
	BTRID=`/bin/ps ax | grep -v grep | grep mkded | awk '{print $1}'`
	if [ "X$BTRID" != "X" ] ; then
		echo Warning: the following service is running
		if [ "X$BTRID" != "X" ] ; then
			echo mkded
		fi
		echo
	fi
	echo mkded
	echo "cd $PVSW_ROOT/bin; . $PVSW_ROOT/bin/.bash_profile ; ./mkded	-start" | /bin/su - psql || exit 1
	touch /var/lock/subsys/psql
	echo ""
}

stop_psql(){
	echo "Shutting down Pervasive services: "
	SQLID=`/bin/ps ax | grep -v grep | grep sqlmgr | awk '{print $1}'`
	if [ "X$SQLID" != "X" ] ; then
		if [ -x /usr/local/psql/bin/sqlmgr ] ; then
            echo sqlmgr
			echo "cd $PVSW_ROOT/bin; . $PVSW_ROOT/bin/.bash_profile ; ./sqlmgr -stop"	| /bin/su - psql
		else
			kill -9 $SQLID
		fi
	fi
	BTRID=`/bin/ps ax | grep -v grep | grep mkded | awk '{print $1}'`
	if [ "X$BTRID" != "X" ] ; then
		echo mkded
		if [ -x /usr/local/psql/bin/mkded ] ; then
			echo "cd $PVSW_ROOT/bin; . $PVSW_ROOT/bin/.bash_profile ; ./mkded -stop" | /bin/su - psql
		else
			kill -9 $BTRID
		fi
	fi
	echo ""
	rm -f /var/lock/subsys/psql
}

force_psql(){
	if [ -x $PVSW_ROOT/bin/mkded ] ; then
		echo "Clearing Pervasive shared memory: "
		echo "cd $PVSW_ROOT/bin; . $PVSW_ROOT/bin/.bash_profile ; ./mkded -force" | /bin/su - psql
	fi

	# jme 27791
	MEMORY=`ipcs -m | grep psql | awk '{print $2}' | tr -d "psql "`
	if [ "X$MEMORY" != "X" ] ; then
		for i in $MEMORY ; do
			ipcrm shm $i
		done
	fi

	echo "Clearing semaphores: "
	SEMAFORES=`ipcs -s | grep psql | awk '{print $2}' | tr -d "psql "`
	if [ "X$SEMAFORES" != "X" ] ; then
		for i in $SEMAFORES ; do
			ipcrm sem $i
		done
	fi
	# em 40585
	rm -f /tmp/PSSem*

	# ayahin: defect 33091
	SQLID=`/bin/ps ax | grep -v grep | grep sqlmgr | awk '{print $1}'`
	BTRID=`/bin/ps ax | grep -v grep | grep mkded | awk '{print $1}'`
	if [ "X$SQLID" != "X" ] ; then
		if [ -x /usr/local/psql/bin/sqlmgr ] ; then
            echo sqlmgr
        fi
		kill -9 $SQLID
	fi
	if [ "X$BTRID" != "X" ] ; then
		echo mkded
		kill -9 $BTRID
	fi

	echo ""
}

# Setting up environment
PVSW_ROOT=/usr/local/psql
PATH=$PVSW_ROOT/bin:$PATH
LD_LIBRARY_PATH=$PVSW_ROOT/lib
cd $PVSW_ROOT/bin

RETVAL=0
TMPVAL=0

case "$1" in
  start)
	start_psql
	;;
  stop)
	stop_psql
	;;
  force)
	stop_psql
	force_psql
	;;
  restart)
	echo "Restarting Pervasive services: "
	stop_psql
	start_psql
	echo "done."
	;;
  status)
	echo "Status Pervasive services: "
	status mkded
	RETVAL=$?
	exit $RETVAL
	;;
  *)
	echo "Usage: psql {start|stop|force|restart|status}"
	exit 1
esac

