#! /bin/bash
# start/stop script for Oracle Listener; typically the config file is a link to oracle config
# compatible with Server-To-Server v2
#
# 2015-May-19 pmo  corrected profile invocation to bash standard
# 2007-Jul-16 aka  current cs2 "rewritten" to cs3
#
# pseudo comments for Novell/SuSE Linux
### BEGIN INIT INFO
# Provides: ora_lsnr
# Required-Start: $network ora_censhare
# Required-Stop: $network ora_censhare
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the oracle listener.
### END INIT INFO

# pseudo comments for RedHat / CentOS / Fedora Linux
### BEGIN
# chkconfig: 345 82 11
### END

test -z "$BASH" && exec /bin/bash $0 "$@"		# needed by Solaris: force bash

progname=$(basename $0)
progname=${progname#rc}
progname=${progname#[KS][0-9][0-9]}
test "$UID" -ne 0  && { echo "$progname: You must be root."; exit 1; }

action="$1"                                             # save action
ec=0                                                    # default: success

# status handling a la LSB-2.x, -3.x
# http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
 ret_other=(success error)                              # return values
ret_status=(running dead_but_run dead_but_lock unused          unknown           reserved5     not_configured)
 ret_start=(started error        inv_args      not_implemented insuff_privileges not_installed not_configured not_running)
  ret_stop=(stopped error        inv_args      not_implemented insuff_privileges not_installed not_configured not_running)
#           0       1            2             3               4                 5             6              7
rc_status () {                                          # inform user
    local s
    case "$action" in
        start)  s=${ret_start[$ec]};;
        stop)   s=${ret_stop[$ec]};;
        status) s=${ret_status[$ec]};;
        *)      test "$ec" = 0 && s=${ret_other[0]} || s=${ret_other[1]};;
    esac
    echo ": ${s:-unknown_exit_code_$ec}"
}

# read configuration
ConfErr () { echo >&2 "$progname: $*"; exit 6; }
cfglist=""						# collect tried pathes, output on error for debugging
ChkCfgfile () {						# first try /etc/sysconfig then /etc/s2s/sysconfig
    local cf="$1"; test -z "$cf" && ConfErr "$FUNCNAME(): filename missing."
    CONFIG_FILE=/etc/sysconfig/$cf;     cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    CONFIG_FILE=/etc/s2s/sysconfig/$cf; cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    CONFIG_FILE=/etc/$cf; cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    return 1						# failed
}
test -z "$CONFIG_FILE" && {				# if no config file defined by environment, look for
    ChkCfgfile $progname.$HOSTNAME || {			#     host specific one or
	ChkCfgfile $progname || {			#     general one or error
	    ConfErr "configuration missing, searched: '${cfglist# }'."; }; }
}							# endif
test -s "$CONFIG_FILE" && . $CONFIG_FILE		# read config
# check for mandatory config vars
test -z "$ORA_USER" && ConfErr "oracle user unknown, ORA_USER unset."

#ORA_USER=oracle					# change manually if multiple databases used
eval ORA_HOME=~$ORA_USER
test ! -d $ORA_HOME && ConfErr "user '$ORA_USER' has no home dir, may not mounted."
LOG=$ORA_HOME/.ora_lsnr.log				# startup logfile
ORA_PROD=$(ls -1dt $ORA_HOME/orahome 2>/dev/null | tail -1)
test -z "$ORA_PROD" && ConfErr "dir '$ORA_HOME/orahome' not avail, may not mounted."
ORA_LSNR_BIN=$ORA_PROD/bin/lsnrctl
test -x $ORA_LSNR_BIN || ConfErr "oracle '$ORA_LSNR_BIN' not found."

# tmpfile handling
OnExit () {
    set +x
    rm -f $tmpfile $tmpfile.*
}
tmpfile=${TMP:-/tmp}/$progname-$$.tmp
trap OnExit EXIT

case "$1" in
    start)
	echo -n "    Starting $progname"
	su - $ORA_USER -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $ORA_LSNR_BIN status >&/dev/null || $ORA_LSNR_BIN start >>$LOG 2>&1 </dev/null;
		echo \$? >$tmpfile" >&/dev/null </dev/null
	test "$(cat $tmpfile)" = 0 && ec=0 || ec=3
	rc_status
	;;
    stop)
	echo -n "    Shutting down $progname"
	su - $ORA_USER -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $ORA_LSNR_BIN stop" >&/dev/null </dev/null
	ec=0						# success anyway
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop; $0 start; ec=$?
	;;
    status)
	echo -n "    Checking for $progname"
	su - $ORA_USER -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $ORA_LSNR_BIN status >&/dev/null; echo \$? >$tmpfile" >&/dev/null </dev/null
 	test "$(cat $tmpfile)" = 0 && ec=0 || ec=3
	rc_status
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart}"
	ec=1
	;;
esac
exit $ec

# Local Variables:
# mode:			shell-script
# mode:			font-lock
# comment-column:	56
# fill-column:		78
# End:
