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

PKG_NAME="VRTSnbpck"

# 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$
#
#  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
}

#----- $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
}
#----- $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

#################### MAIN ####################

create_tracefile_name "${PKG_NAME}"

#start subshell for scriptlet
{ { { { (

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

# Set umask to 022 to make sure files and directories
# are not created with world writable permissions.
umask 022

remove_previous_rpm_pkg_install_failed_file

# 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}