#!/usr/sue/bin/pagsh
##############################################################################
#
# FILE: aexec
#
# DATE: 24-Ago-95
#
# SYNTAX: aexec <time> [-p <file>] [-bg] <command> [[parameter]...]
#
#	o <time>, interval in seconds between consecutive refreshings of the
#	  AFS token.
#	o -p <file>, reads the password from the file <file>.
#	o -bg, to execute the job in the background.
#	o <command> [[parameter]...], command that has need of a long
#         lasting AFS token.
#
# DESCRIPTION: Submit a job under a shell with a non-ending afs token. When
# the job submitted finishes, aexec returns and the afs tokens is removed.
# The job can be executed in the background.
#
# WARNINGS: If aexec gets killed, the process that refreshes the afs
# token remains in the background, so it must be killed by sending
# a signal to it. Its process ID can be localized issuing a
# "ps -ef | grep reauth" command (flags for ps are system dependent).
#
# AUTHOR: Francisco Jose Lozano Alemany. CN/DCI Division. CERN.
#
############################################################################## 


cat <<EOFmessage

  The use of 'aexec' is deprecated and the tool will be removed 
  in an upcoming release of the afs_tools package. 

  To get refresh Kerberos5 and AFS credentials, please use 
  'k5reauth'.

  Will sleep for 30 seconds now...

EOFmessage

sleep 30


PATH=$PATH:/afs/usr/local/etc

if [ $# -eq 0 ]; then 
    echo "Usage: aexec <time> [-p <file>] [-bg] <command> [[parameter]...]"
    exit 0
fi

time=$1
if [ $2 = "-p" ]; then
    if [ -r $3 ]; then
        read password dummy < $3
    else
	echo "Cannot read password file ($3)." >&2
	exit 1
    fi
    shift 3
else
    shift 
fi


# File descriptor 7 used to get pid of reauth
pid=`reauth $time $LOGNAME $password -pid 7>&1 >/dev/tty` 
rc=$?
if [ $rc -eq 1 ]; then
    echo "reauth failed. Exit code: $rc"
    exit 1
fi
 
if [ $1 = "-bg" ]; then
    shift
    /bin/sh -c "$*;kill $pid" &
else
    $*
    kill $pid
fi
