#!/bin/ksh

#ident	"$(#)prepatch.template		1.8	09/06/05 SMI"	from 1.8 basis

# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

# This is an automatically generated prepatch script exclusively for
# Interim Diagnostic/Relief.
# DO NOT MANUALLY EDIT THIS SCRIPT.


cleanup () {
	[[ -d "$WORKDIR" ]] && \
		rm -fr $WORKDIR
	exit $1
}

times_up() {
	main
}

check_platform() {
	idr_platforms=""
	[[ -z "$idr_platforms" ]] && return 1
	platform=$(uname -i)
	mach=$(uname -m)
	for i in $idr_platforms; do
		[[ "$platform" = "$i" ]] && return 1
		[[ "$mach" = "$i" ]] && return 1
	done
	return 0
}

main () {

export LC_ALL=C

# If the -d (don't save backout data) option has been provided to patchadd,
# halt the installation. All IDR's must not use -d.

[[ "$saveold" = "no" ]] && \
	echo "$PatchNum must be able to be backed out. The -d option is invalid for IDR's. If file system space is low, please use the -B option. See patchadd(1M) for details." && \
	cleanup 1

check_platform && \
	echo "$PatchNum may only be installed on one of the following hardware classes or platforms: " && \
	cleanup 1





# Check that the required files exist before installing the IDR.
[[ -f "$WORKDIR/IDR_files.$$" ]] && \
	echo "$WORKDIR/IDR_files.$$ exists." && \
	cleanup 1

cat > $WORKDIR/IDR_files.$$ << EOF
1 f none /platform/i86pc/kernel/unix 0755 root sys 950028 17175 1260369303
1 f none /platform/i86pc/kernel/amd64/unix 0755 root sys 1279088 42425 1260369303
EOF

# Logic for checking for files
# Verify ftype as either e|f|v
# Check for existence of file
# Check the attributes, size and sum

PKGCOND=/usr/bin/pkgcond
is_srngz=99

if [ -x $PKGCOND ]; then
     $PKGCOND -v is_sparse_root_nonglobal_zone > /dev/null 2>&1
     is_srngz=$?
fi

# if this is a sparse non-global zone, then the act of installing
# in the global zone will have changed the checksum.
# Then the patch install failes for the local zones, which is bad.

if [ $is_srngz -eq 0 ] ; then
        exit 0
fi

while read line; do
    file=$(echo $line | nawk '{print $4}')
    # Do the checks only if the file exists on the system
    if [[ -f "$ROOTDIR$file" ]]; then
	ftype=$(echo $line | nawk '{print $2}')
	if [[ "$ftype" != "f" && "$ftype" != "e" && "$ftype" != "v" ]]; then
		echo "An ftype of $ftype is not supported"
		cleanup 1
	fi
	# pkgchk eats commas passed on the command line
	# Save the file to a temp. file and invoke pkgchk on it.
	echo $file > $WORKDIR/IDR_FCS_FILE.$$
	pkgchk -R $ROOTDIR -li $WORKDIR/IDR_FCS_FILE.$$ > $PKGCHK_RES 2>&1
	if [[ $? != 0 ]]; then
		echo "pkgchk can't find $ROOTDIR$file"
		cleanup 1
	fi
	rm $WORKDIR/IDR_FCS_FILE.$$
	sz=$(grep "^Expected file size" $PKGCHK_RES | nawk -F: '{print $2}')
	sum=$(grep "^Expected sum" $PKGCHK_RES | nawk -F: '{print $2}')
	f_sz=$(echo $line | nawk '{print $8}')
	f_sum=$(echo $line | nawk '{print $9}')

	# Remove white space
	sz=$(echo $sz);sum=$(echo $sum);f_sz=$(echo $f_sz);f_sum=$(echo $f_sum);
	
	if [[ "$sz" != "$f_sz" ]]; then
		echo "The size of $ROOTDIR$file is not what is"
		echo "expected by this patch!"
		echo "Expected $f_sz (bytes)"
		echo "Found: $sz (bytes)"
		cleanup 1
	fi
	if [[ "$sum" != "$f_sum" ]]; then
		echo "The sum of the $ROOTDIR$file is not what is"
		echo "expected by this patch!"
		echo "Expected $f_sum"
		echo "Found: $sum"
		cleanup 1
	fi
	rm -f $PKGCHK_RES
    fi
done < $WORKDIR/IDR_files.$$




exit 0
}

SLEEP_TIME=60
PATH=/usr/sadm/bin:/usr/sbin:/usr/bin:$PATH
WORKDIR=/tmp/IDR_$$
CONTENTS=$ROOTDIR/var/sadm/install/contents
PKGCHK_RES=$WORKDIR/pkgchk.$$
export PATH

if [[ ! -d $WORKDIR ]]; then
	mkdir $WORKDIR
else
	echo "Can't continue. $WORKDIR already exists."
	cleanup 1
fi

# Print the IDR disclaimer. Not to be localized.

[[ ! -f ./copyright ]] && \
	echo "Fatal error, can't find copyright file" && \
	cleanup 1

cat ./copyright

trap 'times_up' VTALRM
( sleep $SLEEP_TIME ; kill -VTALRM $$ ) &
child_pid=$!
sleep_pid=`ps -ef | grep $!| grep sleep | awk '{print $2}'`

#
# Asks user is they would like to continue.  It will timeout in
# SLEEP_TIME and installation will continue if no response is given.
#

echo "Do you wish to continue this installation {yes or no} [yes]? \\c"
echo "\n(by default, installation will continue in $SLEEP_TIME seconds)"

read Response
while [ 1 ]; do
	case $Response in

	n | no | N | NO | No)
		kill -9 $sleep_pid
		kill -9 $child_pid
		cleanup 1 ;;
	"" | y | yes | Y | Yes | YES)
		kill -9 $sleep_pid
		kill -9 $child_pid
		main ;;
	*)  echo "yes or no please. \\c"
		read Response ;;
	esac
done

exit 0
