#!/bin/sh
#
#	"@(#)parse_dynamic_clustertoc.sh 1.2 01/04/25"
#
# parse dynamic clustertoc
#	invoke from the end of <cd>/sbin/sysconfig BEFORE running suninstall
#	or invoke from live upgrade before running pfinstall
#

# PKGS is where the installable pkgs live
# ASSUME: running off CDROM, only one version of Solaris packages per CD

# set ALT_ROOT to the default of /cdrom
ALT_ROOT="/cdrom"

# parse the arguements 
while getopts "c:" opt; do
	case $opt in
		c ) ALT_ROOT=$OPTARG;;
		* ) echo "ERROR: invalid option"
		    exit 1;;
	esac
done

# determine the PRODUCT, ie Solaris_8 or Solaris_9
prod_name=`cat $ALT_ROOT/.cdtoc | grep PRODNAME | grep Solaris`
prod_ver=`cat $ALT_ROOT/.cdtoc | grep PRODVERS`
PRODUCT="`expr ${prod_name} : 'PRODNAME=\(.*\)'`"
PRODUCT="${PRODUCT}_"
PRODUCT="${PRODUCT}`expr ${prod_ver} : 'PRODVERS=\(.*\)'`"

# Get the package path, this may be redundant, but that is what
# PRODDIR is there for
prod_line=`cat $ALT_ROOT/.cdtoc |grep PRODDIR | grep Solaris`
pkg_dir=`expr ${prod_line} : 'PRODDIR=\(.*\)'`
PKGS=$ALT_ROOT/${pkg_dir}

# TMPDIR is the writable place to put the parsed clustertoc
TMPDIR=/tmp/clustertocs/locale/C

# DEFAULT is the default .clustertoc
DEFAULT="${PKGS}/locale/C/.clustertoc.default"

# DYNAMIC is the dynamic .clustertoc
DYNAMIC="${PKGS}/locale/C/.clustertoc.dynamic"

# TESTDIR is where to find the tests that are programs
# always use the one in the miniroot on the install media
MINIROOT_LOC=/Tools/Boot
DYNAMIC_TEST_PATH=/usr/sbin/install.d/dynamic_test
TESTDIR=$ALT_ROOT/${PRODUCT}/${MINIROOT_LOC}/${DYNAMIC_TEST_PATH}

if [ ! -d $TESTDIR ];then
	TESTDIR=${DYNAMIC_TEST_PATH}
fi

#
# process_it() - processes a SUNW_CSRMBRIFF line
# INPUT: $1 is the line
# OUTPUT: stdout, if test matches then SUNW_CSRMEMBER entry, else null
#	stderr: if unknown test, print a warning.
#
process_it () {

	thetest=`expr "$1" : 'SUNW_CSRMBRIFF=(\(.*\)[ ].*'`
	thearg=`expr "$1" : 'SUNW_CSRMBRIFF=(.*[ ][ ]*\(.*\)).*'`
	thething=`expr "$1" : 'SUNW_CSRMBRIFF=(.*)\(.*\)'`

	# if the test is a builtin, do it then return
	case ${thetest} in
	platform)
		# a builtin - platform
		if [ "${SI_MODEL}" = "${thearg}" ]; then
			echo "SUNW_CSRMEMBER=${thething}"
		fi
		return
		;;
	# locale and other builtins should go here
	esac

	# if we got here, must try an external program
	if [ ! -x "${TESTDIR}/${thetest}" ]; then
		echo "parse dynamic clustertoc: \"${thetest}\" not found or not executable" >> /dev/stderr
		return
	fi
	${TESTDIR}/${thetest} "${thearg}" 1>> /dev/stderr 2>&1
	if [ $? -eq 0 ]; then
		echo "SUNW_CSRMEMBER=${thething}"
	fi
}

# get the platform specific identifier
SI_MODEL=`prtconf | awk ' {
		if (state == 1 && $0 != "") {
			print $0
			exit
		}
		if (substr($0, 1, 18) == "System Peripherals")
			state=1
	}'`

# substitute all spaces and tabs with "_"
SI_MODEL=`echo ${SI_MODEL} | sed 's/ /_/g' | sed 's/	/_/g' | sed "s/\'//g"`

# if we cannot get the dynamic clustertoc, then punt
if [ ! -f "${DYNAMIC}" ]; then
	# use the .default if possible
	if [ -f ${DEFAULT} ]; then
		mkdir -p ${TMPDIR}
		ln -s "${DEFAULT}" "${TMPDIR}/.clustertoc"
	fi
	exit 0
fi
		
if [ ! -d ${TMPDIR} ];then
	mkdir -p ${TMPDIR}
fi

rm -f ${TMPDIR}/.clustertoc

while read aline ; do
	# if anything other than "SUNW_CSRMBRIFF", pass thru
	case "${aline}" in
	SUNW_CSRMBRIFF=*)
		# process it, assume it to be syntactically correct
		output=`process_it "${aline}"`
		if [ "${output}" ]; then
			echo "${output}" >> "${TMPDIR}/.clustertoc"
		fi
		;;
	*)
		echo "${aline}" >> "${TMPDIR}/.clustertoc"
	esac
done < ${DYNAMIC}

exit 0
