#!/bin/bash

arg=()
case "$(hesinfo "${ATHENA_USER:-$USER}" pobox 2>/dev/null | cut -d' ' -f2)" in
    *EXCHANGE*) arg=(-disable-these-authenticators=GSSAPI) ;;
esac

if ! klist -s; then
    arg=(-disable-these-authenticators=GSSAPI)
fi

: ${ATHENA_USER=$USER}
export ATHENA_USER

# If you've customized your folder collections on an Athenified pine,
# then you probably have copied in mit.edu/hesiod/imap, which isn't
# going to work.
#
# We substitute in $ATHENA_USER rather than use the variable because
# Athena 9 doesn't have it, and besides this is presumably not being
# shared between users.
if [ -e ~/.pinerc ] && grep -qF mit.edu/hesiod/imap ~/.pinerc; then
    sed -i "s#mit\.edu/hesiod/imap#$ATHENA_USER\.mail\.mit\.edu/imap/user=$ATHENA_USER#g" ~/.pinerc
fi

exec "alpine.debathena-orig" "${arg[@]}" "$@"
