#!/bin/sh
#
# globus-gram-local-proxy-util
#
#  This program will write the data provided on stdin to the specified file.
#  It will set the file permissions to be read,write by the current user.
#  If the file path contains a directory that does not exist, it will be
#  created automatically.
#

GLOBUS_LOCATION="$1"
export GLOBUS_LOCATION;

COMMAND="$2"
PROXYFILE="$3"

. ${GLOBUS_LOCATION}/libexec/globus-script-initializer

# delete these if dirname is portable enough
mydirname_helper ()
{
    IFS="${IFS_save}"
    dir=
    while [ $# -gt 1 ]
    do
        dir="${dir}/${1}"
        shift
    done
echo "$dir"
}

mydirname ()
{
    IFS_save="$IFS"
    IFS="/"
    mydirname_helper $1
    IFS="${IFS_save}"
}

# change to dirname if that is portable enough
PROXYDIR=`mydirname $PROXYFILE`

case "$COMMAND" in

    -write)
        # create certificate request using openssl:
        REQUEST=`${bindir}/openssl req -newkey rsa:512 -config ${sysconfdir}/globus_gram_local_proxy_tool.conf -nodes -batch`
        if test $? -ne 0 ; then
            exit $?
        fi
        # echo request on stdout
        echo "${REQUEST}"| sed '/RSA PRIVATE KEY/,/RSA PRIVATE KEY/d'
        # get the key
        KEY=`echo "${REQUEST}" | sed -n '/RSA PRIVATE KEY/,/RSA PRIVATE KEY/p'`
        # must create proxyfile w/ 0700 perms or fail, which is the same as
        # umask of 166
        # mkdir cannot use the umask of 166, so must set mode with -m option
        # writes stdin (signed cert) to temp cert file
        set -C  # noclobber
        umask 166 
        mkdir -m 744 -p "$PROXYDIR" && cat > "$PROXYFILE.cert"
        if test $? -ne 0 ; then
            exit $?
        fi
        # the format requires proxy cert, key, cert chain
        cat "${PROXYFILE}.cert" | \
            sed -n '1,/END CERTIFICATE/p' > "${PROXYFILE}.tmp" \
            || { TMP=$?; rm "${PROXYFILE}.cert"; rm "${PROXYFILE}.tmp"; exit ${TMP}; }
        echo "${KEY}" >> "${PROXYFILE}.tmp"
        cat "${PROXYFILE}.cert" | sed '1,/END CERTIFICATE/d' >> "${PROXYFILE}.tmp"
        rm "${PROXYFILE}.cert"
        mv "${PROXYFILE}.tmp" "${PROXYFILE}"
        ;;

    -delete)
        # proxyfile should exist
        exec rm "$PROXYFILE"
        exit $?
        ;;

    -help)
        echo "globus-gram-local-proxy-util <globus location> [-help] [-write <file> | -delete <file>]"
        echo ""
        echo "   -write <file>"
        echo ""
        echo "   Emits a certificate request and writes the signed proxy "
        echo "   certificate provided on stdin to the specified file."
        echo "   The file permissions are be set to be read,write by the"
        echo "   current user only.  If the file path contains a directory that"
        echo "   does not exist, it will be created automatically."
        echo ""
        echo "   -delete <file>"
        echo ""
        echo "   Removes the file specified."

        exit 0
        ;;


    *)
        echo "unknown option ${COMMAND}, use -help to display full usage" >&2
        exit 1
        ;;

esac
