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

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

COMMAND=PrettyPrintCert

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

###############################################################################
##  (3) Define helper functions.                                             ##
###############################################################################

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

JAVA="${JAVA_HOME}/bin/java"
JAVA_OPTIONS=""

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

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

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

exit $?
