#!/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 ---
#

# Read default PKI configuration.
. /usr/share/pki/etc/pki.conf

# Read user-defined PKI configuration.
. /etc/pki/pki.conf

# 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@
COMMAND=PrettyPrintCrl


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

set_nss_default_db_type() {
    if [ -z ${NSS_DEFAULT_DB_TYPE+x} ]; then
        # NSS_DEFAULT_DB_TYPE is undefined; set 'dbm' default NSS DB type
        NSS_DEFAULT_DB_TYPE=dbm
    elif [ -z ${NSS_DEFAULT_DB_TYPE} ] ; then
        # NSS_DEFAULT_DB_TYPE is empty; set 'dbm' default NSS DB type
        NSS_DEFAULT_DB_TYPE=dbm
    else
        # normalize NSS_DEFAULT_DB_TYPE to all lowercase letters
        nss_type=`echo ${NSS_DEFAULT_DB_TYPE} | tr '[:upper:]' '[:lower:]'`
        if [ ${nss_type} = 'dbm' ] ; then
            # Always set/reset 'dbm' default NSS DB type
            NSS_DEFAULT_DB_TYPE=dbm
        elif [ ${nss_type} = 'sql' ] ; then
            # Always set/reset 'sql' default NSS DB type
            # NSS_DEFAULT_DB_TYPE=sql

            # Warn user and set 'dbm' default NSS DB type
            echo "WARNING: NSS_DEFAULT_DB_TYPE=sql is currently unsupported!"
            echo "         Resetting to NSS_DEFAULT_DB_TYPE=dbm."
            NSS_DEFAULT_DB_TYPE=dbm
        else
            # NSS_DEFAULT_DB_TYPE is invalid; set 'dbm' default NSS DB type
            echo -n "WARNING: NSS_DEFAULT_DB_TYPE='${NSS_DEFAULT_DB_TYPE}' is "
            echo "invalid!"
            echo "         Resetting to NSS_DEFAULT_DB_TYPE=dbm."
            NSS_DEFAULT_DB_TYPE=dbm
        fi
    fi
    export NSS_DEFAULT_DB_TYPE
}


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

OS=`uname -s`

if [ "${OS}" = "Linux" ] ; then
    ARCHITECTURE=`arch`
    JAVA="${JAVA_HOME}/bin/java"
    JAVA_OPTIONS=""
elif [ "${OS}" = "SunOS" ] ; then
    ARCHITECTURE=`uname -p`
    if [ "${ARCHITECTURE}" = "sparc" ] &&
       [ -d "/usr/lib/sparcv9/" ] ; then
        ARCHITECTURE="sparcv9"
    fi
    if [ "${ARCHITECTURE}" = "sparc" ] ; then
        JAVA="/usr/jdk/instances/jdk1.5.0/jre/bin/java"
        JAVA_OPTIONS=""

        LD_LIBRARY_PATH=/usr/lib:/lib
        LD_LIBRARY_PATH=/usr/lib/dirsec:${LD_LIBRARY_PATH}
        LD_LIBRARY_PATH=/usr/lib/${PRODUCT}:${LD_LIBRARY_PATH}
        export LD_LIBRARY_PATH
    elif [ "${ARCHITECTURE}" = "sparcv9" ] ; then
        JAVA="/usr/jdk/instances/jdk1.5.0/jre/bin/java"
        JAVA_OPTIONS="-d64"

        LD_LIBRARY_PATH=/usr/lib:/lib
        LD_LIBRARY_PATH=/usr/lib/dirsec:${LD_LIBRARY_PATH}
        LD_LIBRARY_PATH=/usr/lib/${PRODUCT}:${LD_LIBRARY_PATH}
        LD_LIBRARY_PATH=/usr/lib/sparcv9:/lib/sparcv9:${LD_LIBRARY_PATH}
        LD_LIBRARY_PATH=/usr/lib/sparcv9/dirsec:${LD_LIBRARY_PATH}
        LD_LIBRARY_PATH=/usr/lib/sparcv9/${PRODUCT}:${LD_LIBRARY_PATH}
        export LD_LIBRARY_PATH
    else
        invalid_architecture "${ARCHITECTURE}"
        exit 255
    fi
else
    invalid_operating_system "${OS}"
    exit 255
fi


###############################################################################
##  (5) Set the CP environment variable to determine the search              ##
##      order this command wrapper uses to find jar files.                   ##
###############################################################################

JNI_JAR_DIR=`. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $JNI_JAR_DIR`
CP=${JNI_JAR_DIR}/jss4.jar

CP=/usr/share/java/commons-codec.jar:${CP}
CP=/usr/share/java/ldapjdk.jar:${CP}
CP=/usr/share/java/${PRODUCT}/pki-nsutil.jar:${CP}
CP=/usr/share/java/${PRODUCT}/pki-cmsutil.jar:${CP}
CP=/usr/share/java/${PRODUCT}/pki-tools.jar:${CP}
export CP


###############################################################################
##  (6) Execute the java command specified by this java command wrapper      ##
##      based upon the preset LD_LIBRARY_PATH and CP environment variables.  ##
###############################################################################

if [ $# -eq 1 ] ||
   [ $# -eq 2 ]
then
    file $1 | grep 'ASCII text' > /dev/null
    if [ $? -ne 0 ] ; then
        ${JAVA} ${JAVA_OPTIONS} -cp ${CP} com.netscape.cmstools.${COMMAND}
        printf "\n"
        printf "ERROR:  '$1' is not an ASCII file!\n\n"
        printf "        First, use 'BtoA $1 $1.b64'\n"
        printf "        to convert a binary file into an ASCII file.\n\n"
        exit 255
    fi
fi

set_nss_default_db_type

${JAVA} ${JAVA_OPTIONS} -cp ${CP} com.netscape.cmstools.${COMMAND} "$@"
exit $?

