# 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.15 04/04/08 SMI
#
# Copyright (c) 2004 by Sun Microsystems, Inc.
# All rights reserved
#

write_sed_script() {
cat > /tmp/sed.scr.$$ << EOF
/hosts:[ 	]*nisplus[ 	][ 	]*dns/i\\
#You must also set up the /etc/resolv.conf file for DNS name\\
#server lookup.  See resolv.conf(4).
EOF
}

write_ipnodes_sed_script() {
cat > /tmp/sed.scr.$$ << EOF
# original hosts entry
/^hosts:/p
# commented-out ipnodes entry
s/^hosts:/#ipnodes:/
# add newline after commented-out ipnodes entry
/^#ipnodes:/a\\

# add "real" ipnodes entry before commented-out one
/^#ipnodes:/i\\
ipnodes:    files\\
# Uncomment the following line and comment out the above to resolve\\
# both IPv4 and IPv6 addresses from the ipnodes databases. Note that\\
# IPv4 addresses are searched in all of the ipnodes databases before\\
# searching the hosts databases. Before turning this option on, consult\\
# the Network Administration Guide for more details on using IPv6.
EOF
}

PATH=/usr/sadm/bin:$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 nsswitch $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
		grep '/etc/nsswitch.nisplus' $dst > /dev/null 2>&1
		if [ $? = 0 ] ; then
			grep 'You must also set up the /etc/resolv.conf' \
			    $dst > /dev/null 2>&1
			if [ $? != 0 ]; then
				write_sed_script
				sed -f /tmp/sed.scr.$$ $dst > /tmp/d.$$
				cp /tmp/d.$$ $dst
				rm -f /tmp/d.$$ /tmp/sed.scr.$$
			fi
		else
			egrep '/etc/nsswitch\.(nis|files)' $dst \
			    > /dev/null 2>&1
			if [ $? = 0 ] ; then
				grep '^sendmailvars:' $dst > /dev/null 2>&1
				if [ $? != 0 ] ; then
					echo 'sendmailvars:   files' >> $dst
				fi
			fi
		fi
		grep 'nametoaddr_libs of' $dst > /dev/null 2>&1
		if [ $? != 0 ]; then
			cat $dst | \
			sed -e 's/# "hosts:" and "services:" in this file are used only if the \/etc\/netconfig.*/# "hosts:" and "services:" in this file are used only if the/' \
			    -e 's/# file contains "switch.so" as a nametoaddr library for/# \/etc\/netconfig file has a "-" for nametoaddr_libs of/' > /tmp/d2.$$
			cp /tmp/d2.$$ $dst
			rm -f /tmp/d2.$$
		fi

		#
		# If the file contains "xfn" in the "hosts:" line, omit it.
		# The support for "xfn" has been removed
		#
		grep xfn $dst > /dev/null 2>&1
		if [ $? = 0 ]; then
			sed '/hosts:/s/xfn[     ]*//' $dst > /tmp/d.$$
			cp /tmp/d.$$ $dst
			rm -f /tmp/d.$$
		fi

		# If the file doesn't have a "printers" database entry, add
		# an appropriate entry.  Default to everything if we can't
		# figure out what is appropriate.
		grep printers: $dst > /dev/null 2>&1
		if [ $? != 0 ]; then
			PRINTERS="files nis nisplus xfn"
			egrep '/etc/nsswitch\.(dns|files)' $dst >/dev/null 2>&1
			if [ $? = 0 ] ; then
				PRINTERS="files"
			fi
			grep '/etc/nsswitch.nis' $dst >/dev/null 2>&1
			if [ $? = 0 ] ; then
				PRINTERS="files nis"
			fi
			grep '/etc/nsswitch.nisplus' $dst >/dev/null 2>&1
			if [ $? = 0 ] ; then
				PRINTERS="nisplus files xfn"
			fi
			echo "printers:	user ${PRINTERS}" >>$dst
		fi

		#
		# If not there, add "ipnodes: files" databases for unified host
		# addresses.  In all cases except the simple "hosts: files" one,
		# add commented-out "ipnodes" databases that match the "hosts"
		# one and a comment regarding the search order of the "ipnodes"
		# and "hosts" databases.  All of this is followed by a blank
		# line.
		#
		grep -s 'ipnodes:' $dst > /dev/null 2>&1
		if [ $? != 0 ] ; then
			grep -s '^hosts:[ 	]*files[ 	]*$' $dst \
				> /dev/null 2>&1
			if [ $? = 0 ]; then
				sed '/^hosts:/p;s/^hosts.*/ipnodes:    files/' \
					$dst > /tmp/d.$$
				if [ $? = 0 ]; then
					cp /tmp/d.$$ $dst
				fi
			else
				write_ipnodes_sed_script
				sed -f /tmp/sed.scr.$$ $dst > /tmp/d.$$
				if [ $? = 0 ]; then
					cp /tmp/d.$$ $dst
				fi
				rm -f /tmp/sed.scr.$$
			fi
			rm -f /tmp/d.$$
		fi

		#role-based access control databases
		ATTR="files nis nisplus"
		# Solaris 8 Beta refresh had audit_user, exec_attr and
		# user_attr entries for dns, files, nis and nis+. Remove
		# them if they are present
                sed -e '/^# role-based access control/d'\
                    -e '/^exec_attr:/d' \
                    -e '/^user_attr:/d' \
                    -e '/^# audit/d' \
                    -e '/^audit_user:/d' \
                    $dst > /tmp/d.$$
                cp /tmp/d.$$ $dst
                rm -f /tmp/d.$$
		# If the file doesn't have user attr database entries, add
		# appropriate entries.  Default to everything if we can't
		# figure out what is appropriate.
		for DB in auth_attr prof_attr
		do
			grep $DB: $dst > /dev/null 2>&1
			if [ $? != 0 ]; then
				egrep '/etc/nsswitch\.(dns|files)' $dst >\
				    /dev/null 2>&1
				if [ $? = 0 ] ; then
					ATTR="files"
				fi
				grep '/etc/nsswitch.nis' $dst >/dev/null 2>&1
				if [ $? = 0 ] ; then
					ATTR="files nis"
				fi
				grep '/etc/nsswitch.nisplus' $dst >/dev/null \
				    2>&1
				if [ $? = 0 ] ; then
					ATTR="files nisplus"
				fi
				echo "${DB}:	${ATTR}" >> $dst
			fi
		done

		# If the file doesn't have a "project" database entry, add
		# an appropriate entry.  Default to "files" if we can't
		# figure out what is appropriate.
		grep project: $dst > /dev/null 2>&1
		if [ $? != 0 ]; then
			PROJECT="files"
			egrep '/etc/nsswitch\.(dns|files)' $dst >/dev/null 2>&1
			if [ $? = 0 ] ; then
				PROJECT="files"
			fi
			grep '/etc/nsswitch.nis' $dst >/dev/null 2>&1
			if [ $? = 0 ] ; then
				PROJECT="files nis"
			fi
			grep '/etc/nsswitch.ldap' $dst >/dev/null 2>&1
			if [ $? = 0 ] ; then
				PROJECT="files ldap"
			fi
			echo "project:    ${PROJECT}" >>$dst
		fi
	fi
done
 
# If additional operations are required for this package, place
# those package-specific commands here.

#XXXSpecial_CommandsXXX#

exit 0
