#!/usr/bin/env pagsh
#Origionally from /mit/kchen  modified a little

screen=`which screen`
if [ ! -x "$screen" ]; then
    attach -n -q gnu
    screen=`athdir /mit/gnu`/screen
fi

# Check for pre-existing screens.
screenlist=`$screen -list | grep '[0-9]\.owl'`
if [ "x$screenlist" != "x" ]; then
    screen -dr owl
    exit 0
fi

# Get AFS tokens for the screen session.
# We are now in a new PAG (because of pagsh).
# If you want to get tokens for other cells, list them in ~/.xlog
aklog
aklog sipb

# Use separate kerberos tickets for screen. Even if we accidentally
# (or intentionally) log out, screen and owl will still have tickets.
NEWKRB5=/tmp/krb5cc_${USER}_screen

# Copy the tickets we obtained on login to the screen tickets.
KRB5CCNAME=`echo $KRB5CCNAME | sed 's/FILE://'`
cp $KRB5CCNAME $NEWKRB5

# Now actually use the new tickets.
KRB5CCNAME=FILE:$NEWKRB5; export KRB5CCNAME

# Repeat for krb4 if appropriate.
if [ "x$KRBTKFILE" != "x" ]; then
   NEWKRB4=/tmp/tkt_${USER}_screen
   KRBTKFILE=`echo $KRBTKFILE | sed 's/FILE://'`
   cp $KRBTKFILE $NEWKRB4
   KRBTKFILE=$NEWKRB4; export KRBTKFILE
fi

# Figure out which zephyr client to use.  Defaults to barnowl.
if [ "x$ZEPHYR_SCREEN_CLIENT" = "x" ]; then
    barnowl=`which barnowl`
    if [ ! -x "$barnowl" ]; then
	barnowl=`athdir /mit/barnowl`/barnowl
    fi
    ZEPHYR_SCREEN_CLIENT="$barnowl"
fi

# Renew tickets and tokens.
contrenew=`which cont-renew-notify`
if [ ! -x "$contrenew" ]; then
    contrenew=`athdir /mit/pweaver`/cont-renew-notify
fi
nohup $contrenew < /dev/null > /dev/null 2> /dev/null &

# Name the screen owl so we can reattach the right screen later.
$screen -S owl $ZEPHYR_SCREEN_CLIENT
