#!/bin/sh
#
# $id$
#
# PROVIDE: oracle
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: FreeBSD shutdown
# 
# Please see rc.conf(5) and rc(8) for further details.
#

. /etc/rc.subr

name=oracle

# make sure your rc.conf is not world-readable
oracle_sys_pass=${oracle_sys_pass:-"change_on_install"}

oracle_enable=${oracle_enable:-"NO"}
oracle_home=${oracle_home:-/opt/orahome/oracle}
oracle_sid=${oracle_sid:-ORACLE_SID}
oracle_user=${oracle_user:-oracle}
start_cmd="oracle_start"
stop_cmd="oracle_stop"
procname="${oracle_home}/bin/tnslsnr"

oracle_start()
{
	if [ -n `check_process ${procname}`]; then
	        su - ${oracle_user} -c "
        	export ORACLE_HOME=${oracle_home}
        	export ORACLE_SID=${oracle_sid} 
		${oracle_home}/bin/lsnrctl start
		${oracle_home}/bin/sqlplus /nolog <<EOF
			connect sys/${oracle_sys_pass} as sysoper
			spool ${oracle_home}/assistants/dbca/logs/startup.log
			startup
			exit
		EOF"
	else
		echo "Already running (at least, tnslsnr)"
	fi
}

oracle_stop()
{
	su - oracle -c "
	${oracle_home}/bin/lsnrctl stop
	${oracle_home}/bin/sqlplus /nolog <<EOF
		connect sys/${oracle_sys_pass} as sysoper
		spool ${oracle_home}/assistants/dbca/logs/shutdown.log;
		shutdown immediate
		exit
	EOF"
}

rcvar=`set_rcvar`
load_rc_config $name
run_rc_command "$1"

