#!/bin/sh
# %Z%%M% %I% %E% SMI
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

trap '/usr/bin/rm $TEMP $REMTEMP > /dev/null 2>&1' 0 1 3

if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
	SDOCSBASE="`pkgparam SUNWsdocs BASEDIR`"
	DOCSDIR=${SDOCSBASE}
	HTMLDIR="/"
else
	SDOCSBASE="`pkgparam -R ${PKG_INSTALL_ROOT} SUNWsdocs BASEDIR`"
	DOCSDIR=${PKG_INSTALL_ROOT}${SDOCSBASE}
	HTMLDIR="${PKG_INSTALL_ROOT}/"
fi

TEMP=/tmp/$$tempfile
SUNDOCSDIR=${HTMLDIR}/var/opt/sun_docs
SUNDOCS=${SUNDOCSDIR}/sundocs.html
TEMPLATE=${DOCSDIR}/SUNWsdocs/sun_docs/sundocs.template
COPYRIGHT=${DOCSDIR}/SUNWsdocs/sun_docs/copyright.template
GifPath=`/usr/bin/echo ${SDOCSBASE}/SUNWsdocs/sun_docs | \
	sed -e 's/\/\//\//'`

ID=`id`
USER=`expr "${ID}" : 'uid=\([^(]*\).*'`

if [ ${USER} != '0' ]
then
	#echo " You must be root to run $0"
	exit 2
fi

OPT=$1

if [ $# -ne 1 ]
then
	exit 2
fi

if [ $OPT = "i" ] # Install package
then

	if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
		INFO=`pkginfo -x -c SUNDOCS |\
			sed -e '/^  /d'|\
			sed -e 's/   */|/'|\
			nawk -F"|" '{print $1}' `
	else
		INFO=`pkginfo -R ${PKG_INSTALL_ROOT} -x -c SUNDOCS |\
			sed -e '/^  /d'|\
			sed -e 's/   */|/'|\
			nawk -F"|" '{print $1}' `
	fi

	mkdir -p -m 0755 ${SUNDOCSDIR}

	sed -e "s,GIFPATH,$GifPath," $TEMPLATE > $TEMP
	cat $TEMP > $SUNDOCS
	>$TEMP
	
	for PName in $INFO
	do
		if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
			BaseDir=`pkgparam $PName BASEDIR`
			Name=`pkgparam $PName NAME`
			TLOC=`pkgparam $PName SUNW_LOC|cut -d , -f 1`
			LOC=${TLOC:-C}
			PRODUCT=`pkgparam $PName SUNW_DOCRELEASE`
			PkgName=`pkgparam $PName PKG`
		else
			BaseDir=`pkgparam -R ${PKG_INSTALL_ROOT} $PName BASEDIR`
			Name=`pkgparam -R ${PKG_INSTALL_ROOT} $PName NAME`
			TLOC=`pkgparam -R ${PKG_INSTALL_ROOT} $PName SUNW_LOC|cut -d , -f1`
			LOC=${TLOC:-C}
			PRODUCT=`pkgparam -R ${PKG_INSTALL_ROOT} $PName SUNW_DOCRELEASE`
			PkgName=`pkgparam -R ${PKG_INSTALL_ROOT} $PName PKG`
		fi

	# Fields:
	#   1 - PKG
	#   2 - BASEDIR
	#   3 - Title
	#   4 - Release
	#   5 - Lang
	#

		echo "$PkgName|$BaseDir|$Name|$PRODUCT|$LOC" >> $TEMP
	done

	sort -t'|' -k 5 -o $TEMP $TEMP

	FIRST=1
	OldLang=`head -n 1 $TEMP | nawk -F"|" '{print $5}'`

	exec 4< $TEMP
	IFS="|"

	while read <&4 line
	do
		set $line
		#echo "$1:$2:$3"

		# Set ALT value based on LOC

		case "$5" in
			C) ALT="English";;
			de) ALT="German";;
			es) ALT="Spanish";;
			fr) ALT="French";;
			it) ALT="Italian";;
			ja) ALT="Japanese";; 
			ko) ALT="Korean";;
			sv) ALT="Swedish";;
			zh) ALT="Simplified Chinese";;
			zh_TW) ALT="Traditional Chinese";;
			*) ALT="";;
		esac

		# Determine if there should be a break in the group.

		if [ $FIRST -eq 1 ]
		then
			BR="<TR><TD height=\"20\"><IMG src=\"$GifPath/graphics/\
${5}_lang.gif\" alt=\"${ALT}\"></TD></TR><!--LOC=$5-->"
			FIRST=0
		elif [ $OldLang = $5 ]
		then
			BR=""
			OldLang=$5
		else
			BR="<TR><TD height=\"20\"><IMG src=\"$GifPath/graphics/\
${5}_lang.gif\" alt=\"${ALT}\"></TD></TR><!--LOC=$5-->"
			OldLang=$5
		fi


		echo "${BR}" >> $SUNDOCS
		echo "<!--INST=$5--><TR><TD width=\"5%\"></TD><TD><A \
href=\"$2/sun_docs/$5/$4/$1/booklist.html\">$3</A></TD></TR>\
<!--SUNDOCS-->" >> $SUNDOCS

	done

	exec 4<&-

	cat $COPYRIGHT >> $SUNDOCS

	exit 0

################################Remove Code######################

elif [ $OPT = "r" ] # Package remove
then

	REMTEMP=/tmp/$$sundocs


	if [ ! -w $SUNDOCS ]
	then
		exit 2
	fi
	# Check that there is an entry, if not this is an error
	grep "$PKGINST" $SUNDOCS > /dev/null 
	if [ $? -ne 0 ]
	then
		exit 2
	else
	# Remove this packages entry

	# First get the locale
		LOC=`grep "INST=" $SUNDOCS \
		| grep "${PKGINST}"\
		| nawk -F"=" '{print $2}'\
		| nawk -F"-" '{print $1}'`

		BRLOC="LOC=$LOC"
		INLOC="INST=$LOC"

		# Remove the entry
		sed -e "/.*${PKGINST}.*/d" $SUNDOCS > $REMTEMP
		/usr/bin/mv $REMTEMP $SUNDOCS

		# Check to see if there any other entires of the same locale
		# If not remove the graphic separator
		grep "$INLOC" $SUNDOCS > /dev/null
		if [ $? -ne 0 ]
		then
			sed -e "/.*${BRLOC}.*/d" $SUNDOCS > $REMTEMP
			/usr/bin/mv $REMTEMP $SUNDOCS

		fi

	fi

	# Check to see if there are any entries left.
	# If there are none, remove the SUNDOCS file
	# and the sun_docs directory.
	grep '<\!--SUNDOCS-->' $SUNDOCS > /dev/null

	if [ $? -ne 0 ]
	then
		/usr/bin/rm -f $SUNDOCS
		/usr/bin/rmdir $SUNDOCSDIR
		exit 0
	fi
else # Neither cmd line argument specified
	exit 2
fi
