#!/bin/sh

backupPinerc () {
    if [ -f $HOME/.pinerc ]; then
	echo "Backing up existing pine configuration..."
	cp -v $HOME/.pinerc $HOME/.pinerc.old
    fi
}

checkExpiredTickets() {
    expiry=`klist -4 2>/dev/null | grep -i krbtgt | awk -F'  ' '{ print $2;}'`
    now=`date +%D\ %T`
    if [ `expr "$expiry" "<" "$now"` -eq 1 ]; then
	echo "Your kerberos 4 tickets have expired or do not exist."
	if [ -x /usr/kerberos/bin/krb524init ]; then
	    /usr/kerberos/bin/krb524init &>/dev/null
	    if [ $? = 0 ]; then
		return
	    fi
	fi
	cat <<EOF
You do not have valid Kerberos 4 tickets, and new ones could not
be obtained using your Kerberos 5 tickets because they were expired, 
non-existent, or krb524init could not be executed.  You should
probably quit, and run "kinit -45" to obtain new tickets.  If you
continue, you will be unable to access your IMAP folder, but will still
be able to access any local mail folders.

EOF
	echo -n "Continue? (y/n) "
	read answer
	case $answer in
	    y|Y)
		;;
	    n|N)
		exit
		;;
	    *)
		echo "Invalid answer, assuming 'n'"
		exit
		;;
	esac
    fi
}

writeVersionHack () {
    if [ ! -f $HOME/.pinerc ]; then
	cat <<EOF > $HOME/.pinerc
# Hack to punt the last-version stuff
last-version-used=z

EOF
    fi
}

writeLocalConfig () {
    cat <<EOF > $HOME/.pinerc
# A bare-bones pine config to check the local mail spool 
#
# Path of (local or remote) INBOX, e.g. ={mail.somewhere.edu}inbox
# Normal Unix default is the local INBOX (usually /usr/spool/mail/$USER).
inbox-path=/var/spool/mail/$USER

# List of directories where saved-message folders may be. First one is
# the default for Saves. Example: Main {host1}mail/[], Desktop mail\[]
# Syntax: optnl-label {optnl-imap-hostname}optnl-directory-path[]
folder-collections=Main ~/mail/[]

# Hack to punt the last-version stuff
last-version-used=z

EOF
}

if [ -f /etc/athena/version ]; then
    echo "This appears to be an Athena machine.  The mit-pine package"
    echo "will not function correctly.  Please remove it."
    exit
fi
    
if [ -n "$1" ]; then
    if [ "$1" == "--install-local-config" ]; then
	backupPinerc
	echo -n "Writing local configuration..."
	writeLocalConfig
	echo "done";
	echo "The next time you run pine, it will use the local configuration."
	# Otherwise we have to deal with shifting arguments and passing
	# them onto pine.real
	exit
    fi
fi

# In case they're su'd or something, $USER doesn't work
UNAME=`/usr/bin/id -nu`
# The package requires Hesiod, so hesinfo should exist
#HESTEST=`/usr/bin/hesinfo $UNAME pobox 2>/dev/null`
/usr/bin/host -t txt $UNAME.pobox.ns.athena.mit.edu >/dev/null

#if [ -z "$HESTEST" ]; then 
if [ $? != 0 ]; then 
    # Probably not an MIT user
    if [ ! -f $HOME/.pinerc ]; then
	continue=
	while :; do
	    echo "You do not appear to have run pine before, and you do not"
	    echo "appear to have an MIT pobox (or the Hesiod lookup failed"
	    echo "for some reason).  If you choose to continue without"
	    echo "installing a default configuration, Pine will use the MIT"
	    echo "configuration, which will cause problems if you do not have"
	    echo "an MIT pobox."
	    echo ""
	    echo -n "Shall I install a default Pine configuration instead? (y/n):"
	    read answer
	    case $answer in
		y|Y)
		    writeLocalConfig
		    continue="y"
		    ;;
		n|N)
		    continue="n"
		    ;;
		*)
		    echo "Unexpected response, please answer 'y' or 'n'."
		    echo
		    ;;
	    esac
	    if [ -n "$continue" ]; then
		break
	    fi
	done
    fi
else
    checkExpiredTickets
    writeVersionHack
fi

exec pine.real "$@"
