#! /bin/sh

#
# Copyright (c) 1990-2023, Dell Inc. or its subsidiaries. All Rights Reserved.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

# chkconfig: 35 95 05
# description: EMC NetWorker. A backup and restoration software package.

### BEGIN INIT INFO
# Provides: networker
# Required-Start: syslog network
# Required-Stop: syslog network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: EMC NetWorker. A backup and restoration software package.
### END INIT INFO

# IMPORTANT NOTE:
# Upgrading NetWorker will overwrite this startup script.
# End-users should place NetWorker environment variables in nsrrc, which will
# be sourced before starting NetWorker daemons. nsrrc must be a Bourne shell
# script, and environment variables in nsrrc must be exported to take effect.
NSRRC=/nsr/nsrrc
NSR_ENVEXEC=/opt/nsr/admin/nsr_envexec

# networkerrc defines environment variables, such as LD_LIBRARY_PATH, required
# to run NetWorker daemons.
NETWORKERRC=/opt/nsr/admin/networkerrc

CLNT_PKG_HOME=`rpm -q --qf "%{INSTPREFIXES}\n" lgtoclnt 2>/dev/null`

start_service()
{
	(echo 'starting NetWorker daemons:') > /dev/console
	if [ -x /opt/nsr/admin/nsr_serverrc ]; then
		. /opt/nsr/admin/nsr_serverrc || exit 1
	fi
	if [ -f ${CLNT_PKG_HOME}/sbin/nsrexecd ]; then
		if [ -f ${CLNT_PKG_HOME}/sbin/NetWorker.clustersvr ]; then
			if [ -d /nsr.NetWorker.local -o \
			    -h /nsr.NetWorker.local ]; then
				if [ -h /nsr ]; then
					rm -f /nsr
					ln -s /nsr.NetWorker.local /nsr
				fi
			fi
		fi
		"$NSR_ENVEXEC" -u "$NSRRC" -s "$NETWORKERRC" \
			"${CLNT_PKG_HOME}/sbin/nsrexecd" 2>&1 | /usr/bin/tee /dev/console
		(echo ' nsrexecd') > /dev/console
		if [ -x ${CLNT_PKG_HOME}/sbin/nsrpsd ]; then
			${CLNT_PKG_HOME}/sbin/nsrpsd -i
		fi
	fi
	if [ -f ${CLNT_PKG_HOME}/sbin/nsrctld -a \
	    ! -f ${CLNT_PKG_HOME}/sbin/NetWorker.clustersvr ]; then
		"$NSR_ENVEXEC" -u "$NSRRC" -s "$NETWORKERRC" \
			"${CLNT_PKG_HOME}/sbin/nsrctld" 2>&1 | /usr/bin/tee /dev/console
		(echo ' nsrctld') > /dev/console
	fi

	if [ -f /opt/nsr/nsrmq/bin/nsrmqd ]; then
		"$NSR_ENVEXEC" -s "$NETWORKERRC" \
			/opt/nsr/nsrmq/bin/nsrmqd --config=/opt/nsr/nsrmq/nsrmq.cfg \
			--pidfile=/opt/nsr/nsrmq/nsrmq.pid \
			--logfile=/opt/nsr/nsrmq/logs/nsrmq.log 2>&1 | \
			/usr/bin/tee /dev/console
		(echo ' nsrmqd') > /dev/console
	fi
	if [ -d /var/lock/subsys ]; then
		touch /var/lock/subsys/networker
	fi
}

case $1 in
    start)
	start_service
	;;
    stop)
	EXITCODE=0
	(echo 'stopping NetWorker daemons:') > /dev/console
	if [[ -f ${CLNT_PKG_HOME}/sbin/nsr_shutdown ]]; then
		mkfifo /tmp/nsr_shutdown.pipe
		if [ $? -eq 0 ]; then
			# tee will exit after the nsr_shutdown call
			/usr/bin/tee /dev/console < /tmp/nsr_shutdown.pipe &
			(${CLNT_PKG_HOME}/sbin/nsr_shutdown -q) > \
				/tmp/nsr_shutdown.pipe 2>&1
			EXITCODE=$?
			rm /tmp/nsr_shutdown.pipe
		else
			# at least this way we'll log to the system still
			(${CLNT_PKG_HOME}/sbin/nsr_shutdown -q) > \
				/dev/console 2>&1
			EXITCODE=$?
		fi
		(echo ' nsr_shutdown -q') > /dev/console
	fi
	if [[ -f ${CLNT_PKG_HOME}/sbin/nsrps_shutdown ]]; then
		(${CLNT_PKG_HOME}/sbin/nsrps_shutdown -q) 2>&1 | \
			/usr/bin/tee /dev/console
		(echo ' nsrps_shutdown -q') > /dev/console
	fi
	if [ $EXITCODE -eq 0 ]; then
		rm -f /var/lock/subsys/networker
	fi
	exit $EXITCODE
	;;
    startps)
	if [ -d /etc/init.d -a ! -L /etc/init.d ]; then
		NSRPS=/etc/init.d/nsrps
	else
		NSRPS=/etc/rc.d/init.d/nsrps
	fi
	(echo 'starting nsrpsd daemon:') > /dev/console
	if [ -f ${NSRPS} ]; then
		(${NSRPS} start)
		result=$?
		if [ ${result} -eq 0 ]; then
			(/bin/sleep 15)
			(echo ' nsrpsd') > /dev/console
		elif [ ${result} -ne 10 ]; then
			(echo ' Error: nsrpsd failed to start') > /dev/console
		fi
	fi
	;;
    status)
	if [ -f ${CLNT_PKG_HOME}/sbin/nsr_shutdown ]; then
		${CLNT_PKG_HOME}/sbin/nsr_shutdown -l
	fi
	;;
    *)
	echo "usage: `basename $0` {start|stop|status}"
	;;
esac

