#
# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
#

# ident "@(#)144500_SUNWcsr_postinstall.sh 1.1     15/05/11 SMI"

### BEGIN: overlay 144500-19/SUNWcsr/install/postinstall
if [ "${PatchID}" = "144500-19" ] ; then

postinstall_merge()
{

#
# Copyright (c) 1991, 2014, Oracle and/or its affiliates. All rights reserved.
#
# ident	"@(#)postinstall	1.58	14/03/27 SMI"
#
BASEPREFIX=`echo $BASEDIR | sed "s/\//_/g"`
#
# Clear sysidtool which may have gone into maintenance due to a dependency
# cycle with milestone/single-user, when upgrading to a system that
# introduces milestone/sysconfig.
#
cat >> $BASEDIR/var/svc/profile/upgrade <<\_CLRSYSID
	/usr/sbin/svcadm clear svc:/system/sysidtool:net
	/usr/sbin/svcadm clear svc:/system/sysidtool:system
_CLRSYSID

if [ -f $BASEDIR/etc/inet/ipnodes.hostsmerge ]; then
	rm -f $BASEDIR/etc/inet/ipnodes.hostsmerge
fi

if [ -h $BASEDIR/var/mail.ABCsavedXYZ ] ; then
	rmdir $BASEDIR/var/mail/:saved >/dev/null 2>&1
	if [ $? = 0 ] ; then
		rmdir $BASEDIR/var/mail >/dev/null 2>&1
		if [ $? = 0 ] ; then
			mv $BASEDIR/var/mail.ABCsavedXYZ $BASEDIR/var/mail
		fi
	fi
	rm -fr $BASEDIR/var/mail.ABCsavedXYZ
fi

if [ -h $BASEDIR/var/news.ABCsavedXYZ ] ; then
	rmdir $BASEDIR/var/news >/dev/null 2>&1
	if [ $? = 0 ] ; then
		mv $BASEDIR/var/news.ABCsavedXYZ $BASEDIR/var/news
	fi
	rm -fr $BASEDIR/var/news.ABCsavedXYZ
fi

PMTAB=$BASEDIR/etc/saf/tcp/_pmtab
if [ -f $PMTAB ] ; then
	PMTAB_TMP="${PMTAB}_$$.new"
	sed -e 's/\\x00020ACE......../\\x00020ACE00000000/' \
	    -e 's/\\x00020203......../\\x0002020300000000/' $PMTAB > $PMTAB_TMP
	cp $PMTAB_TMP $PMTAB && rm -f $PMTAB_TMP
fi

AUTH_ATTR=$BASEDIR/etc/security/auth_attr
if [ -f $AUTH_ATTR ] ; then
	AUTH_ATTR_TMP="${AUTH_ATTR}_$$.new"
	sed '/^solaris\.\*/d' $AUTH_ATTR > $AUTH_ATTR_TMP
	cp $AUTH_ATTR_TMP $AUTH_ATTR && rm -f $AUTH_ATTR_TMP
fi

INITTAB=$BASEDIR/etc/inittab
if [ -f $INITTAB -a -n "$SUNW_PKG_INSTALL_ZONENAME" -a \
    "$SUNW_PKG_INSTALL_ZONENAME" != "global" ]; then
	INITTAB_TMP="${INITTAB}_$$.new"
	sed -e '/^ap:/d' -e '/^sp:/d' $INITTAB > $INITTAB_TMP
	cp $INITTAB_TMP $INITTAB && rm -f $INITTAB_TMP
fi

VFSTAB=$BASEDIR/etc/vfstab
if [ -f $VFSTAB -a -n "$SUNW_PKG_INSTALL_ZONENAME" -a \
    "$SUNW_PKG_INSTALL_ZONENAME" != "global" ]; then
	VFSTAB_TMP="${VFSTAB}_$$.new"
	sed '/^\/devices[	 ]/d' $VFSTAB > $VFSTAB_TMP
	cp $VFSTAB_TMP $VFSTAB && rm -f $VFSTAB_TMP
fi

#
# Get rid of obsolete BIND 8 server instance
#
cat >> $BASEDIR/var/svc/profile/upgrade <<\_DEL_BIND8
	obs_se=/usr/sbin/in.named
	cur_se=`svcprop -p start/exec svc:/network/dns/server 2>/dev/null`
	if [ "$obs_se" = "$cur_se" ]; then
		svcadm disable -s svc:/network/dns/server:default
		svccfg delete svc:/network/dns/server:default
		# If this was the only instance, delete the service also
		svcs network/dns/server >/dev/null 2>&1 || \
		    svccfg delete svc:/network/dns/server
	fi
_DEL_BIND8

# The ipsecalgs and policy services are delivered enabled.
# The ike and manual-key services are delivered disabled. The
# system administrator will need to enable them after creating
# the config file(s). If the system has been upgraded and the
# config files exist, enable the services on first boot.
# The file in /var/run indicates that this system had already been
# upgraded. The service will only be enabled on first upgrade.
IKEMANIFEST=`echo ike.xml"$BASEPREFIX" | cut -c 1-256`
if [ ! -f /var/run/${IKEMANIFEST} ]; then
	cat >> ${PKG_INSTALL_ROOT}/var/svc/profile/upgrade <<\_IPSECUPGRD
	if [ -f /etc/inet/ike/config ]; then
		/usr/sbin/svcadm enable svc:/network/ipsec/ike:default
	fi
_IPSECUPGRD
	else
		rm /var/run/${IKEMANIFEST}
fi
MANKEYMANIFEST=`echo manual-key.xml"$BASEPREFIX" | cut -c 1-256`
if [ ! -f /var/run/${MANKEYMANIFEST} ]; then
	cat >> ${PKG_INSTALL_ROOT}/var/svc/profile/upgrade <<\_IPSECUPGRD
	if [ -f /etc/inet/secret/ipseckeys ]; then
		/usr/sbin/svcadm enable svc:/network/ipsec/manual-key:default
	fi
_IPSECUPGRD
	else
		rm /var/run/${MANKEYMANIFEST}
fi

#
# svc:/network/rpc/keyserv is expected to be off on systems that don't
# set domainname.  On systems that do define a default domain, leave the
# setting as previously set.
#
cat >> $BASEDIR/var/svc/profile/upgrade <<\_CSVC_UPGRADE_2
	if [ ! -f /etc/defaultdomain ]; then
		svcadm disable network/rpc/keyserv
	fi
_CSVC_UPGRADE_2

if [ "$UPDATE" != yes ]; then
	#
	# On initial install, default to ns_files.xml.  The installer will
	# customize, if appropriate.
	#
	ln -s ns_files.xml $BASEDIR/var/svc/profile/name_service.xml
else
	if [ -f $BASEDIR/var/svc/profile/name_service.xml ]; then
		rm -f $BASEDIR/var/svc/profile/name_service.xml
	fi

	#
	# Associate name service profile, if none present.
	#
	grep '^[a-z_]\{1,\}[ 	]*:[^#]*\<ldap\>' $BASEDIR/etc/nsswitch.conf >/dev/null 2>&1
	is_ldap=$?
	grep '^[a-z_]\{1,\}[ 	]*:[^#]*\<nisplus\>' $BASEDIR/etc/nsswitch.conf >/dev/null 2>&1
	is_nisplus=$?
	grep '^[a-z_]\{1,\}[ 	]*:[^#]*\<nis\>' $BASEDIR/etc/nsswitch.conf >/dev/null 2>&1
	is_nis=$?
	egrep '^(ipnodes|hosts)[ 	]*(:dns|:[^#]*[ 	]dns)($|[ 	])' $BASEDIR/etc/nsswitch.conf >/dev/null 2>&1
	is_dns=$?

	if [ $is_ldap = 0 ]; then
		ns_profile=ns_ldap.xml
	elif [ $is_nisplus = 0  ]; then
		ns_profile=ns_nisplus.xml
	elif [ $is_nis = 0 ]; then
		ns_profile=ns_nis.xml
	elif [ $is_dns = 0 ]; then
		ns_profile=ns_dns.xml
	else
		ns_profile=ns_files.xml
	fi

	ln -s $ns_profile $BASEDIR/var/svc/profile/name_service.xml

	if [ $is_dns = 0 ]; then
		echo "/usr/sbin/svcadm enable network/dns/client" >> \
		    $BASEDIR/var/svc/profile/upgrade
	fi
fi

#
# Associate correct inetd services profile.
#
rm -f $BASEDIR/var/svc/profile/inetd_services.xml
if [ "$UPDATE" = yes ]; then
	ln -s inetd_upgrade.xml $BASEDIR/var/svc/profile/inetd_services.xml
	# Ensure inetd-upgrade is run post-upgrade
	echo "/usr/sbin/svcadm enable network/inetd-upgrade" >> \
	    $BASEDIR/var/svc/profile/upgrade
else
	ln -s inetd_generic.xml $BASEDIR/var/svc/profile/inetd_services.xml
fi

#
# /etc/svc/repository.db was previously packaged but it is now generated
# from one of the seed repositories and then updated by svccfg(1M).
# Therefore, removef(1M) is used to remove the packaging database entry
# although the repository itself is preserved.
#
/usr/sbin/removef $PKGINST /etc/svc/repository.db >/dev/null 2>&1
/usr/sbin/removef -f $PKGINST >/dev/null 2>&1

#
# The installer will take care of installs with generic_limited_net.
# For upgrade from older versions of S10, if we have a previous version of
# generic.xml, keep that.  For upgrade from pre-SMF systems, or initial
# install without generic_limited_net, symlink generic.xml to
# generic_open.xml.
#
PROFILEDIR=$BASEDIR/var/svc/profile
GENERICXML=${PROFILEDIR}/generic.xml
if [ ! -h $GENERICXML -a ! -f $GENERICXML ]; then
	ln -s ./generic_open.xml $GENERICXML
fi

# generic.xml is now dynamically maintained at install/upgrade time
# and no longer delivered by any package. We therefore removef(1) 
# any reference to it in the package database.
/usr/sbin/removef $PKGINST /var/svc/profile/generic.xml >/dev/null 2>&1
/usr/sbin/removef -f $PKGINST >/dev/null 2>&1

# Solaris audit's internal "enable/disable" state is maintained by
# c2audit; if c2audit accepts the auditconfig query, then auditing is
# enabled. If that is the case, then SMF should always enable auditd
# in the global zone and also in a non-global zone if perzone auditing
# is in use.
#
cat >> $BASEDIR/var/svc/profile/upgrade <<\_ENABLE_AUDIT
/usr/sbin/auditconfig -getcond 2> /dev/null
if [ $? -eq 0 ]; then
	if [ `/sbin/zonename` = global ]; then
		/usr/sbin/svcadm enable system/auditd
	else
		echo `/usr/sbin/auditconfig -getpolicy` | grep perzone > /dev/null
		if [ $? -eq 0 ]; then
			/usr/sbin/svcadm enable system/auditd
		fi
	fi
fi
_ENABLE_AUDIT

# if platform/sun4u/mpxio-upgrade service exists, then
# delete the service on boot after upgrading.
if [ "$UPDATE" = yes ]; then
	cat >> $BASEDIR/var/svc/profile/upgrade <<\_MPXIOUPGRD
	/usr/bin/svcs platform/sun4u/mpxio-upgrade > /dev/null 2>&1
	if [ "$?" = 0 ]; then
		/usr/sbin/svccfg delete svc:/platform/sun4u/mpxio-upgrade
	fi
_MPXIOUPGRD
fi

#
# Apply updates to the sun4v platform service profile after upgrading.
#
MACH=`/usr/bin/uname -m`
SVCPROFILE=$BASEDIR/var/svc/profile/platform_sun4v.xml
if [ "$MACH" = "sun4v" ]; then
	if [ -f $SVCPROFILE ]; then
		/usr/sbin/svccfg apply $SVCPROFILE
	fi
fi

return 0

}

fi
### END: overlay 144500-19/SUNWcsr/install/postinstall
