#! /bin/sh
#
# Copyright 1996-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident  "@(#)postinstall 1.25 03/08/14 SMI"
#

###########################################################################
#
# postinstall for SUNWscr
#
#	* Update ${BASEDIR}/etc/devlinks
#	(* Add DID driver: moved to scinstall)
#	* Make ${BASEDIR}/global/.devices/node@${nodeid} directories
#	* Insert "sd_retry_on_reservation_conflict=0" in sd.conf
#	* Insert "sd_retry_on_reservation_conflict=0" in ssd.conf
#	* Add appropriate lines to /etc/system
#	* Update /etc/iu.ap file to include clhbsndr for public net devices
#
###########################################################################

PATH=/usr/bin:/usr/sbin
export PATH

SC_MAXNODEID=64

PKGNAME=SUNWscr
STARTTAG="Start of lines added by ${PKGNAME}"
  ENDTAG="End   of lines added by ${PKGNAME}"
PROG=${PKGNAME}.postinstall

TMPFILE1=/tmp/devlink_update.$$
TMPFILE2=/tmp/new.iu.ap.$$
PUBLIC_NETWORK_DEVICES="hme qfe eri ge ce bge"

###########################################
#
# Cleanup any temporary files on exit/interrupt
#
###########################################
cleanup()
{
	exit_code=$1

	if [ "${TMPFILE1}" ]; then
		rm -f ${TMPFILE1}
	fi

	if [ -f "${TMPFILE2}" ]; then
		rm -f ${TMPFILE2}
	fi

	exit ${exit_code}
}

###########################################
#
# remove_SUNWscr_edits <filename>
#
#	Backout all lined from the given ascii <filename>
#	indicated by the SUNWscr comment tag lines.
#
###########################################
remove_SUNWscr_edits()
{
	update_file=$1

	while :
	do
		grep "${STARTTAG}" ${update_file} >/dev/null 2>&1
		case $? in
		2)	# error reading file
			echo "${PROG}:  error reading ${update_file}"
			return 1
			;;

		0)	# grep found the string, so get rid of the entries
			ed -s ${update_file} << EOF >/dev/null 2>&1
/${STARTTAG}/,/${ENDTAG}/d
w
q
EOF
			if [ $? -ne 0 ]; then
				echo "${PROG}: problem updating ${update_file}"
				return 1
			fi
			;;

		*)
			break
			;;
		esac
	done

	return 0
}

###########################################
#
# add entries to /etc/devlinks.tab
#
###########################################
update_etc_devlinks()
{
	update_file=${BASEDIR}/etc/devlink.tab

	# remove the old entries
	remove_SUNWscr_edits ${update_file}

	# remove any errant did entries
	grep -v "^type=ddi_pseudo;name=did;minor" ${update_file} > ${TMPFILE1}

	if [ ! -s ${TMPFILE1} ]
	then
		return 1
	fi
	mv ${TMPFILE1} ${update_file}

	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
type=ddi_pseudo;name=did;minor=admin	did/admin
type=ddi_pseudo;name=did;minor3=blk	did/dsk/d\M2
type=ddi_pseudo;name=did;minor3=raw	did/rdsk/d\M2
type=ddi_pseudo;name=did;minor3=tp	did/rmt/\M2
type=ddi_pseudo;name=clprivnet	\M0
# ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# Make /global/.devices/node@<ID> directories.
# /global and /global/.devices should have already
# been created as a result of entries in the package map.
#
###########################################
make_globaldevdirs()
{
	nodeid=1
	mkdirerrs=0
	chgerr=0
	dir=

	while [ ${nodeid} -le ${SC_MAXNODEID} ]
	do
		dir=${BASEDIR}/global/.devices/node@${nodeid}
		if [ ! -d ${dir} ]; then
			mkdir -m 0755 ${dir}
			if [ $? -ne 0 ]; then
				mkdirerrs=`expr ${mkdirerrs} + 1`
				echo "${PROG}: failed to mkdir ${dir}"
			else
				chgerr=0
				chown root ${dir} || `expr ${chgerr} + 1`
				chgrp sys  ${dir} || `expr ${chgerr} + 1`
				if [ ${chgerr} -ne 0 ]; then
					mkdirerrs=`expr ${mkdirerrs} + 1`
					echo "${PROG}: failed to change group/owner for ${dir}"
				fi
			fi
		fi

		nodeid=`expr ${nodeid} + 1`
	done

	return ${mkdirerrs}
}

###########################################
#
# Insert "sd_retry_on_reservation_conflict=0"
# in sd.conf or ssd.conf.  The SCSI disk driver
# must not do retries when encountering reservation
# conflict conditions.
#
###########################################
update_sd_conf()
{
	update_file=$1

	# If sd.conf or ssd.conf do not exist, we are done
	if [ ! -f "${update_file}" ]; then
		return 0
	fi

	# remove the old entries
	remove_SUNWscr_edits ${update_file}

	# insert the new line near the top of the file
	ed -s ${update_file} << EOF >/dev/null 2>&1
/name=/i

# ${STARTTAG}
sd_retry_on_reservation_conflict=0;
# ${ENDTAG}

.
w
q
EOF

	# report any errors
	if [ $? -ne 0 ]; then
		echo "${PROG}: problem updating ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
###########################################
update_etc_system()
{
	update_file=${BASEDIR}/etc/system

	# remove the old entries
	remove_SUNWscr_edits ${update_file}

	# add the new entries
cat << EOF >> ${update_file}
* ${STARTTAG}
set rpcmod:svc_default_stksize=0x6000
set ge:ge_intr_mode=0x833
* ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# Set up a link in the rcS.d directory for
# the did_update_vfstab. This will remove
# itself after it has run once in clustered
# mode.
#
###########################################
link_did_rcscript()
{
	rcfile=${BASEDIR}/etc/rcS.d/S68did_update_vfstab

	if [ -f "${rcfile}" ]; then
		rm -f ${rcfile}
	fi

	ln ${BASEDIR}/etc/init.d/did_update_vfstab ${rcfile}

	if [ $? -ne 0 ]; then
		echo "${PROG}: problem linking did_update_vfstab"
		return 1
	fi

	return 0
}

###########################################
#
# Update /etc/iu.ap file. Known public network
# devices will be configured to have the clhbsndr
# streams module autopushed.
#
###########################################
update_etc_iu_ap()
{
	update_file=${BASEDIR}/etc/iu.ap

	# If /etc/iu.ap does not exist, create it
	if [ ! -f "${update_file}" ]; then
		cat << EOF >> ${update_file}
#
# Autopush setup
#

EOF
		if [ $? -ne 0 ]; then
			echo "${PROG}: problem updating ${update_file}"
			return 1
		fi
        fi

	# remove the old entries
	remove_SUNWscr_edits ${update_file}

	#
	# Add an entry for each know public network device to the the
	# /etc/iu.ap file. The streams module clhbsndr is configured to
	# be pushed right above the device driver. If an entry already
	# exists, the entry is updated to include the clhbsndr module.
	# The following awk script creates the new /etc/iu.ap file after
	# consulting the existing file. The main block updates existing
	# entries. The END block adds entries for devices that did not
	# already exist in the file.
	#
	awk 'BEGIN {modified = 0; ndevs = 0} \
		   { \
			if (NR == 1) ndevs = split(devices, dev, " "); \
			for (i = 1; i <= ndevs; i++) { \
				if ($1 == dev[i]) { \
					found[dev[i]] = 1 \
				} \
			} \
        		if (found[$1] != 1) { \
                		print \
        		} else if ($4 != "clhbsndr") { \
				modified++; \
                		for (i = 1; i <= 3; i++) printf("\t%s", $i); \
                		printf("\tclhbsndr"); \
                		for (i = 4; i <= NF; i++) printf(" %s", $i); \
                		printf("\n"); \
        		} else { \
                		print \
        		} \

		   } \
	     END   { \
			if (ndevs == 0) ndevs = split(devices, dev, " "); \
			printf("# %s\n", st); \
			if (modified > 0) { \
                        	printf("# In addition to these new line,"); \
				printf(" some preexisting lines above\n"); \
                        	printf("# might have been modified to"); \
				printf(" include the module clhbsndr\n"); \
			} \
			for (i = 1; i <= ndevs; i++) { \
				if (found[dev[i]] != 1) { \
					printf("\t%s\t-1\t0\tclhbsndr\n", dev[i]) \
				} \
		   	} \
			printf("# %s\n", et) \
		   }' "devices=$PUBLIC_NETWORK_DEVICES" "st=$STARTTAG" \
			 "et=$ENDTAG" ${update_file} > ${TMPFILE2} \
			2>/dev/null

	if [ $? -ne 0 ]; then
		echo "${PROG}: problem updating ${update_file}"
		return 1
	fi

	mv -f ${TMPFILE2} ${update_file}

	if [ $? -ne 0 ]; then
		echo "${PROG}: problem updating ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# add clprivnet driver 
#
###########################################
add_clprivnet_driver()
{
#
# If clprivnet is already included in /etc/name_to_major then skip add_drv
# is not necessary so return 0 immediately.
#
/usr/bin/grep clprivnet ${BASEDIR}/etc/name_to_major > /dev/null
if [ $? -eq 0 ]; then
	return 0
fi

/usr/sbin/add_drv -b ${BASEDIR} -m '* 0600 root root' -i 'SUNW,clprivnet' clprivnet

# report any errors
if [ $? -ne 0 ]; then
	echo "${PROG}: add_drv of clprvinet driver failed"
	return 1
fi

return 0
}

###########################################
#
# main
#
###########################################
errs=0

# catch common signals
trap 'echo "${PROG}: caught signal";  cleanup 3' 1 2 3 15

update_etc_devlinks	|| errs=`expr ${errs} + 1`
make_globaldevdirs	|| errs=`expr ${errs} + 1`
update_sd_conf ${BASEDIR}/kernel/drv/sd.conf	|| errs=`expr ${errs} + 1`
update_sd_conf ${BASEDIR}/kernel/drv/ssd.conf	|| errs=`expr ${errs} + 1`
update_etc_system	|| errs=`expr ${errs} + 1`
link_did_rcscript	|| errs=`expr ${errs} + 1`
update_etc_iu_ap	|| errs=`expr ${errs} + 1`
add_clprivnet_driver	|| errs=`expr ${errs} + 1`

# make sure next reboot is a reconfig reboot
touch ${BASEDIR}/reconfigure

if [ ${errs} -ne 0 ]; then
	cleanup 1
else
	cleanup 0
fi
