#! /bin/sh
#
#ident	"@(#)postinstall	1.6	07/02/21 SMI"
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# post-installation script for admin apps.

write_locale_term_sed_script() {
cat > /tmp/sed.scr.$$ << EOF
/root password/a\\
1	# locale and term prompted for?
EOF
}

write_ext_net_sed_script() {
cat > /tmp/sed.scr.$$ <<EOF
/System is on a network/a\\
1	# Extended network information gathered?
EOF
}

write_sec_policy_sed_script() {
cat > /tmp/sed.scr.$$ <<EOF
/locale and term prompted for/a\\
0	# security policy in place
EOF
}

touch ${BASEDIR}/etc/.UNCONFIGURED

STATEFILE=${BASEDIR}/etc/.sysIDtool.state

install_nfs4_domain_line() {
	cat > /tmp/sed.scr.$$ <<-EOF
		/security policy in place/a\\
		$1	# NFSv4 domain configured
	EOF

	sed -f /tmp/sed.scr.$$ ${STATEFILE} > /tmp/sed.tmp.$$
	mv /tmp/sed.tmp.$$ ${STATEFILE}
	rm -f /tmp/sed.scr.$$
}

# We need to update the .sysIDtool.state file on the system being
# upgraded to reflect the current number fields.  If we don't,
# sysid will reject it as illegal on boot after the upgrade.  It
# will then ask all of the questions again.
if [ -f $STATEFILE ] ; then

	grep 'locale and term prompted for' $STATEFILE >/dev/null 2>&1
	if [ $? != 0 ] ; then
		# Locale line wasn't found, so add our own
		write_locale_term_sed_script
		sed -f /tmp/sed.scr.$$ $STATEFILE >/tmp/sed.tmp.$$
		mv /tmp/sed.tmp.$$ $STATEFILE
		rm -f /tmp/sed.scr.$$
		echo "Added locale and term to sysid state file"
	fi

	grep 'security policy in place' $STATEFILE >/dev/null 2>&1
	if [ $? != 0 ] ; then
		# Security policy line wasn't found, so add our own
		write_sec_policy_sed_script
		sed -f /tmp/sed.scr.$$ $STATEFILE >/tmp/sed.tmp.$$
		mv /tmp/sed.tmp.$$ $STATEFILE
		rm -f /tmp/sed.scr.$$
		echo "Added security policy to sysid state file"
	fi

	# Fix for 4283281 - make sure there aren't duplicate Extended lines
	if [ `grep 'Extended network information gathered' $STATEFILE | \
				    wc -l |awk '{print $1;}'` -gt 1 ] ; then
		uniq <$STATEFILE >/tmp/state.tmp.$$
		mv /tmp/state.tmp.$$ $STATEFILE
		echo "Removed duplicate extended network lines"
	fi

	grep 'Extended network information gathered' $STATEFILE >/dev/null 2>&1
	if [ $? != 0 ] ; then
		# Extended network line wasn't found, so add our own
		write_ext_net_sed_script
		sed -f /tmp/sed.scr.$$ $STATEFILE >/tmp/sed.tmp.$$
		mv /tmp/sed.tmp.$$ $STATEFILE
		rm -f /tmp/sed.scr.$$
		echo "Added extended network to sysid state file"

		# We also want to copy down the answer from sysid, if there
		# is one.  Our criteria are as follows:
		#  1. We must be installing to an alternate root
		#  2. Both /.tmp_proto (d) and /etc/transfer_list (h) must exist
		# We copy all /etc/hostname6.* files from the transfer list
		# (if any).
		if [ "${BASEDIR}" != "/" -a -d /.tmp_proto -a \
		     -h /etc/transfer_list ] ; then
			FILES=`awk '{print $1;}' /etc/transfer_list | sort | \
			    uniq |grep '^/etc/hostname6\.' 2>/dev/null`
			if [ $? = 0 ] ; then
				# Match
				for file in $FILES ; do
					echo "Copying ${file} to ${BASEDIR}"
					cp /tmp/root/${file} ${BASEDIR}/${file}
				done
			fi
		fi
	fi

	#
	# Add NFSv4 line to ${STATEFILE} only if it doesn't already
	# have it. If this is an upgrade from s10, then there'll be
	# no need to prompt the user, so add line as "configured"
	#
	NFS4_STATE_FILE=${BASEDIR}/etc/.NFS4inst_state.domain
	grep 'NFSv4 domain configured' $STATEFILE >/dev/null 2>&1
	if [ $? != 0 ]; then
		#
		# NFSv4 line does not exist in ${STATEFILE}.
		#
		if [ -f ${NFS4_STATE_FILE} ]; then
			install_nfs4_domain_line 1
			echo "Migrated NFSv4 domain config to sysid state file"
		else
			install_nfs4_domain_line 0
			echo "Added NFSv4 domain config to sysid state file"
		fi
	fi
	[ -f ${NFS4_STATE_FILE} ] && rm -f ${NFS4_STATE_FILE};

	# Update the sysidcfg file on the system with the one we just used
	# (if any) for the upgrade
	if [ "${BASEDIR}" != "/" -a -d /.tmp_proto -a \
	     -h /etc/transfer_list ] ; then
		# We assume we're in the miniroot
		if [ -f ${BASEDIR}/etc/sysidcfg ] ; then
			echo "Removing existing sysidcfg file"
			rm ${BASEDIR}/etc/sysidcfg
		fi

		if [ -f /etc/sysidcfg ] ; then
			echo "Copying miniroot sysidcfg to ${BASEDIR}"
			cp /etc/sysidcfg ${BASEDIR}/etc/sysidcfg
		fi
	fi
fi

exit 0
