#!/bin/sh
#
# ident "@(#)postinstall	1.2    06/16/04 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

OS=`uname -s`
if [ ! -n "${OS}" ]; then
    echo "Unable to determine operating system."
    exit 1
fi

INSTALL_DATA=${BASEDIR}/usr/share/lib/jato/.install_data

RELOCATE=""
if [ -n "${PKG_INSTALL_ROOT}" ]; then
    RELOCATE="-R ${PKG_INSTALL_ROOT}"
fi

if [ "${OS}" = "SunOS" ]; then

    # SUNWjato pkgs version 2.0.4 or earlier included the entire Jato
    # distribution, including the extras & demo.  Since this stuff is now 
    # contained in it's own pkg, we should re-register those files with 
    # the pkg database as being owned by the correct pkg.
    #
    files2reregister=/tmp/files2reregister.$$
    rm -f ${files2reregister} >/dev/null 2>&1
    env LANG=C LC_ALL=C pkgchk -l ${RELOCATE} SUNWjato \
	| grep -i pathname | grep "jato/extra" \
	| awk '{print $2}' > ${files2reregister}

    if [ -s ${files2reregister} ]; then

	obsoleteFiles=/tmp/obsoleteFiles.$$
	rm -f $obsoleteFiles >/dev/null 2>&1

	# Lock pkg database to remove each entry from the DB.
	# Note that we pump a file list into a single call to removef,
	# which is ALOT faster than calling removef individually for each file.
	# A file will be deleted only if no longer has a pkg owner, which
	# is the case for files that are no longer installed by $PKGINST.
	#
	removef ${RELOCATE} SUNWjato `cat ${files2reregister}` >> $obsoleteFiles
	removef ${RELOCATE} -f SUNWjato

	if [ -s $obsoleteFiles ]; then
	    cat $obsoleteFiles | \
		while read pathname
		do
		    rm -rf $pathname
		done
	fi

	# Since we need to dynamically re-register components using `installf',
	# for performance sake we queue them up in a file and invoke `installf'
	# on the file, rather than running a seperate instance for each 
	# component.
	PKGREGS=/tmp/${PKGINST}_I.$$
	rm -f ${PKGREGS} >/dev/null 2>&1

	for f in `cat $files2reregister`
	do
	    # Don't re-register files which are obsolete and which
	    # have been deleted.
	    if [ -s $obsoleteFiles ]; then
		grep $f $obsoleteFiles >/dev/null 2>&1
		if [ $? -eq 0 ]; then
		    continue
		fi
	    fi

	    if [ -d $f ]; then
		echo "${f} d 0755 root bin" >> ${PKGREGS}
	    else
		echo "${f} f 0444 root bin" >> ${PKGREGS}
	    fi
	done
	cat ${PKGREGS} | installf ${RELOCATE} -c none ${PKGINST} -

	# Inform pkg DB we're done
	installf ${RELOCATE} -f ${PKGINST}

	rm -f ${PKGREGS}
	rm -f ${files2reregister}
	rm -f ${obsoleteFiles} >/dev/null 2>&1

    fi


    # Save pkginfo to a file and register it with the pkg database.
    pkgparam -v $RELOCATE $PKGINST > ${INSTALL_DATA}/${PKG}.pkginfo
    installf -c none ${RELOCATE} ${PKGINST} ${INSTALL_DATA}/${PKG}.pkginfo f 644 root sys
    installf -f ${RELOCATE} ${PKGINST}
fi

exit 0
