#!/bin/bash
# -*- mode: shell-script; coding: utf-8 -*-
#
# Copyright 2002-2014 Cendio AB.
# For more information, see http://www.cendio.com
#
# action: Obtaining Kerberos ticket
#

if type kinit > /dev/null 2>&1; then
    if [ -z "${KRB5CCNAME}" ]; then
        export KRB5CCNAME="${TLSESSIONDATA}/krb5cc"
    fi
    # -l without argument gives usage with both MIT and Heimdal
    if kinit -l 2>&1 | grep password-file >/dev/null; then
        # Heimdal
        stdin_arg="--password-file=STDIN"
    else
	# MIT
        stdin_arg=""
    fi
    if "${TLPREFIX}/bin/tl-sso-token-passphrase" --check; then
        "${TLPREFIX}/bin/tl-sso-token-passphrase" | kinit -c "${KRB5CCNAME}" ${stdin_arg}
    elif "${TLPREFIX}/bin/tl-sso-password" --check; then
        "${TLPREFIX}/bin/tl-sso-password" | kinit -c "${KRB5CCNAME}" ${stdin_arg}
    fi
fi
