#! /usr/bin/sh
#
# ident	"@(#)postbackout	1.5	00/10/19 SMI"
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All rights reserved.
#
# This is the postbackout shell script for patch 108885-05.
# The purpose of this postbackout script is to make sure that
# all scantool database files that were introduced by the
# addition and use of patch 108885-05, and subsequently
# propagated by autoconfig, are all properly removed now.

##############################################################
# CHANGE HISTORY (REVERSE CHRONOLOGICAL)
# 19-Oct-00 fly	Overhaul database verify, to reset everything
#		back to rev1 if the database needs cleaning.
# 18-Oct-00 fly	Add check/update of board.sigs* files.
# 17-Oct-00 fly	Update mode, owner, and group for *.config.
# 09-Oct-00 fly	BugId 4377005: Integrate the following changes:
#		- Automatically select this patch's backup file,
#		  data.pre_patch_${PatchNum}*.tar.Z, and backup
#		  from it.
#		- In database verification, if clean up is needed,
#		  also clean up the <platform>.config file.
# 14-Sep-00 fly	Initial creation.
##############################################################

# Set preliminary file and path name variables
RESTORED="was not found."
CUR_FILE="data.prepatch_${PatchNum}*.tar.Z"

if [ "${ROOTDIR}" = "/" ]; then
        SSP_DIR="/var/opt/SUNWssp"
else
        SSP_DIR="${ROOTDIR}/var/opt/SUNWssp"
fi

# Output to the user what is going on
echo "\n******************************************************************"
echo "As part of the patch removal process for patch ${PatchNum}, it"
echo "will be necessary to make sure that the scantool database is"
echo "restored from a compressed tar file backup that was created"
echo "during the prepatch installation phase of this patch.  If no such"
echo "backup compressed tar file exists, then the existing database at"
echo "${SSP_DIR}/data		will be cleaned up as needed."
echo "******************************************************************"

# Check the *data*tar.Z files for a desired backup, prompting with the
# most recent backup files first.

# BugId 4377005: Change the code to use only data.pre_patch_${PatchNum}*.tar.Z
DATA_TARZ_FILES=`ls -t ${SSP_DIR} | grep data.prepatch_${PatchNum} | grep tar.Z`

for CUR_FILE in ${DATA_TARZ_FILES} ; do

	# If the *data*tar.Z file is not a readable file, then bail
	if [ ! -f "${SSP_DIR}/${CUR_FILE}" ]; then
		RESTORED="was not readable."
		continue
	else
		if [ -d "${SSP_DIR}/data" ]; then
			# Put the orig data dir aside for now
			if [ -d "${SSP_DIR}/data.bu" ]; then
				rm -fr ${SSP_DIR}/data.bu
			fi
			mv ${SSP_DIR}/data ${SSP_DIR}/data.bu
		else
			echo "\nWARNING:"
			echo "The directory	\c"
			echo "${SSP_DIR}/data	was not found."
		fi

		# Test the backup for good compression
		if zcat ${SSP_DIR}/${CUR_FILE} > /dev/null ; then
			:
		else
			# uncompress failure encountered
			if [ -d "${SSP_DIR}/data.bu" ]; then
				mv ${SSP_DIR}/data.bu ${SSP_DIR}/data
			fi
			echo "\nCould not uncompress \c"
			echo "${SSP_DIR}/${CUR_FILE}\n"
			RESTORED="could not be uncompressed."
			continue
		fi

		# Now go ahead and zcat and tar extract
		echo "\nRestoring the data directory	${SSP_DIR}/data"
		echo "from the compressed tar file	${SSP_DIR}/${CUR_FILE}"

		if zcat ${SSP_DIR}/${CUR_FILE} | tar xfp - ; then
			rm -f ${SSP_DIR}/${CUR_FILE}
		else
			# tar failure encountered
			if [ -d "${SSP_DIR}/data.bu" ]; then
				if [ -d "${SSP_DIR}/data" ]; then
					rm -fr ${SSP_DIR}/data
				fi
				mv ${SSP_DIR}/data.bu ${SSP_DIR}/data
			fi
			echo "\nCould not tar extract from"
			echo "${SSP_DIR}/${CUR_FILE}\n"
			RESTORED="could not be tar extracted."

			continue
		fi

		RESTORED="ok"
		break
	fi
done

# If the user could not use a backup file, notify the user,
# as to the reason why not.  The existing database under
# /var/opt/SUNWssp/data will be cleaned up so that the patch
# can be removed.

if [ ! "ok" = "${RESTORED}" ]; then
	echo "\nNOTE:	${SSP_DIR}/data could not be restored from any"
	echo "	data.prepatch_${PatchNum}*tar.Z compressed tar file backup.\n"
	echo "	Therefore the existing database will be cleaned up."
fi

##############################################################
# Now verify the database does not contain any unwanted files
# that were added/propagated by the patch.  If any are found
# in procN.chips files, the database will need to be reset.

# Set new file and path name variables
SAP_TMPLT="BB3.chips"
MIR_TMPLT="BS1.chips"
SYS_DIR="${SSP_DIR}/data/Ultra-Enterprise-10000/common/board/sysboard"
CFG_DIR="${SSP_DIR}/data/Ultra-Enterprise-10000/config"

BSIG_FNS=`ls ${SYS_DIR} | grep board.sigs`
CFG_FNS=`ls ${CFG_DIR} | grep config`

# Begin verification
if [ -d "${SYS_DIR}" ]; then
	echo "\nVerifying current database files at:"
	echo "${SYS_DIR}\n"
else
	echo "\nDid not find directory:"
	echo "${SYS_DIR}"
	if [ "${RESTORED}" = "ok" ]; then
		echo "\nThis indicates that the backup compressed tar file:"
		echo "\n${SSP_DIR}/${CUR_FILE}\n"
		echo "was incomplete. The existing database will be cleaned"
		echo "up as needed."
	fi

	# Restore original data directory, then continue onward
	if [ -d "${SSP_DIR}/data.bu" ]; then
		mv ${SSP_DIR}/data.bu ${SSP_DIR}/data
	fi
fi

# BugId 4377005: Reset board.sigs* files to have only rev1 entries
reset_sigs_files()
{
	for BSIG_F in ${BSIG_FNS} ; do

		BSIG_PFN="${SYS_DIR}/${BSIG_F}"
		BE_MSG1="Please manually edit the file to remove all non-rev1"
		BE_MSG2="lines in the file."

		if [ ! -r ${BSIG_PFN} ] ; then
			echo "ERROR:\n${BSIG_PFN}"
			echo "\tis not readable and therefore cannot be edited."
			echo "\t${BE_MSG1}"
			echo "\t${BE_MSG2}"
			continue
		fi

		echo "\n${BSIG_PFN}" 
		# Remove all non-rev1 entries in the board.sigs* files
		if sed /"rev[2-99]"/d ${BSIG_PFN} |sed /"rev1[0-9]"/d \
						> /tmp/${BSIG_F}
		then
			mv -f /tmp/${BSIG_F} ${BSIG_PFN}
			chmod 640 ${BSIG_PFN}
			chown ssp:staff ${BSIG_PFN}
			echo "\thas been successfully reset to rev1."
		else
			echo "ERROR:\c"
			echo "\tCould not remove all non-rev1 entries in file."
			echo "\t${BE_MSG1}"
			echo "\t${BE_MSG2}"
		fi
	done
}

# BugId 4377005: Reset *.config files to have only rev1 entries
reset_config_files()
{

	for CFG_F in ${CFG_FNS} ; do
		CFG_PFN="${CFG_DIR}/${CFG_F}"

		echo "\n${CFG_PFN}" 
		# Remove all non-rev1 entries in the board.sigs* files
		if sed s/"rev[2-99]"/rev1/g ${CFG_PFN}|sed s/"rev1[0-9]"/rev1/g\
						> /tmp/${CFG_F}
		then
			mv -f /tmp/${CFG_F} ${CFG_PFN}
			chmod 640 ${CFG_PFN}
			chown ssp:staff ${CFG_PFN}
			echo "\thas been successfully reset to rev1."
		else
			echo "ERROR:\c"
			echo "\tCould not change all non-rev1 entries back \c"
			echo "to rev1."
			echo "\tPlease manually edit the file to change all \c"
			echo "non-rev1 "
			echo "\tentries back to rev1."
		fi
	done
}

reset_dir_revs()
{
	# Remove all non-rev1 directories
	for DIR_REV in `ls ${SYS_DIR} | grep "rev[2-99]"`
	do
		rm -rf ${SYS_DIR}/${DIR_REV}
	done

	# Remove the directories rev10 to rev19 separately
	for DIR_REV in `ls ${SYS_DIR} | grep "rev1[0-9]"`
	do
		rm -rf ${SYS_DIR}/${DIR_REV}
	done
	echo "\n${SYS_DIR}"
	echo "\thas been successfully reset to rev1."
}

# If we find "sombra" in a non-rev proc*.chips file, then
# the entire database will need to be reset to rev1.
if fgrep -s sombra ${SYS_DIR}/rev*/proc*.chips
then
	echo "\nWARNING:"
	echo "\tThe restore of ${SSP_DIR}/${CUR_FILE}"
	echo "\tresulted in the presence of certain files that require"
	echo "\tthat the scantool database be completely reset to rev1:"

	reset_sigs_files
	reset_config_files
	reset_dir_revs

	echo "\nWARNING:"
	echo "\tResetting the scantool database to rev1 requires that"
	echo "\tautoconfig be run on all system boards in the platform,"
	echo "\tat a time that is appropriate for each domain."

else
	# Remove and BS1.chips template files
	rm -f ${SYS_DIR}/rev*/${MIR_TMPLT}
fi

##############################################################

# It is now safe to remove the orig data dir
if [ -d "${SSP_DIR}/data.bu" ]; then
	rm -rf ${SSP_DIR}/data.bu
fi

echo "\nVerification completed.\n"
