#!/bin/ksh93

PROG="${.sh.file##*/}"


SVCADM='/usr/sbin/svcadm'
ALL_SVCS='bmrbd nbdisco nbftclnt bpcd vnetd vxpbxd'
ZNAME="${ zonename ; }"

# Time in seconds to wait for bpbackup|bparchive|bprestore to stop before they
# got forcibly terminated.
integer GRACETIME=5
WAITFOR='bpbackup|bparchive|bprestore'

function startAll {
	typeset STATE='' X KEY VAL T=''
	(( TEMP )) && T='-t'
	for X in ${ALL_SVCS} ; do
		svcs -l netbackup:$X | while read KEY VAL ; do
			[[ ${KEY}  == 'enabled' ]] && STATE="${VAL}" && break
		done
		[[ ${STATE} == 'false (temporary)' ]] && \
			${SVCADM} enable -s ${T} netbackup:$X
	done
	return 0
}

function stopAll {
	typeset PIDS=${ pgrep -z "${ZNAME}" -l "${WAITFOR}" ; } T
	integer I
	(( TEMP )) && T='-t' || T=''
	if [[ -n ${PIDS} ]]; then
		print "The following process is still running:\n${PID}"
		if (( FORCE )); then
			print 'All are terminated ...'
		else
			# chance to exit
			typeset -l X=''
			while [[ $X != 'n' && $X != 'no' && $X != 'y' && $X != 'yes' ]]
			do
				read X?'Do you still want to disable all services [y/N]: '
				[[ -z $X ]] && X='n'
			done
			[[ $X == 'y' || $X == 'yes' ]] || exit 2
		fi
		pkill -HUP -z "${ZNAME}" "${WAITFOR}"
		for T in {0..${GRACETIME}} ; do
			PIDS=${ pgrep -z "${ZNAME}" "${WAITFOR}" ; }
			[[ -z ${PIDS} ]] && break
			sleep 1
		done
		[[ -n ${PIDS} ]] && pkill -TERM -z "${ZNAME}" "${WAITFOR}"
	fi
	for X in ${ALL_SVCS} ; do
		${SVCADM} disable -s ${T} netbackup:$X
	done
	return 0
}

function printUsage {
    getopts -a "${PROG}" "${ print ${USAGE}; }" OPT --man
}

USAGE_START='[-?$Id: bp.all 627 2014-07-10 19:43:56Z elkner $ ]
[-copyright?Copyright (c) 2014 Jens Elkner. All rights reserved.]
[-license?CDDL 1.0]
[+NAME?'"${PROG}"' - enbable all temporary disabled NetBackup client services]
[+DESCRIPTION?Checks for all temporary disabled NetBackup client services and enables them permanently, or if option \b-t\b is given temporaryly.]
[h:help?Print this help and exit.]
[F:functions?Print a list of all functions available.]
[T:trace]:[functionList?A comma separated list of functions of this script to trace (convinience for troubleshooting).] 
[+?]
[t:temporary?Enable the services in question temporaryly, not permanently.]
'
USAGE_STOP='[-?$Id: bp.all 627 2014-07-10 19:43:56Z elkner $ ]
[-copyright?Copyright (c) 2014 Jens Elkner. All rights reserved.]
[-license?CDDL 1.0]
[+NAME?'"${PROG}"' - disable all NetBackup client services]
[+DESCRIPTION?Disables all NetBackup client services temporaryly, or if option \b-p\b is given permanently. Once they are permanently disabled, you need to enable the service in question manually to get it back online.]
[h:help?Print this help and exit.]
[F:functions?Print a list of all functions available.]
[T:trace]:[functionList?A comma separated list of functions of this script to trace (convinience for troubleshooting).] 
[+?]
[p:permanently?Disable the services in question permanently, not temporaryly.]
[g:grace]:[seconds?Seconds to wait for graceful termination of related processes, which require the corresponding services running. Invalid values are silently ignored.]
[f:force?Do not ask, whether all service should still be disabled, if there are processes, which require the corresponding services running.]
'
integer FORCE=0 TEMP=0 START=1
if [[ ${PROG} =~ kill ]]; then
	START=0 ; TEMP=1
	typeset -n USAGE=USAGE_STOP
else
	typeset -n USAGE=USAGE_START
fi

while getopts -a "${PROG}" "${ print ${USAGE}; }" OPT ; do
	case "${OPT}" in
		h) printUsage ; exit 0 ;;
		T) typeset -ft ${OPTARG//,/ } ;;
		F) typeset +f && exit 0 ;;
		t) TEMP=1 ;;
		p) TEMP=0 ;;
		f) FORCE=1 ;;
	esac
done

(( START )) && startAll || stopAll
