defaultDomain=".fnal.gov"
host=`uname -n`

# we need the full domain name, if no domain is there, add default one on
if expr $host : '.*\.' >/dev/null; then 
   thisHost=$host
else 
   thisHost=${host}${defaultDomain}
fi

# reuse creds cache if it is owned by enstore; otherwise, make a new one
OLDKRB5CCNAME=${KRB5CCNAME:-NONE}; export OLDKRB5CCNAME
if [ $OLDKRB5CCNAME = NONE ] || [ ! -r $OLDKRB5CCNAME ]; then
   KRB5CCNAME=/tmp/krb5cc_enstore_$$; export KRB5CCNAME
elif [ `ls -l ${KRB5CCNAME#FILE:} | awk '{print $3}'` != enstore ]; then
   KRB5CCNAME=/tmp/krb5cc_enstore_$$; export KRB5CCNAME
fi

if [ $KRB5CCNAME != $OLDKRB5CCNAME ]; then
   ungettkt() {
      /usr/krb5/bin/kdestroy -q
      if [ $OLDKRB5CCNAME != NONE ]; then KRB5CCNAME=$OLDKRB5CCNAME; fi
      unset -f ungettkt
      }
else
   ungettkt() {
      unset -f ungettkt
      }
fi

# if there is no enstorekt file, give error and exit
ENSTOREKT=/local/ups/kt/enstorekt
if [ -r $ENSTOREKT ]; then
   /usr/krb5/bin/kinit -k -t $ENSTOREKT enstore/cd/$thisHost
else
   echo "ERROR: No such file - $ENSTOREKT"
fi
