# This class action script copies the files being replaced
# into a package being constructed in $BUILD_DIR. This class
# action script is only appropriate for regular files that
# are installed by simply copying them into place.
#
# For special package objects such as edittable files, the patch
# producer must supply appropriate class action scripts.
#
# directory format options.
#
#       @(#)i.script 1.14 01/04/09 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

add_remount_lines()
{

	# add the new remount lines after the notify line

	source_file="$1"
	dest_file="$2"

	if grep "^remount" $dest_file > /dev/null 2>&1; then
		:
	else
		line_number=`grep -n "^insert dev/dsk" $dest_file | \
			tail -1 | awk -F: '{print $1}'`
		if [ -n "$line_number" ] ; then
			sed -e "$line_number a\\
remount dev/dsk/* user=root /usr/sbin/rmmount" $dest_file > /tmp/tmp.$$
			cp /tmp/tmp.$$ $dest_file
		fi

		line_number=`grep -n "^insert dev/diskette" $dest_file | \
			tail -1 | awk -F: '{print $1}'`
		if [ -n "$line_number" ] ; then
			sed -e "$line_number a\\
remount dev/diskette[0-9]/* user=root /usr/sbin/rmmount" $dest_file > \
				/tmp/tmp.$$
			cp /tmp/tmp.$$ $dest_file
		fi

		rm -f /tmp/tmp.$$
	fi
}

#
# update_use_line:
#
# takes three params:
#	1. dest file name;
#	2. new line to go after last 'use ...' line;
#	3. old line to delete;
#
# algorithm:
#	if the "$new" line is already in the file "$d", then return, else
#	find the line number of the last "use ..." line in the "$d" file
#	add the supplied "$new" line after the current last "use ..." line
#	if the "$old" line is in the file, remove it
#
update_use_line()
{

	d="$1"
	new="$2"
	old="$3"

	# check for the "$new line"
	if fgrep "$new" $d >/dev/null 2>&1 ; then
		return		# no work needs to be done
	fi

	# find last '^use ...' line
	line_no="`grep -n '^use' $d|tail -1|awk -F: '{print $1}'`"

	# add new line after last '^use ...' line
	sed -e "$line_no a\\
$new" $d > /tmp/tmp.$$

	# copy temp file back to dest file, removing old line (if present)
	grep -v "^$old" /tmp/tmp.$$ > $d
}

#
# update_notify_line
#
# takes one param:
#	1. dest file name;
#
# algorithm:
#	- find the current "notify ..." line
#	- if the current line ends with "-c" and the line has no 'use='
#	  clause, then
#		- change the "-c" to a "-p", and
#		- add a 'user=root' clause
#
update_notify_line()
{

	d="$1"


	# get the current notify line
	nl="`grep '^notify' $d`"

	# check for "-c"
	echo "$nl" | grep ' -c$' >/dev/null 2>&1
	if [ $? != 0 ] ; then
		# '-c' not found
		return		# not an orig line -- just leave it alone
	fi

	# check for not having a 'user=' clause
	echo "$nl" | grep ' user=' >/dev/null 2>&1
	if [ $? = 0 ] ; then
		# 'user=' found
		return		# not an orig line -- just leave it alone
	fi

	# update the notify line to have a 'user=root' and a '-p'
	sed -e "/^notify/s~/usr/lib~user=root /usr/lib~" \
	    -e "/^notify/s~-c~-p~" $d > /tmp/tmp.$$

	# move result back
	mv /tmp/tmp.$$ $d
}

update_label_sun_line()
{
        d="$1"

        # get the line and check if up to date
        if grep '^label sun .* rmscsi' $d >/dev/null 2>&1 ; then
                :
        elif grep '^label sun .* rmdisk' $d >/dev/null 2>&1 ; then
                :
        else
                #edit label sun line
                sed -e '/^label sun label_sun/ s/$/ rmscsi/' \
                        $dst >/tmp/tmp.$$
                cp /tmp/tmp.$$ $dst

                # clean up
                rm -f /tmp/*.$$
        fi
}

update_label_dos_line()
{
        d="$1"

        # get the line and check if up to date
        if grep '^label dos .* rmscsi' $d >/dev/null 2>&1 ; then
                :
        elif grep '^label dos .* rmdisk' $d >/dev/null 2>&1 ; then
                :
        else
                #edit label dos line
                sed -e '/^label dos label_dos/ s/$/ rmscsi/' \
                        $dst >/tmp/tmp.$$
                cp /tmp/tmp.$$ $dst

                # clean up
                rm -f /tmp/*.$$
        fi
}

update_unsafe_udfs_line()
{
        d="$1"

	LINE=`grep '^unsafe' $d`
	for word in $LINE
	do
		if [ $word = "udfs" ]
		then
			return
		fi
	done
	LINE="$LINE udfs"
	grep -v '^unsafe' $d >/tmp/tmp.$$
	echo $LINE >>/tmp/tmp.$$
	cp /tmp/tmp.$$ $d
}

PATH=/usr/sadm/bin:$PATH
PATH="/usr/bin:/usr/sbin:${PATH}"

ECHO="/usr/bin/echo"
SED="/usr/bin/sed"
PKGPROTO="/usr/bin/pkgproto"
EXPR="/usr/bin/expr"	# used by dirname
MKDIR="/usr/bin/mkdir"
CP="/usr/bin/cp"
RM="/usr/bin/rm"
MV="/usr/bin/mv"
KSH="/usr/bin/ksh"

recovery="no"
Pn=$$
procIdCtr=0

CMDS_USED="$KSH $ECHO $SED $PKGPROTO $EXPR $MKDIR $CP $RM $MV"
LIBS_USED=""

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

# Check to see if this is a patch installation retry.
if [ "$INTERRUPTION" = "yes" ]; then
	if [ -d "$PKG_INSTALL_ROOT/var/tmp/$SUNW_PATCHID.$PKGINST" ] || [ -d "$PATCH_BUILD_DIR/$SUNW_PATCHID.$PKGINST" ]; then
		recovery="yes"
	fi
fi

if [ -n "$PATCH_BUILD_DIR" -a -d "$PATCH_BUILD_DIR" ]; then
	BUILD_DIR="$PATCH_BUILD_DIR/$SUNW_PATCHID.$PKGINST"
else
	BUILD_DIR="$PKG_INSTALL_ROOT/var/tmp/$SUNW_PATCHID.$PKGINST"
fi

FILE_DIR=$BUILD_DIR/files
RELOC_DIR=$FILE_DIR/reloc
ROOT_DIR=$FILE_DIR/root
BO_Deletes=$FILE_DIR/deletes
PROGNAME=`basename $0`

if [ "$PATCH_PROGRESSIVE" = "true" ]; then
	PATCH_NO_UNDO="true"
fi

# Since this is generic, figure out the class.
Class=`echo $PROGNAME | nawk ' { print substr($0, 3)  }'`

# Since this is an update, $BASEDIR is guaranteed to be correct
BD=${BASEDIR:-/}

cd $BD

#
# First, figure out the dynamic libraries that can trip us up.
#
if [ -z "$PKG_INSTALL_ROOT" ]; then
	if [ -x /usr/bin/ldd ]; then
		LIB_LIST=`/usr/bin/ldd $CMDS_USED | sort -u | nawk '
			$1 ~ /\// { continue; }
			{ printf "%s ", $3 } '`
	else
		LIB_LIST="/usr/lib/libc.so.1 /usr/lib/libdl.so.1 /usr/lib/libw.so.1 /usr/lib/libintl.so.1 /usr/lib/libgen.so.1 /usr/lib/libadm.so.1 /usr/lib/libelf.so.1"
	fi
fi

#
# Now read the list of files in this class to be replaced. If the file
# is already in place, then this is a change and we need to copy it
# over to the build directory if undo is allowed. If it's a new entry
# (No $dst), then it goes in the deletes file for the backout package.
#

LD_LIB_DIR=$PKG_INSTALL_ROOT/var/tmp/LDLIB.$$

procIdCtr=0
while read src dst; do
	if [ "$PATCH_PROGRESSIVE" = "true" ]; then
		# If this is being used in an old-style patch, insert
		# the old-style script commands here.

		#XXXOld_CommandsXXX#
		echo >/dev/null	# dummy
	fi

	if [ "${PATCH_NO_UNDO}" != "true" ]; then
		#
		# Here we construct the path to the appropriate source
		# tree for the build. First we try to strip BASEDIR. If
		# there's no BASEDIR in the path, we presume that it is
		# absolute and construct the target as an absolute path
		# by stripping PKG_INSTALL_ROOT. FS_Path is the path to
		# the file on the file system (for deletion purposes).
		# Build_Path is the path to the object in the build
		# environment.
		#
		# The following rootPath variable accounts for a BASEDIR
		# that is used as a regular variable within the path and
		# not as a variable that a path needs to be relocated to.

		rootPath=`$ECHO $src | $SED s@"$INST_DATADIR/$PKGINST"@@ | \
			nawk -F/ '{print $2}'`
		if [ "$rootPath" = "root" ]; then
			FS_Path=$dst
		elif [ "$BD" = "/" ]; then
			FS_Path=`$ECHO $dst | $SED s@"$BD"@@`
		else
			FS_Path=`$ECHO $dst | $SED "s|$BD/||"`
		fi

		# If it's an absolute path the attempt to strip the
		# BASEDIR will have failed.
		if [ "$dst" = "$FS_Path" ]; then
			if [ -z "$PKG_INSTALL_ROOT" ]; then
				FS_Path=$dst
				Build_Path="$ROOT_DIR$dst"
			else
				Build_Path="$ROOT_DIR"`echo $dst | \
				    $SED "s|$PKG_INSTALL_ROOT||"`
				FS_Path=`echo $dst | \
				    $SED "s|$PKG_INSTALL_ROOT||"`
			fi
		else
			Build_Path="$RELOC_DIR/$FS_Path"
		fi

		if [ -f "$dst" ]; then	# If this is replacing something
			cd $FILE_DIR
			#
			# Construct the prototype file entry. We replace
			# the pointer to the filesystem object with the
			# build directory object.
			#
			echo "e voldconf $FS_Path=$Build_Path ? ? ?" >> \
				$BUILD_DIR/prototype
			# Now copy over the file
			if [ "$recovery" = "no" ]; then
				DirName=`dirname $Build_Path`
				$MKDIR -p $DirName
				$CP -p $dst $Build_Path
			else
				# If this file is already in the build area skip it
				if [ -f "$Build_Path" ]; then
					cd $BD
					continue
				else
					DirName=`dirname $Build_Path`
					if [ ! -d "$DirName" ]; then
						$MKDIR -p $DirName
					fi
					$CP -p $dst $Build_Path
				fi
			fi

			cd $BD
		else	# It's brand new
			$ECHO $FS_Path >> $BO_Deletes
		fi
	fi

	# If special processing is required for each src/dst pair,
	# add that here.
	#
	#XXXSpecial_CommandsXXX#
	#

	if [ ! -f $dst ] ; then
		cp $src $dst
	else
		# save the 'ident' line from the 'new' file
		grep '@(#)vold.conf' $src \
		    > /tmp/newident.$$ 2>/dev/null

		# save all but the 'ident' line from the old (orig) file
		grep -v '@(#)vold.conf' $dst \
		    > /tmp/rest.$$ 2>/dev/null

		# concatenate the new ident and the old data, replacing old
		cat /tmp/newident.$$ /tmp/rest.$$ > $dst

		# convert '/dev/fd' to '/dev/diskette' wherever found
		sed -e 's,/dev/fd\[,/dev/diskette[,' $dst > /tmp/tmp.$$
		cp /tmp/tmp.$$ $dst

		# get the new 'use cdrom ...' line
		new_use_line="`grep '^use cdrom drive' $src`"

		# ensure that default cdrom line is up to date
		update_use_line $dst "$new_use_line" \
		    'use cdrom drive /dev/dsk/c0t6 dev_cdrom.so cdrom0'

		# get the new 'use floppy ...' line
		new_use_line="`grep '^use floppy drive' $src`"

		# ensure that default floppy line is up to date
		update_use_line $dst "$new_use_line" \
		    'use floppy drive /dev/diskette dev_floppy.so floppy0'

		# get the new 'use rmscsi ...' line
		new_use_line="`grep '^# use rmscsi drive' $src`"

		# ensure that default floppy line is up to date
		update_use_line $dst "$new_use_line" \
		    '# there is no old rmscsi line'

		# update the volmissing line (if it hasn't changed)
		update_notify_line $dst

		# update the action lines
		sed -e '/^# Action/,/^$/ s,/vol\*/,,' $dst >/tmp/tmp.$$
		cp /tmp/tmp.$$ $dst

                # ensure that the label_dos and label_sun lines are current
                update_label_sun_line $dst 
                update_label_dos_line $dst
		# ensure unsafe line has udfs
		update_unsafe_udfs_line $dst

		# replace "rmscsi" with "rmdisk" everywhere in the file

		sed -e 's/rmscsi/rmdisk/g' $dst > /tmp/tmp.$$
		cp /tmp/tmp.$$ $dst

		# add the new "remount" lines

		add_remount_lines $src $dst

		# restore permissions if they changed
		chmod 0444 $dst
		chgrp bin $dst

		# clean up

		rm -f /tmp/*.$$

	fi
done

# If additional operations are required for this package, place
# those package-specific commands here.

#XXXSpecial_CommandsXXX#

exit 0
