
trap 'exit 3' 15

#+

#AB2BIN=/usr/lib/ab2/bin
#AB2BASEDIR=`pkgparam SUNWab2u BASEDIR 2>> /dev/null`
#AB2BIN=${AB2BASEDIR}/lib/ab2/bin

#re: configuration file text composition:
#    the following var must be modified under circumstances
#    before writing its value to a config file :

#Jan 97: for the nil_heavy interactive package:
# AB2DATADIR should have been assigned and exported by the request script.


if [ -z "${AB2DATADIR}" ]
then
	echo "FATAL install script processing error!"
	echo "AB2DATADIR has not been passed by th request script!"
	echo "signaling error to pkgadd..."
	
	exit 1
fi

if [ -z "${AB2INSTALLDIR}" ]
then
	echo "FATAL install script processing error!"
	echo "AB2INSTALLDIR has not been passed by th request script!"
	echo "signaling error to pkgadd..."
	
	exit 1
fi

if [ ! "$PKG_INSTALL_ROOT" = "" ] -a [ ! "$PKG_INSTALL_ROOT" = "/" ]
then
	ABINSTALLPATH=`echo ${PKG_INSTALL_ROOT}/${AB2INSTALLDIR} |sed -e 's;//;/;'|sed -e 's;//;/;'`
else
	ABINSTALLPATH=`echo ${AB2INSTALLDIR} | sed -e 's;//;/;'`
fi

export ABINSTALLPATH

#-

postinstallstat=0

ESCAPEDAB2DATADIR=`echo ${AB2DATADIR} | sed 's;\\$;;'`
sed -e 's:<<ABINSTALLPATH>>:'${ESCAPEDAB2DATADIR}':' <  ${ABINSTALLPATH}/socat > /tmp/socat$$

mv /tmp/socat$$ ${ABINSTALLPATH}/socat

sed -e 's:<<ABINSTALLPATH>>:'${ESCAPEDAB2DATADIR}':' <  ${ABINSTALLPATH}/collinfo > /tmp/collinfo$$

mv /tmp/collinfo$$ ${ABINSTALLPATH}/collinfo


chown bin ${ABINSTALLPATH}/socat
chgrp bin ${ABINSTALLPATH}/socat
chown bin ${ABINSTALLPATH}/collinfo
chgrp bin ${ABINSTALLPATH}/collinfo

if [ ! "$PKG_INSTALL_ROOT" = "" ] 
then
	AB2BASEDIR=`pkgparam -R $PKG_INSTALL_ROOT SUNWab2u BASEDIR 2>> /dev/null`
	if [ "$AB2BASEDIR" = "" ]
	then
                echo "WARNING: This package has been installed on this machine,"
                echo "however an AnswerBook2 server is required to view document collections."
		exit $postinstallstat
	fi
	AB2BASEDIR=`echo ${PKG_INSTALL_ROOT}/${AB2BASEDIR}  |sed -e 's;//;/;'|sed -e 's;//;/;'`
else
	AB2BASEDIR=`pkgparam SUNWab2u BASEDIR 2>> /dev/null`
	if [ "$AB2BASEDIR" = "" ]
	then
                echo "WARNING: This package has been installed on this machine,"
                echo "however an AnswerBook2 server is required to view document collections."
		exit $postinstallstat
	fi
fi

AB2BIN=${AB2BASEDIR}/lib/ab2/bin
export AB2BIN

if [ ! -x "${AB2BIN}/ab2admin" ]
then
   echo "WARNING: Can't see ${AB2BIN}/ab2admin , the AnswerBook2 admin utility,"
   echo "on this machine!  You will have to go to machine having ${AB2BIN}/ab2admin"
   echo "to administer the collections which were added on this machine by this"
   echo "package."
  echo "Note: This Collection will not be accessible until after the AB2 server"
   echo "      has been installed."
   exit $postinstallstat
fi

   if [ -z "$PKG_INSTALL_ROOT" ] 
   then
      cmd="${AB2BIN}/ab2admin -o install -n ${PKGINST} -d ${ABINSTALLPATH}"
   else
        if [ "$PKG_INSTALL_ROOT" = "/" ]
        then
      	   cmd="${AB2BIN}/ab2admin -o install -n ${PKGINST} -d ${ABINSTALLPATH}"
	else
      	   cmd="${AB2BIN}/ab2admin -o install -n ${PKGINST} -d ${ABINSTALLPATH} -R $PKG_INSTALL_ROOT"
           echo "WARNING: This package is installed with pkgadd -R."
           echo "The collection is registered to AB2 server with path ${AB2INSTALLDIR}.\n"
           echo "This collection will not show up until the root_path $PKG_INSTALL_ROOT is resolved to the system location."
	fi
   fi

   echo "Adding AnswerBook Collections with command:"
   echo "$cmd"

   eval $cmd

   if [ "$?" -ne 0 ]
   then
	postinstallstat=1
	echo "NOTICE: postinstall failed to add AnswerBook collection to AB2 server"
	echo "The collections in this package have been physically added"
	echo "to this machine, but the AnswerBook2 admin function to"
	echo "logically add the collections has failed."
	echo ""
   else
        if [ -z "$PKG_INSTALL_ROOT" ] 
        then 
                doit="yes" 
        fi 
        if [ "$PKG_INSTALL_ROOT" = "/" ] 
        then 
                doit="yes" 
        fi 
        if [ "$doit" = "yes" ] 
        then
	cmd1="${AB2BIN}/ab2admin -o stop"
	cmd2="${AB2BIN}/ab2admin -o start"
	echo "Restarting the server"
	echo "$cmd1"
	eval $cmd1
	echo "$cmd2"
	eval $cmd2
	fi

	#if we get here, then ab2admin has been run and may have
	#created ents/map.txt under the collection directory, so:

   fi

exit $?
