#!/bin/sh
#
# --- BEGIN COPYRIGHT BLOCK ---
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; 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 ---
#

# load default, system-wide, and user-specific PKI configuration and
# set NSS_DEFAULT_DB_TYPE.
. /usr/share/pki/scripts/config

# Check to insure that this script's original invocation directory
# has not been deleted!

# shellcheck disable=SC2034
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
COMMAND=PKCS10Client


###############################################################################
##  (2) Check for valid usage of this command wrapper.                       ##
###############################################################################



###############################################################################
##  (3) 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
}

###############################################################################
##  (4) Set the LD_LIBRARY_PATH environment variable to determine the        ##
##      search order this command wrapper uses to find shared libraries.     ##
###############################################################################

OS=`uname -s`

ARCHITECTURE=`arch`
if [ -e "${PKI_JAVA_PATH}" ]; then
    JAVA="${PKI_JAVA_PATH}"
elif [ -e "${JAVA_HOME}/jre/bin/java" ]; then
    JAVA="${JAVA_HOME}/jre/bin/java"
elif [ -e "${JAVA_HOME}/bin/java" ]; then
    JAVA="${JAVA_HOME}/bin/java"
else
    JAVA="/usr/bin/env java"
fi
JAVA_OPTIONS=""

###############################################################################
##  (5) Execute the java command specified by this java command wrapper      ##
##      based upon the LD_LIBRARY_PATH and PKI_LIB environment variables.    ##
###############################################################################

${JAVA} ${JAVA_OPTIONS} \
  -cp "${PKI_LIB}/*" \
  -Djava.util.logging.config.file=${PKI_LOGGING_CONFIG} \
  com.netscape.cmstools.${COMMAND} "$@"

exit $?

