#!/bin/sh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)i.preserve	1.4	04/04/22 SMI"
#
# i.preserve script for PatchPro
#

PATH=/usr/sadm/bin:${PATH}	# use admin utilities

#
# Pick up user modifications to PatchPro 2.x configuration if PatchPro
# config files are present on the system. This is done only when PM2
# is initially installed on the system.
#
# NOTE: Currently the following files are left behind in the file system
# after the upgrade so that user may refer to them later. For exmmple,
# patchpro.conf may have NWS paltform specific configuration which is
# not merged during the upgrade. Basically this script doesn't consume all of
# the config information from these files but only the core PatchPro parameters
# that are configurable in PM2.
#
#	/opt/SUNWppro/etc/patchpro.conf
#	/opt/SUNWppro/lib/.sunsolvepw
#	/opt/SUNWppro/lib/.proxypw
#	/etc/opt/SUNWppro/etc/patchpro.conf
#
get_patchpro_config()
{
	if [ ! -f $pp2xcfg ]; then
		return 0
	fi

	# patchpro.download.directory
	#
	#      NAME	                                DEFAULT
	# PM2: patchpro.download.directory              /var/sadm/spool
	# 2.1: patchpro.patch.download.directory        /var/spool/pkg/patchpro
	# 2.2: patchpro.patch.download.directory        /var/sadm/spool
	# 2.3: patchpro.patch.download.directory        /var/sadm/spool

	val=`sed -n "s=^patchpro.patch.download.directory\===p" "$pp2xcfg"`
	if [ "$val" != "/var/sadm/spool" -a "$val" != "" ]; then
		echo "patchpro.download.directory=$val" >>$pm2cfg
	fi

	# patchpro.backout.directory
	#
	#      NAME	                             DEFAULT
	# PM2: patchpro.backout.directory            ""
	# 2.1: patchpro.patch.backout.directory      ""
	# 2.2: patchpro.patch.backout.directory      ""
	# 2.3: patchpro.patch.backout.directory      ""

	val=`sed -n "s=^patchpro.patch.backout.directory\===p" "$pp2xcfg"`
	if [ "$val" != "" ]; then
		echo "patchpro.backout.directory=$val" >>$pm2cfg
	fi

	# patchpro.proxy.host
	#
	#      NAME	                          DEFAULT
	# PM2: patchpro.proxy.host                ""
	# 2.1: patchpro.proxyserver.host          ""
	# 2.2: patchpro.proxyserver.host          ""
	# 2.3: patchpro.proxyserver.host          ""

	val=`sed -n "s=^patchpro.proxyserver.host\===p" "$pp2xcfg"`
	if [ "$val" != "" ]; then
		echo "patchpro.proxy.host=$val" >>$pm2cfg
	fi

	# patchpro.proxy.port
	#
	#      NAME	                          DEFAULT
	# PM2: patchpro.proxy.port                8080
	# 2.1: patchpro.proxyserver.port          ""
	# 2.2: patchpro.proxyserver.port          ""
	# 2.3: patchpro.proxyserver.port          ""

	val=`sed -n "s=^patchpro.proxyserver.port\===p" "$pp2xcfg"`
	if [ "$val" != "" -a "$val" != "8080" ]; then
		echo "patchpro.proxy.port=$val" >>$pm2cfg
	fi

	# patchpro.sun.user
	#
	#      NAME	                          DEFAULT
	# PM2: patchpro.sun.user                  ""
	# 2.1: patchpro.sunsolve.identity         ""
	# 2.2: patchpro.sunsolve.identity         ""
	# 2.3: patchpro.sunsolve.identity         ""

	val=`sed -n "s=^patchpro.sunsolve.identity\===p" "$pp2xcfg"`
	if [ "$val" != "" ]; then
		echo "patchpro.sun.user=$val" >>$pm2cfg
	fi

	# patchpro.proxy.user
	#
	#      NAME	                          DEFAULT
	# PM2: patchpro.proxy.user                ""
	# 2.1: patchpro.proxy.identity            ""
	# 2.2: patchpro.proxy.identity            ""
	# 2.3: patchpro.proxy.identity            ""

	val=`sed -n "s=^patchpro.proxy.identity\===p" "$pp2xcfg"`
	if [ "$val" != "" ]; then
		echo "patchpro.proxy.user=$val" >>$pm2cfg
	fi

	# patchpro.sun.passwd
	#
	#      NAME	                          DEFAULT
	# PM2: patchpro.sun.passwd                  ""
	# 2.1: /opt/SUNWppro/lib/.sunsolvepw
	# 2.2: /opt/SUNWppro/lib/.sunsolvepw
	# 2.3: /opt/SUNWppro/lib/.sunsolvepw

	if [ -f $pp2spw ]; then
	    val=`cat $pp2spw`
	    if [ "$val" != "" ]; then
		if [ ! -f $pm2secret ]; then
			# create the secret.conf with right permissions
			create_pm2secret
		fi
		echo "patchpro.sun.passwd=$val" >>$pm2secret
	    fi
	fi

	# patchpro.proxy.passwd
	#
	#      NAME	                          DEFAULT
	# PM2: patchpro.proxy.passwd                  ""
	# 2.1: /opt/SUNWppro/lib/.proxypw
	# 2.2: /opt/SUNWppro/lib/.proxypw
	# 2.3: /opt/SUNWppro/lib/.proxypw

	if [ -f $pp2ppw ]; then
	    val=`cat $pp2ppw`
	    if [ "$val" != "" ]; then
		if [ ! -f $pm2secret ]; then
			# create the secret.conf with right permissions
			create_pm2secret
		fi
		echo "patchpro.proxy.passwd=$val" >>$pm2secret
	    fi
	fi

	# patchpro.patch.source
	#
	#      NAME	DEFAULT
	# PM2: patchpro.patch.source
	#		https://updateserver.sun.com/solaris/
	# 2.1: patchpro.patch.server.url
	#	https://americas.patchmanager.sun.com/patchmanager/
	# 2.2: patchpro.database.source.url
	#	https://patchpro.sun.com/servlet/com.sun.patchpro.server.PatchProServerServlet/
	# 2.3: patchpro.database.source.url
	#	https://patchpro.sun.com/psvr/servlet/PatchServer

	val=`sed -n "s=^patchpro.patch.server.url\===p" "$pp2xcfg"`
	url21="https://americas.patchmanager.sun.com/patchmanager/"
	url22="https://patchpro.sun.com/servlet/com.sun.patchpro.server.PatchProServerServlet/"
	url23="https://patchpro.sun.com/psvr/servlet/PatchServer"
	if [ "$val" != "$url21" -a \
	     "$val" != "$url22" -a \
	     "$val" != "$url23" ]; then
		echo "patchpro.patch.source=$val" >>$pm2cfg
	fi

	# patchpro.patchset
	#
	# Note: For PatchPro 2.1 and 2.2 the database name is part
	# of an URL.
	#
	#      NAME     	DEFAULT
	# PM2: patchpro.patchset         
	#			patchdb
	# 2.1: patchpro.database.source.url (basename part of it) 
	#			http://patchpro.sun.com/expert/patchprodb.zip
	# 2.2: patchpro.database.source.url (basename part of it)
	#			http://patchpro.sun.com/expert/patchprodb.zip
	# 2.3: patchpro.database.name
	#			patchprodb

	val=`sed -n "s=^patchpro.database.source.url\===p" "$pp2xcfg"`
	if [ "$val" != "" ]; then
		name=`basename $val`
		if [ "$name" != "patchprodb.zip" ]; then
			n=`echo $name|cut -f1 -d.`
			echo "patchpro.patchset=$n" >>$pm2cfg
		fi
	else
		# it must be 2.3 version
		name=`sed -n "s=^patchpro.database.name\===p" "$pp2xcfg"`
		if [ "$name" != "" -a \
		     "$name" != "patchprodb" ]; then
			echo "patchpro.patchset=$name" >>$pm2cfg
		fi
	fi

	# patchpro.install.types
	#
	# Note: Ignore types 'clientroot', 'clientusr' and 'noncontract'
	# if they are set.
	#
	#      NAME     DEFAULT
	# PM2: patchpro.install.types
	#		rebootafter:reconfigafter:standard
	# 2.1: patchpro.patch.interactive.patchtype   ""
	#		clientroot:clientusr:noncontract:rebootafter:\
	#		reconfigafter:standard
	# 2.2: patchpro.patch.interactive.patchtype   ""
	#		clientroot:clientusr:noncontract:rebootafter:\
	#		reconfigafter:standard
	# 2.3: patchpro.patch.interactive.patchtype   ""
	#		clientroot:clientusr:noncontract:rebootafter:\
	#		reconfigafter:standard

	val=`sed -n "s=^patchpro.patch.interactive.patchtype\===p" "$pp2xcfg"`
	deflist="clientroot:clientusr:noncontract:rebootafter:reconfigafter:standard"
	if [ "$val" != "$deflist" ]; then
		list=`echo $val|sed "s/:/ /g"`
		newlist=""
		for i in $list
		do
			case "$i" in
				clientroot)
					# ignore it
					;;
				clientusr)
					# ignore it
					;;
				noncontract)
					# ignore it
					;;
				*)	if [  "$newlist" = "" ]; then
						newlist=$i
				   	else
						newlist=${newlist}:$i
				   	fi
			   		;;
			
			esac
		done
		echo "patchpro.install.types=$newlist" >>$pm2cfg
	fi

	# remove the saved copy of patchpro.conf file
	rm -f $PKGSAV/patchpro.conf

	return 0
}

#
# Create secret.conf file if it doesn't exist. Normally this file is
# installed by pkgadd of SUNWppror but we may need to create it
# depending on which file (patchpro.conf, secret.conf) is created first
# and we need to update them with user customizations from PatchPro 2.x
# config file.
# 
create_pm2secret()
{
	if [ -f $pm2secret ]; then
		return 0
	fi
	echo "# THIS FILE IS WRITTEN BY A PROGRAM." >> $pm2secret
	echo "# Normally you should change the values in this file using" >> $pm2secret
	echo "# the Patch Manager commands (see smpatch (1M))." >> $pm2secret
	echo "#" >> $pm2secret
	echo "# This file is confidential. It is only readable by root." >> $pm2secret
	echo "#" >> $pm2secret
	chmod 600 $pm2secret
	chown root:sys $pm2secret

	return 0
}


# config file locations
pp2xcfg=$PKG_INSTALL_ROOT/etc/opt/SUNWppro/etc/patchpro.conf
if [ ! -f $pp2xcfg ]; then
	pp2xcfg=$PKG_INSTALL_ROOT/opt/SUNWppro/etc/patchpro.conf
fi
pm2cfg=$PKG_INSTALL_ROOT/etc/patch/patch.conf
pm2secret=$PKG_INSTALL_ROOT/etc/patch/secret.conf
pp2spw=$PKG_INSTALL_ROOT/opt/SUNWppro/lib/.sunsolvepw
pp2ppw=$PKG_INSTALL_ROOT/opt/SUNWppro/lib/.proxypw

#
# If the destination file already exists (which means it is a normal
# upgrade from S10 to S10+) then we do nothing. Otherwise, we install
# the default file and then add any user customizations from any
# saved patchpro configuration file.
#
while read src dst; do
	if [ ! -f $dst ]; then
		/usr/bin/cat $src > $dst.tmp || exit 1
		/usr/bin/mv $dst.tmp $dst
		#
		# If we are installing patch.conf file for the first
		# time then update it based on any existing PatchPro 2.x
		# installation configuration.
		#
		name=`basename $dst`
		if [ "$name" = "patch.conf" ]; then
			get_patchpro_config
		fi
	fi
done

exit 0
