#!/bin/sh
# $Id$
#***************************************************************************
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $
#***************************************************************************

PKG_NAME="VRTSnbclt"

#----- $Id$ -----
#
#		This function is a case statement sets
#		the ECHO variable
#		with the appropriate path & flags.

#Define Echo to allow escape characters
case "`uname -s`" in
	Linux*)
		unset POSIXLY_CORRECT
		ECHO="/bin/echo -e"
		;;
	SunOS*)
		ECHO="/usr/bin/echo"
		;;
	*)
		ECHO="echo"
		;;
esac

#----- $Id$
#
#  print_error is used in the native packaging control scripts
#  to display a message.  It expects one parameter:  the message
#  to be displayed.
#
#  Error messages should be in the peculiar format required by HP.
#  They must start with ERROR: and text message starts in column 10.
#  No blank lines or tabs in the message.  Message line cannot be
#  more than 72 characters or a new line must be started.  Second
#  line of a message must be indented to column 10.
#
#  ECHO is expected to be set already.

print_error ()
{
	if [ $# -ne 1 ] ; then
		${ECHO} "
ERROR:   Call to print_error has the wrong number of parameters."
		exit 1
	else
		# If the message text has any lines longer than 81 characters
		# (including the ERROR string or leading blanks), split them.
		${ECHO} "
ERROR:   ${1}
" | sed 's/\(.................................................................................\)/\1\
         /g'
	fi
}

# vi: set ft=sh:
#
#  Creates trace log file name.
#
#  It expects just 1 parameter, the package name.
#  Generates the tracefile name that includes the parent process id
#
#  print_error is expected to be included already.

if [ -z "${PPID}" ] ; then
	# On some platforms (esp. Solaris), the PPID variable doesn't get
	# through to the scriptlet. The script driver will write its pid to a
	# temporary file so we can consume it here. If there is no driver
	# (e.g., when user runs pkgadd directly), then we give up on getting
	# PPID and just use the current pid instead.
	ppid=`cat /tmp/pid.installer 2>/dev/null || ${ECHO} $$`
else
	ppid=${PPID}
fi

create_tracefile_name ()
{
	if [ $# -ne 1 ] ; then
		print_error "Call to create_tracefile_name did not contain package name."
		exit 2
	fi

	# Append parent process id to log file name so both the pre and post install
	# scripts will use the same file.
	trace_file="/tmp/install_${1}_trace.${ppid}"

	return 0
}

#----- $Id$
#
# The rpm package manager on Linux has a limitation where if a postinstall
# scriptlet fails, rpm still indicates the package is successfully installed.
# To get around this there is a touch file to record postinstall scriptlet
# failures. If the previous_rpm_pkg_install_failed touch file exists and
# contains this package's name, remove the touch file since we are now removing
# the package that had the postinstall failure and created the touch file.
#
#  These are expected to be set already:
#      PKG_NAME
#
remove_previous_rpm_pkg_install_failed_file ()
{
	if [ -f /tmp/previous_rpm_pkg_install_failed ] ; then
		failed_pkg_name=`sed -n '1p' /tmp/previous_rpm_pkg_install_failed`
		if [ "${failed_pkg_name}" = "${PKG_NAME}" ] ; then
			rm -f /tmp/previous_rpm_pkg_install_failed
		fi
	fi
}
#################### MAIN ####################

create_tracefile_name "${PKG_NAME}"

#start subshell for scriptlet
{ { { { (

${ECHO} "Running NB Client pre-remove scriptlet."

remove_previous_rpm_pkg_install_failed_file

${ECHO} "Stopping all running NetBackup daemons"

kill_status=0

if [ -f /usr/openv/netbackup/bin/bpps ] ; then

	#  Find bp.kill_all.  Could be in two possible spots depending
	#  on what's installed.
	wheres_kill_all=""
	if [ -f /usr/openv/netbackup/bin/bp.kill_all ] ; then
		wheres_kill_all=/usr/openv/netbackup/bin
	elif [ -f /usr/openv/netbackup/bin/goodies/bp.kill_all ] ; then
		wheres_kill_all=/usr/openv/netbackup/bin/goodies
	fi
	if [ -f ${wheres_kill_all}/bp.kill_all ] ; then
		${ECHO} "Terminating NetBackup and/or Media Manager processes."
		${wheres_kill_all}/bp.kill_all FORCEKILL SKIPBPCD > /dev/null 2>&1
		kill_status=$?
	else
		kill_status=1
	fi

	# Temporary Hack for Jasper release - Open User Story NBU-46489
	# Instead of aborting preremove on exit 1, the AIX/HPUX packager ignores
	# error codes/treats them as warnings and continues with file removal.
	# To ensure private certificates are securely removed along with the fileset removal,
	# need to override kill_status for AIX/HPUX and list offending processes for manual termination.
	if [ `uname -s` = "AIX" -o `uname -s` = "HP-UX" ]; then
		if [ ${kill_status} -ne 0 ] ; then
			${ECHO} "This script was not successful in terminating the
      following NetBackup processes and daemons. To prevent an inconsistent state of the system,
      manually terminate the processes that are still running on the `hostname` post uninstall:"
			/usr/openv/netbackup/bin/bpps -a | grep -v bpcd
			# Reset kill_status flag and ignore exit code.
			kill_status=0
		fi
	fi

	if [ ${kill_status} -ne 0 ] ; then
		${ECHO} "Cannot continue when NetBackup or Media Manager processes
 are running.  This script was not successful in terminating
 these processes.  Please terminate the following processes that
 are still running on `hostname` before trying this script again:"
		/usr/openv/netbackup/bin/bpps -a | grep -v bpcd
		exit 1
	else
		# Bring down the bpcd parent specifically and
		# leave the bpcd child up to handle the socket.
		# bp.kill_all is intentionally using SKIPBPCD.
		#
		# Suppress stderr in case we're pushing to an
		# old bpcd that doesn't know about -terminate yet.

		do_delete=0
		do_terminate=0
		if [ -f /usr/openv/netbackup/bp.conf ] ; then
			do_terminate=1
		elif [ -f /usr/openv/netbackup/bp.conf.rpmsave ] ; then
			cp /usr/openv/netbackup/bp.conf.rpmsave /usr/openv/netbackup/bp.conf
			do_delete=1
			do_terminate=1
		elif [ -f /usr/openv/netbackup/bp.conf.save ] ; then
			cp /usr/openv/netbackup/bp.conf.save /usr/openv/netbackup/bp.conf
			do_delete=1
			do_terminate=1
		fi

		# Remove all the certificates and key files securely using nbcertcmd.
		NBCERTCMD=/usr/openv/netbackup/bin/nbcertcmd
		${ECHO} "Removing certificates and key files."
		if [ -f ${NBCERTCMD} ] ; then
			NB_DELETE_ALLOWED='1'
			export NB_DELETE_ALLOWED
			${ECHO} "`${NBCERTCMD} -deleteAllCertificates -forceDelete -noPrompt 2>&1`"
			unset NB_DELETE_ALLOWED
		else
			${ECHO} "WARNING: File ${NBCERTCMD} doesn't exist hence certificates and key files cannot be removed."
		fi

		if [ ${do_terminate} -eq 1 ] ; then
			/usr/openv/netbackup/bin/bpcd -terminate 2>/dev/null
		else
			${ECHO} "WARNING: Cannot do bpcd -terminate because there is no bp.conf available."
		fi

		if [ ${do_delete} -eq 1 ]; then
			rm /usr/openv/netbackup/bp.conf
		fi
	fi
fi

#
# Clean up files left over from RedHat Linux installs unless it was deployed by VRTSnetbp
#
if [ -f /usr/openv/netbackup/bin/private/nbbsdtar ] ; then
	# See if nbbsdtar is owned by the VRTSnetbp package
	rpm -qf /usr/openv/netbackup/bin/private/nbbsdtar > /dev/null 2>&1
	if [ $? -ne 0 ] ; then
		rm -f /usr/openv/netbackup/bin/private/nbbsdtar
	fi
fi

# Check for the startup script also and if present delete it.

rm -f /etc/rc.netbackup.aix

# We need to capture the output from the subshell as well as the proper exit status 
) 2>&1; echo $? >&3; } | tee -i -a ${trace_file} >&4; } 3>&1; } | ( read xs; exit $xs; ) } 4>&1
# end subshell for scriptlet
subshell_status=$?

if [ -f ${trace_file} -a -d /usr/openv/tmp/ ] ; then
	cp ${trace_file} /usr/openv/tmp/
fi

exit ${subshell_status}