#! /bin/sh
# Copyright (c) 2002 ALT Linux Team, Russia.
#
# Author: Igor Muratov <migor@altlinux.ru>
#
# /sbin/init.d/oracle-agent
#
# chkconfig: 35 84 16
# description:  The start/stop script for Oracle Intelligent Agent
#

. /etc/init.d/functions

SourceIfNotEmpty /etc/profile.d/oracle8.sh
SourceIfNotEmpty /etc/profile.d/oracle9.sh

# Setup environment
LOCKFILE=/var/lock/subsys/oracle-agent
USER=oracle
ORATAB=/etc/oratab
HOMEDIR=`grep ^$USER: /etc/passwd | /bin/awk -F: '{print $6}'`
RETVAL=0

# Stop when all bases is disabled
[ -s $ORATAB ] \
	&& /bin/grep -v "^#" $ORATAB | /bin/awk -F: '{print $3}' \
	| grep "Y" >/dev/null || exit 1

start()
{
	# Oracle8i an Oracle9i uses a diffrent command
	if [ -x $ORACLE_HOME/bin/agentctl ]; then
		# Oracle 9i
		AGENT_START="$ORACLE_HOME/bin/agentctl start"
	else
		# Oracle 8i
		[ -x "$ORACLE_HOME/bin/lsnrctl" ] || exit 1
		AGENT_START="$ORACLE_HOME/bin/lsnrctl dbsnmp_start"
	fi
	action "Starting Oracle Intellegent Agent:" \
		su - $USER -c \"$AGENT_START\" || return
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}
stop()
{
	# Oracle8i an Oracle9i uses a diffrent command
	if [ -x $ORACLE_HOME/bin/agentctl ]; then
		# Oracle 9i
		AGENT_STOP="$ORACLE_HOME/bin/agentctl stop"
	else
		# Oracle 8i
		AGENT_STOP="$ORACLE_HOME/bin/lsnrctl dbsnmp_stop"
	fi
	action "Shutting down Oracle Intellegent Agent:" \
		/bin/su - $USER -c \"$AGENT_STOP\" || return
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}
restart()
{
	stop
	start
}

case "$1" in
    start)
		start
        ;;
    stop)
		stop
        ;;
    restart|reload)
		restart
        ;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
    status)
        status dbsnmp
        status dbsnmpwd
		RETVAL=$?
        ;;
    *)
        echo "Usage: ${0##*/} {start|stop|restart|reload|condstop|condrestart|status}"
        RETVAL=1
        ;;
esac

exit $RETVAL

