# This script creates the backout package for a patch package
#
# directory format options.
#
# @(#) postinstall 1.13 01/01/12 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

# Description:
#       Set the TYPE parameter for the remote file
#
# Parameters:
#       none
#
# Globals set:
#	TYPE

set_TYPE_parameter () {
	if [ ${PATCH_UNDO_ARCHIVE:?????} = "/dev" ]; then
		# handle device specific stuff
		TYPE="removable"
	else
		TYPE="filesystem"
	fi
}

#
# Description:
#       Build the remote file that points to the backout data
#
# Parameters:
#       $1:	the un/compressed undo archive
#
# Globals set:
#	UNDO, STATE

build_remote_file () {
	remote_path=$PKGSAV/$SUNW_PATCHID/remote
	set_TYPE_parameter
	STATE="active"

	if [ $1 = "undo" ]; then
		UNDO="undo"
	else
		UNDO="undo.Z"
	fi

	cat > $remote_path << EOF
# Backout data stored remotely
TYPE=$TYPE
FIND_AT=$ARCHIVE_DIR/$UNDO
STATE=$STATE
EOF
}

PATH=/usr/sadm/bin:$PATH

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
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

if [ ! -n "$PATCH_UNDO_ARCHIVE" ]; then
	PATCH_UNDO_ARCHIVE="none"
fi

FILE_DIR=$BUILD_DIR/files
RELOC_DIR=$FILE_DIR/reloc
ROOT_DIR=$FILE_DIR/root
BO_Deletes=$FILE_DIR/deletes
THIS_DIR=`dirname $0`
PROTO_FILE=$BUILD_DIR/prototype
TEMP_REMOTE=$PKGSAV/$SUNW_PATCHID/temp

if [ "$PATCH_PROGRESSIVE" = "true" ]; then
        # remove the scripts that are left behind
        install_scripts=`dirname $0`
        rm $install_scripts/checkinstall $install_scripts/patch_checkinstall $install_scripts/patch_postinstall

	# If this is being used in an old-style patch, insert
	# the old-style script commands here.

	#XXXOld_CommandsXXX#

	exit 0
fi

#
# At this point we either have a deletes file or we don't. If we do,
# we create a prototype entry.
#
if [ -f $BO_Deletes ]; then
	echo "i deletes=$BO_Deletes" >> $BUILD_DIR/prototype
fi

#
# Now delete everything in the deletes list after transferring
# the file to the backout package and the entry to the prototype
# file. Remember that the pkgmap will get the CLIENT_BASEDIR path
# but we have to actually get at it using the BASEDIR path. Also
# remember that removef will import our PKG_INSTALL_ROOT
#
Our_Deletes=$THIS_DIR/deletes
if [ -f $Our_Deletes ]; then
	cd $BASEDIR

	cat $Our_Deletes | while read path; do
		Reg_File=0

		if valpath -l $path; then
			Client_Path="$CLIENT_BASEDIR/$path"
			Build_Path="$RELOC_DIR/$path"
			Proto_Path=$BASEDIR/$path
		else	# It's an absolute path
			Client_Path=$path
			Build_Path="$ROOT_DIR$path"
			Proto_Path=$PKG_INSTALL_ROOT$path
		fi

		# If BASEDIR/CLIENTBASEDIR = "/", then the previous prepends
		# an extra / i.e. //. The sed command later can't find a
		# Proto_Path with // and therefore will not substitute the
		# correct build_Path resulting in the backout pkg not being
		# created.

		if [ "$CLIENT_BASEDIR" = "/" ]; then
			Client_Path=`echo $Client_Path | sed 's|^\/\/|\/|'`
			Proto_Path=`echo $Proto_Path | sed 's|^\/\/|\/|'`
		fi
			
		# Note: If the file isn't really there, pkgproto
		# doesn't write anything but displays an error
		# so check for the file before processing.

		if [ -f "$Proto_Path" ]; then
			LINE=`pkgproto $Proto_Path=$path`
		else
			continue
		fi

		ftype=`echo $LINE | nawk '{ print $1 }'`
		if [ "$ftype" = "f" ]; then
			Reg_File=1
		fi

		if [ $Reg_File = 1 ]; then
			# Add source file to the prototype entry
			if [ "$Proto_Path" = "$path" ]; then
				LINE=`echo $LINE | sed -e "s|$Proto_Path|$Build_Path|2"`
			else
				LINE=`echo $LINE | sed -e "s|$Proto_Path|$Build_Path|"`
			fi

			DirName=`dirname $Build_Path`
			# make room in the build tree
			mkdir -p $DirName
			cp -p $Proto_Path $Build_Path
		fi

		# Insert it into the prototype file
		echo $LINE 1>>$PROTO_FILE 2>/dev/null

		# Remove the file only if it's OK'd by removef
		rm `removef $PKGINST $Client_Path` 1>/dev/null 2>&1
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi

#
# Unless specifically denied, make the backout package.
#
if [ "$PATCH_NO_UNDO" != "true" ]; then
	cd $BUILD_DIR	# We have to build from here.

	if [ "$PATCH_UNDO_ARCHIVE" != "none" ]; then
		STAGE_DIR="$PATCH_UNDO_ARCHIVE"
		ARCHIVE_DIR="$PATCH_UNDO_ARCHIVE/$SUNW_PATCHID/$PKGINST"
		mkdir -p $ARCHIVE_DIR
		mkdir -p $PKGSAV/$SUNW_PATCHID
	else
		if [ -d $PKGSAV/$SUNW_PATCHID ]; then
			rm -r $PKGSAV/$SUNW_PATCHID
		fi
		STAGE_DIR=$PKGSAV
		ARCHIVE_DIR=$PKGSAV/$SUNW_PATCHID
		mkdir $ARCHIVE_DIR
	fi

	pkgmk -o -d $STAGE_DIR 1>/dev/null 2>&1
	pkgtrans -s $STAGE_DIR $ARCHIVE_DIR/undo $PKG 1>/dev/null 2>&1
	compress $ARCHIVE_DIR/undo
	retcode=$?
	if [ "$retcode" != 0 ]; then
		echo "compress(1) returned error code $retcode"
		echo "The $PKGINST backout package will not be compressed."
		echo "Continuing to process backout package."
	fi
	if [ "$PATCH_UNDO_ARCHIVE" != "none" ]; then
		if [ $retcode != 0 ]; then
			build_remote_file "undo"
		else
			build_remote_file "undo.Z"
		fi
	fi
	rm -r $STAGE_DIR/$PKG

	cd ..
	rm -r $BUILD_DIR
	# remove the scripts that are left behind
	install_scripts=`dirname $0`
	rm $install_scripts/checkinstall $install_scripts/patch_checkinstall $install_scripts/patch_postinstall
fi

#
# Since this apparently worked, we'll mark as obsoleted the prior
# versions of this patch - installpatch deals with explicit obsoletions.
#
cd ${PKG_INSTALL_ROOT:-/}
cd var/sadm/pkg

active_base=`echo $SUNW_PATCHID | nawk '
	{ print substr($0, 1, match($0, "-")-1) } '`

List=`ls -d $PKGINST/save/${active_base}* 2>/dev/null`
if [ $? -ne 0 ]; then
	List=""
fi

for savedir in $List; do
        patch=`basename $savedir` 
        if [ $patch = $SUNW_PATCHID ]; then
		break
	fi

        # If we get here then the previous patch gets deleted
	if [ -f $savedir/undo ]; then
		mv $savedir/undo $savedir/obsolete
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif [ -f $savedir/undo.Z ]; then
		mv $savedir/undo.Z $savedir/obsolete.Z
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
        elif  [ -f $savedir/remote ]; then
                `grep . $PKGSAV/$patch/remote | sed 's|STATE=.*|STATE=obsolete|' > $TEMP_REMOTE` 
                rm -f $PKGSAV/$patch/remote 
                mv $TEMP_REMOTE $PKGSAV/$patch/remote  
                rm -f $TEMP_REMOTE 
                echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif  [ -f $savedir/obsolete -o -f $savedir/obsolete.Z ]; then
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	fi
done

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

#XXXSpecial_CommandsXXX#

TMP=/tmp/devlink.tab.$$
ETC_SYSTEM=${BASEDIR}/etc/system
TMP_SYSTEM=${BASEDIR}/tmp/system.$$
AWK=/usr/bin/nawk
BC="/usr/bin/bc -l"

_OsVer=`uname -r`

check_basedir()
{
	if [ "${BASEDIR}" = "/" -o -z "${BASEDIR}" ]
	then
		echo 0
	else
		echo 1
	fi
}

conv_to_dec()
{
	echo $1|grep "^0x" >/dev/null
	if [ $? -eq 0 ]
	then
		var=`echo $1|cut -c3-`
		DEC=`echo "ibase=16;$var"|$BC`
	else
		echo $1|grep "^0" > /dev/null
		if [ $? -eq 0 ]
		then
			var=`echo $1|cut -c2-`
			DEC=`echo "ibase=8;$var"|$BC`
		else
			DEC=$1
		fi
	fi
	echo $DEC
}

get_insert_linenum()
{
	cat $ETC_SYSTEM|grep -n $1 | while read i
	do
		line_num=`echo $i|$AWK -F':' '{ print $1 }'`
		tunable=`echo $i|$AWK -F'=' '{ print $2 }'`
		dec_tunable=`conv_to_dec $tunable`

		if [ `expr $dec_insert_tunable` -lt	\
			`expr $dec_tunable` ]
		then
			echo $line_num
			break
		fi
	done
}

insert_tunable()
{
	header="\n* vxfs_START -- do not remove the following lines:\n"
	header=$header"* VxFS requires a stack size greater than the "
	header=$header"default 8K.\n"
	header=$header"* The following value allows the kernel stack size"
	header=$header" to be\n* increased to 24K for Solaris 7, 8 and 9,"
	header=$header" and 16K for Solaris 5.6."

	dec_insert_tunable=`conv_to_dec $2`
	tline_num=`wc -l $ETC_SYSTEM|$AWK '{ print $1 }'`
	line_num=""
	line_num=`get_insert_linenum $1 $2`

	if [ "X${line_num}" != "X"  ]
	then
		max=`cat $ETC_SYSTEM|grep -n "^\*"|$AWK -F ':'	\
		-v line_num=$line_num 'BEGIN { localvar = 0; }	\
		{						\
			if ( $1 <= line_num )			\
			{					\
				localvar=$1;			\
			}					\
		}						\
		END { print localvar }'`

		iline_num=`$AWK -v max=$max			\
			'BEGIN { linenum=0; }			\
			{					\
				if ( NF == 0 && NR <= max )	\
				{				\
					linenum=NR;		\
				}				\
			}					\
			END { print linenum }' $ETC_SYSTEM`
		rline_num=`expr $tline_num - $iline_num`
	else
		iline_num=`wc -l $ETC_SYSTEM|$AWK '{ print $1 }'`
		rline_num=0
	fi

	rm -rf $TMP_SYSTEM
	head -$iline_num $ETC_SYSTEM >> $TMP_SYSTEM
	echo "$header" >> $TMP_SYSTEM
	echo "set $1=$2" >> $TMP_SYSTEM
	echo "* vxfs_END\n" >> $TMP_SYSTEM
	tail -$rline_num $ETC_SYSTEM >> $TMP_SYSTEM
	mv -f $TMP_SYSTEM $ETC_SYSTEM
}

cleanup_etcsystem()
{
	ETC_SYSTEM=${BASEDIR}/etc/system
	cp -p ${ETC_SYSTEM} ${BASEDIR}/etc/system.vxfs.preremove
	remove_all=1
	while [ $remove_all = 1 ]
	do
		grep -n vxfs_START ${ETC_SYSTEM} > /dev/null 2>&1
		if [ $? = 0 ]
		then
			start_line=`grep -n vxfs_START ${ETC_SYSTEM} | \
				head -1 | cut -d: -f1`
			start_line=`expr $start_line - 1`
			head -$start_line ${ETC_SYSTEM} > /tmp/system.$$
			end_line=`grep -n vxfs_END ${ETC_SYSTEM} | \
				head -1 | cut -d: -f1`
			if [ "$end_line" != "" ]
			then
				end_line=`expr $end_line + 1`
				tail +$end_line ${ETC_SYSTEM} >> /tmp/system.$$
			fi
			mv /tmp/system.$$ ${ETC_SYSTEM}
		else
			remove_all=0
		fi
	done
}

if [ `check_basedir` -eq 1 ]
then
	add_drv -n -b ${BASEDIR} -m '* 0640 root sys' vxportal
	add_drv -n -b ${BASEDIR} fdd
	echo "IMPORTANT: because BASEDIR is set, you are probably doing"
	echo "an installation to a client. You must reboot the client"
	echo "system before using VxFS."
else
	add_drv -n vxportal
	rem_drv vxportal
	add_drv -n -m '* 0640 root sys' vxportal
	add_drv -n fdd
	rem_drv fdd
	add_drv -n fdd
fi

#
# Check that QuickLog is not already loaded or installed on the system.
# Then add QuickLog.
#
modinfo | grep qlog 2>&1 > /dev/null
if [ $? -eq 0 -a `check_basedir` -eq 0 ]
then
	modunload -i `modinfo | grep qlog | cut -f1,2 -d' '`
#	if [ $? -ne 0 ]
#	then
#		echo "The QuickLog kernel module 'qlog' could not be unloaded."
#		echo "You may proceed with the pkgadd, but you may have to"
#		echo "reboot your system afterwards."
#		echo
#		echo "Do you want to proceed with addition of VRTSvxfs [y/n]?\c"
#		read ans
#		if [ "X" = "X$ans" ]
#		then
#			ans="n"
#		fi
#		if [ $ans = "N" -o $ans = "n" ]
#		then
#			exit 1
#		fi
#	fi
fi
if [ `check_basedir` -eq 1 ]
then
	add_drv -n -b ${BASEDIR} qlog > /dev/null 2>&1
else
	add_drv -n qlog
	rem_drv qlog
	add_drv -n qlog
fi

#
# Update /etc/devlink.tab
#
grep -v fdd $BASEDIR/etc/devlink.tab > $TMP
cat >> $TMP << EOT
type=ddi_pseudo;name=fdd	\D
EOT
mv -f $TMP $BASEDIR/etc/devlink.tab

$BASEDIR/usr/sbin/devlinks -r $BASEDIR > /dev/null 2>&1
if [ $? != 0 ]
then
	echo "Error creating device links; removing driver"
	if [ "$BASEDIR" = "" -o "$BASEDIR" = "/" ] ; then
		rem_drv fdd >/dev/null 2>&1
	else
		rem_drv -b $BASEDIR fdd >/dev/null 2>&1
	fi
	exit 1
fi

grep -v vxportal $BASEDIR/etc/devlink.tab > $TMP
cat >> $TMP << EOT
type=ddi_pseudo;name=vxportal	\D
EOT
mv -f $TMP $BASEDIR/etc/devlink.tab

$BASEDIR/usr/sbin/devlinks -r $BASEDIR > /dev/null 2>&1
if [ $? != 0 ]
then
	echo "Error creating device links; removing driver"
	if [ "$BASEDIR" = "" -o "$BASEDIR" = "/" ] ; then
		rem_drv vxportal >/dev/null 2>&1
	else
		rem_drv -b $BASEDIR vxportal >/dev/null 2>&1
	fi
	exit 1
fi

grep -v qlog ${BASEDIR}/etc/devlink.tab > ${BASEDIR}/etc/NEW1_devlink.tab
sed -e '$ a\
type=ddi_pseudo;name=qlog;minor=0,raw	qlog/admin\
type=ddi_pseudo;name=qlog;minor=1,raw	qlog/vxlog1\
type=ddi_pseudo;name=qlog;minor=2,raw	qlog/vxlog2\
type=ddi_pseudo;name=qlog;minor=3,raw	qlog/vxlog3\
type=ddi_pseudo;name=qlog;minor=4,raw	qlog/vxlog4\
type=ddi_pseudo;name=qlog;minor=5,raw	qlog/vxlog5\
type=ddi_pseudo;name=qlog;minor=6,raw	qlog/vxlog6\
type=ddi_pseudo;name=qlog;minor=7,raw	qlog/vxlog7\
type=ddi_pseudo;name=qlog;minor=8,raw	qlog/vxlog8\
type=ddi_pseudo;name=qlog;minor=9,raw	qlog/vxlog9\
type=ddi_pseudo;name=qlog;minor=10,raw	qlog/vxlog10\
type=ddi_pseudo;name=qlog;minor=11,raw	qlog/vxlog11\
type=ddi_pseudo;name=qlog;minor=12,raw	qlog/vxlog12\
type=ddi_pseudo;name=qlog;minor=13,raw	qlog/vxlog13\
type=ddi_pseudo;name=qlog;minor=14,raw	qlog/vxlog14\
type=ddi_pseudo;name=qlog;minor=15,raw	qlog/vxlog15\
type=ddi_pseudo;name=qlog;minor=16,raw	qlog/vxlog16\
type=ddi_pseudo;name=qlog;minor=17,raw	qlog/vxlog17\
type=ddi_pseudo;name=qlog;minor=18,raw	qlog/vxlog18\
type=ddi_pseudo;name=qlog;minor=19,raw	qlog/vxlog19\
type=ddi_pseudo;name=qlog;minor=20,raw	qlog/vxlog20\
type=ddi_pseudo;name=qlog;minor=21,raw	qlog/vxlog21\
type=ddi_pseudo;name=qlog;minor=22,raw	qlog/vxlog22\
type=ddi_pseudo;name=qlog;minor=23,raw	qlog/vxlog23\
type=ddi_pseudo;name=qlog;minor=24,raw	qlog/vxlog24\
type=ddi_pseudo;name=qlog;minor=25,raw	qlog/vxlog25\
type=ddi_pseudo;name=qlog;minor=26,raw	qlog/vxlog26\
type=ddi_pseudo;name=qlog;minor=27,raw	qlog/vxlog27\
type=ddi_pseudo;name=qlog;minor=28,raw	qlog/vxlog28\
type=ddi_pseudo;name=qlog;minor=29,raw	qlog/vxlog29\
type=ddi_pseudo;name=qlog;minor=30,raw	qlog/vxlog30\
type=ddi_pseudo;name=qlog;minor=31,raw	qlog/vxlog31\
type=ddi_pseudo;name=qlog;minor=32,raw	qlog/frlog' \
< ${BASEDIR}/etc/NEW1_devlink.tab > ${BASEDIR}/etc/NEW2_devlink.tab
mv ${BASEDIR}/etc/NEW2_devlink.tab ${BASEDIR}/etc/devlink.tab
rm ${BASEDIR}/etc/NEW1_devlink.tab

#
# Make qlog nodes in /dev/qlog
#
rm -fr ${BASEDIR}/dev/qlog >/dev/null 2>&1
mkdir ${BASEDIR}/dev/qlog >/dev/null 2>&1
QL_MAJOR=`grep qlog ${BASEDIR}/etc/name_to_major | awk '{print $2}'`
mknod ${BASEDIR}/dev/qlog/admin c $QL_MAJOR 0
mknod ${BASEDIR}/dev/qlog/vxlog1 c $QL_MAJOR 1
mknod ${BASEDIR}/dev/qlog/vxlog2 c $QL_MAJOR 2
mknod ${BASEDIR}/dev/qlog/vxlog3 c $QL_MAJOR 3
mknod ${BASEDIR}/dev/qlog/vxlog4 c $QL_MAJOR 4
mknod ${BASEDIR}/dev/qlog/vxlog5 c $QL_MAJOR 5
mknod ${BASEDIR}/dev/qlog/vxlog6 c $QL_MAJOR 6
mknod ${BASEDIR}/dev/qlog/vxlog7 c $QL_MAJOR 7
mknod ${BASEDIR}/dev/qlog/vxlog8 c $QL_MAJOR 8
mknod ${BASEDIR}/dev/qlog/vxlog9 c $QL_MAJOR 9
mknod ${BASEDIR}/dev/qlog/vxlog10 c $QL_MAJOR 10
mknod ${BASEDIR}/dev/qlog/vxlog11 c $QL_MAJOR 11
mknod ${BASEDIR}/dev/qlog/vxlog12 c $QL_MAJOR 12
mknod ${BASEDIR}/dev/qlog/vxlog13 c $QL_MAJOR 13
mknod ${BASEDIR}/dev/qlog/vxlog14 c $QL_MAJOR 14
mknod ${BASEDIR}/dev/qlog/vxlog15 c $QL_MAJOR 15
mknod ${BASEDIR}/dev/qlog/vxlog16 c $QL_MAJOR 16
mknod ${BASEDIR}/dev/qlog/vxlog17 c $QL_MAJOR 17
mknod ${BASEDIR}/dev/qlog/vxlog18 c $QL_MAJOR 18
mknod ${BASEDIR}/dev/qlog/vxlog19 c $QL_MAJOR 19
mknod ${BASEDIR}/dev/qlog/vxlog20 c $QL_MAJOR 20
mknod ${BASEDIR}/dev/qlog/vxlog21 c $QL_MAJOR 21
mknod ${BASEDIR}/dev/qlog/vxlog22 c $QL_MAJOR 22
mknod ${BASEDIR}/dev/qlog/vxlog23 c $QL_MAJOR 23
mknod ${BASEDIR}/dev/qlog/vxlog24 c $QL_MAJOR 24
mknod ${BASEDIR}/dev/qlog/vxlog25 c $QL_MAJOR 25
mknod ${BASEDIR}/dev/qlog/vxlog26 c $QL_MAJOR 26
mknod ${BASEDIR}/dev/qlog/vxlog27 c $QL_MAJOR 27
mknod ${BASEDIR}/dev/qlog/vxlog28 c $QL_MAJOR 28
mknod ${BASEDIR}/dev/qlog/vxlog29 c $QL_MAJOR 29
mknod ${BASEDIR}/dev/qlog/vxlog30 c $QL_MAJOR 30
mknod ${BASEDIR}/dev/qlog/vxlog31 c $QL_MAJOR 31
mknod ${BASEDIR}/dev/qlog/frlog c $QL_MAJOR 32

sync; sleep 1; sync; sleep 1; sync

# first cleanup entries put in by 3.4ga pkg for stksizes
cleanup_etcsystem
#
# We need to increase the stack size of all threads in the system
# to 24K for sol 5.7,5.8 and 5.9 and 16K for sol5.6. To do this,
# we set lwp_default_stksize to appropriate value in /etc/system.
# We also set svc_run_stksize=0x4000/0x6000 (on sol 5.6/5.7) and
# svc_default_stksize=0x6000 (on sol1 5.8, 5.9) for NFS server threads.
#

cp -p ${ETC_SYSTEM} ${BASEDIR}/etc/fs/vxfs/system.preinstall

if [ "${_OsVer}" = "5.8" -o "${_OsVer}" = "5.9" -o "${_OsVer}" = "5.7" ]
then
	insert_tunable "lwp_default_stksize" 0x6000
else
	if [ "${_OsVer}" = "5.6" ]
	then
		insert_tunable "lwp_default_stksize" 0x4000
	fi
fi

if [ "${_OsVer}" = "5.8" -o "${_OsVer}" = "5.9" ]
then
	echo "svc_default_stksize=X" | adb -k > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		insert_tunable "rpcmod:svc_default_stksize" 0x6000
	fi
else
	if [ "${_OsVer}" = "5.7" ]
	then
		echo "svc_run_stksize=X" | adb -k > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
			insert_tunable "rpcmod:svc_run_stksize" \
			0x6000
		fi
	fi
	if [ "${_OsVer}" = "5.6" ]
	then
		echo "svc_run_stksize=X" | adb -k > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
			insert_tunable "rpcmod:svc_run_stksize" \
			0x4000
		fi
	fi
fi


exit 0

