#!/bin/sh
# --- BEGIN COPYRIGHT BLOCK ---
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301  USA 
# 
# Copyright (C) 2007 Red Hat, Inc.
# All rights reserved.
# --- END COPYRIGHT BLOCK ---

# Check to insure that this script's original invocation directory
# has not been deleted!
CWD=`/bin/pwd > /dev/null 2>&1`
if [ $? -ne 0 ] ; then
	echo "Cannot invoke '$0' from non-existent directory!"
	exit 255
fi


###############################################################################
##  (1) Specify variables used by this script.                               ##
###############################################################################

PRODUCT=[PKI_PRODUCT]
SUBSYSTEM=[PKI_SUBSYSTEM]
INSTANCE=[PKI_INSTANCE]
COMMAND=[PKI_COMMAND]


###############################################################################
##  (2) Define helper functions.                                             ##
###############################################################################

invalid_operating_system() {
   	echo
   	echo "ERROR:  '$0' does not execute on the '$1' operating system!"
   	echo
}

invalid_architecture() {
   	echo
   	echo "ERROR:  '$0' does not execute on the '$1' architecture!"
   	echo
}


###############################################################################
##  (3) Set environment variables.                                           ##
##                                                                           ##
##      Set the LD_LIBRARY_PATH environment variable to determine the        ##
##      search order this command wrapper uses to find shared libraries.     ##
##                                                                           ##
##      Set the PATH environment variable to determine the search            ##
##      order this command wrapper uses to find binary executables.          ##
##                                                                           ##
##      NOTE:  Since the wrappers themselves are ALWAYS located in           ##
##             "/usr/bin" on 32-bit and 64-bit Linux as well as both         ##
##             32-bit Solaris and 64-bit Solaris, this directory             ##
##             will always be excluded from the search path.                 ##
##                                                                           ##
##             Additionally, since "/bin" is nothing more than a symbolic    ##
##             link to "/usr/bin" on Solaris, this directory will also       ##
##             always be excluded from the search path on this platform.     ##
##                                                                           ##
###############################################################################

OS=`uname -s`
ARCHITECTURE=""

if [ "${OS}" = "Linux" ] ; then
	ARCHITECTURE=`arch`
	if [ "${ARCHITECTURE}" = "i686" ] ; then
		PATH=/usr/lib/${PRODUCT}:/bin
		PATH=/usr/lib/${PRODUCT}/${SUBSYSTEM}:${PATH}
		PATH=/var/lib/${INSTANCE}:${PATH}
		export PATH
	elif [ "${ARCHITECTURE}" = "x86_64" ] ; then
		PATH=/usr/lib/${PRODUCT}
		PATH=/usr/lib/${PRODUCT}/${SUBSYSTEM}:${PATH}
		PATH=/var/lib/${INSTANCE}:${PATH}
		PATH=/usr/lib64/${PRODUCT}:/bin:${PATH}
		PATH=/usr/lib64/${PRODUCT}/${SUBSYSTEM}:${PATH}
		export PATH
	else
		invalid_architecture "${ARCHITECTURE}"
		exit 255
	fi
elif [ "${OS}" = "SunOS" ] ; then
	ARCHITECTURE=`uname -p`
	if	[ "${ARCHITECTURE}" = "sparc" ] &&
		[ -d "/usr/lib/sparcv9/" ] ; then
		ARCHITECTURE="sparcv9"
	fi
	if [ "${ARCHITECTURE}" = "sparc" ] ; then
		LD_LIBRARY_PATH=/usr/lib/java:/usr/lib:/lib
		LD_LIBRARY_PATH=/usr/lib/${PRODUCT}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/${PRODUCT}/${SUBSYSTEM}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/var/lib/${INSTANCE}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/dirsec:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/java/dirsec:${LD_LIBRARY_PATH}
		export LD_LIBRARY_PATH

		PATH=/usr/lib/${PRODUCT}
		PATH=/usr/lib/${PRODUCT}/${SUBSYSTEM}:${PATH}
		PATH=/var/lib/${INSTANCE}:${PATH}
		export PATH
	elif [ "${ARCHITECTURE}" = "sparcv9" ] ; then
		LD_LIBRARY_PATH=/usr/lib/java:/usr/lib:/lib
		LD_LIBRARY_PATH=/usr/lib/${PRODUCT}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/${PRODUCT}/${SUBSYSTEM}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/var/lib/${INSTANCE}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/dirsec:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/java/dirsec:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/sparcv9:/lib/sparcv9:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/sparcv9/java:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/sparcv9/${PRODUCT}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/sparcv9/${PRODUCT}/${SUBSYSTEM}:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/sparcv9/dirsec:${LD_LIBRARY_PATH}
		LD_LIBRARY_PATH=/usr/lib/sparcv9/java/dirsec:${LD_LIBRARY_PATH}
		export LD_LIBRARY_PATH

		PATH=/usr/bin/sparcv9
		PATH=/usr/lib/${PRODUCT}:${PATH}
		PATH=/usr/lib/${PRODUCT}/${SUBSYSTEM}:${PATH}
		PATH=/var/lib/${INSTANCE}:${PATH}
		PATH=/usr/lib/sparcv9/${PRODUCT}:${PATH}
		PATH=/usr/lib/sparcv9/${PRODUCT}/${SUBSYSTEM}:${PATH}
		export PATH
	else
		invalid_architecture "${ARCHITECTURE}"
		exit 255
	fi
else
	invalid_operating_system "${OS}"
	exit 255
fi


###############################################################################
##  (4) Execute the binary executable specified by this command wrapper      ##
##      based upon the preset LD_LIBRARY_PATH and PATH environment variables.##
###############################################################################

ORIGINAL_IFS=${IFS}
IFS=:

for dir in ${PATH}
do
	if [ -x ${dir}/${COMMAND} ]
	then
		IFS=${ORIGINAL_IFS}
		${dir}/${COMMAND} "$@"
		exit $?
	fi
done

echo "Unable to find \"${COMMAND}\" in \"${PATH}\"!"

exit 255

