#!/bin/sh
#
# install-pkgindex package version installdir pkgfile
# 

package="$1"
version="$2"
installdir="$3"
pkgfile=${4:-pkgIndex.tcl}

if [ -s $installdir/pkgIndex.tcl ]
then
    echo "Updating $installdir/pkgIndex.tcl"
	cp $installdir/pkgIndex.tcl $installdir/pkgIndex.tcl.old
sed -e "/^# -- $package $version package ifneeded script --\$/,/^# end of $package $version package ifneeded script --\$/d" < $installdir/pkgIndex.tcl >  newpkgIndex.tcl
else
    echo "Creating $installdir/pkgIndex.tcl"
fi


echo >> newpkgIndex.tcl
echo "# -- $package $version package ifneeded script --" >> newpkgIndex.tcl
egrep -v '^#|^$' $pkgfile >> newpkgIndex.tcl
echo "# -- end of $package $version package ifneeded script --" >> newpkgIndex.tcl

cp newpkgIndex.tcl $installdir/pkgIndex.tcl
rm -f newpkgIndex.tcl

exit 0
