#!/bin/sh
#  Copyright (c) 2017, 2020, Oracle and/or its affiliates.
#  This software code contains proprietary information
#  and should be treated as confidential.

. "${INSTDIR}/bin/scripts/common"

[ -s "${INSTDIR}/etc/version" ] && . ${INSTDIR}/etc/version

# Set up awk correctly
which nawk 1>/dev/null 2>&1
if [ $? -eq 0 ]
then
    AWK="nawk"
else
    AWK="awk"
fi

# Usage message for clientcert
usage_clientcert_and_exit() {
    echo "${i18n_clientcertusage1O_gw}"
    exit 1
}

# Usage message for clientcert import
usage_clientcert_import_and_exit() {
    echo "${i18n_clientcertimportusage1O_gw}"
    exit 1
}

# Usage message for clientcert list
usage_clientcert_list_and_exit() {
    echo "${i18n_clientcertlistusage1O_gw}"
    exit 1
}

# Usage message for clientcert remove
usage_clientcert_remove_and_exit() {
    echo "${i18n_clientcertremoveusage1O_gw}"
    exit 1
}

# Return the keystore password
get_password() {
    cat ${INSTDIR}/etc/password
    return 0
}

# Import a certificate with the given alias into the client keystore
# Args : $1 alias to be added to the keystore
#        $2 file containing the certiifcate to be added
#
clientcert_import() {
    ALIAS="$1"
    CERTFILE="$2"

    [ -z "${ALIAS}" -o -z "${CERTFILE}" ] && {
        usage_clientcert_import_and_exit
    }

    # Check whether the file path is absolute or relative
    case "${CERTFILE}" in
        /*)
            # Already absolute so do nothing
            ;;
        *)
            CERTFILE="${ORIGDIR}/${CERTFILE}"
            ;;
    esac
    
    [ ! -f "${CERTFILE}" ] && {
        printf "${i18n_invalidarg01_gw}" "${CERTFILE}"
        usage_clientcert_import_and_exit
    }

    PASSWORD=`get_password`
    ${GW_JAVA_DIR}/keytool -importcert -noprompt \
        -alias "${ALIAS}" \
        -keystore "${INSTDIR}/proxy/etc/keystore.client" \
        -storepass "${PASSWORD}" \
        -file "${CERTFILE}"
    status=$?
    [ "${status}" -ne 0 ] && {
        printf "${i18n_certimportfailed01_gw}" "${status} (${CERTFILE})"
    	exit 1
    }
}

# List the certificates in the gateway's client keystore.
clientcert_list() {

    PASSWORD=`get_password`
    CERTS=`${GW_JAVA_DIR}/keytool -list \
        -keystore "${INSTDIR}/proxy/etc/keystore.client" \
        -storepass "${PASSWORD}"`

    # Print out the trustedCertEntries and the line below each one
    echo "${CERTS}" | ${AWK} '\
     BEGIN { FS="," }
     /.*trustedCertEntry.*/ {
         show_next_cert = 1;
         print $1" "$2" "$3;
         next
     }
     show_next_cert {
         print $0;
         print "";
         show_next_cert = 0;
         next
     }
    '
}

# Remove a certificate with the given alias from the client keystore
# Args : $1 alias to be removed from the keystore
#
clientcert_remove() {
    ALIAS="$1"

    [ -z "${ALIAS}" ] && {
        usage_clientcert_remove_and_exit
    }

    PASSWORD=`get_password`
    ${GW_JAVA_DIR}/keytool -delete \
        -alias "${ALIAS}" \
        -keystore "${INSTDIR}/proxy/etc/keystore.client" \
        -storepass "${PASSWORD}"
    status=$?
    [ "${status}" -ne 0 ] && {
        printf "${i18n_deletefailed01_gw}" "${status} (${ALIAS})"
        exit 1
    }
}

case "$2" in
    import)
        while [ -n "$3" ]; do
            case "$3" in
                --alias)
                    case "$4" in
                        "" | --*) printf "${i18n_invalidarg01_gw}" "$3"
                    esac
                    ALIAS="$4"
                    shift; shift
                    ;;
                --certfile)
                    case "$4" in
                        "" | --*) printf "${i18n_invalidarg01_gw}" "$3"
                    esac
                    CERTFILE="$4"
                    shift; shift
                    ;;
                --help)
                    usage_clientcert_import_and_exit
                    ;;
                *)
                    printf "${i18n_invalidarg01_gw}" "$3"
                    usage_clientcert_import_and_exit
            esac
        done
        clientcert_import "${ALIAS}" "${CERTFILE}"
        ;;
    list)
        while [ -n "$3" ]; do
            case "$3" in
                --help)
                    usage_clientcert_list_and_exit
                    ;;
                *)
                    printf "${i18n_invalidarg01_gw}" "$3"
                    usage_clientcert_list_and_exit
                    ;;
            esac
        done
        clientcert_list
        ;;
    remove)
        while [ -n "$3" ]; do
            case "$3" in
                --alias)
                    case "$4" in
                        "" | --*) printf "${i18n_invalidarg01_gw}" "$3"
                    esac
                    ALIAS="$4"
                    shift; shift
                    ;;
                --help)
                    usage_clientcert_remove_and_exit
                    ;;
                *)
                    printf "${i18n_invalidarg01_gw}" "$3"
                    usage_clientcert_remove_and_exit
                    ;;
            esac
        done
        clientcert_remove "${ALIAS}"
        ;;
    *)
        usage_clientcert_and_exit
        ;;
esac
