#! /bin/sh

# This script is similar to the script in the athena release.
# The two main differences are:
#  (1) If the user has set .athena-sipb-pine-noask, we make
#      sure that we re-ask them their preference once per year.
#  (2) If the user runs sipb-pine, we monitor a .pine-lock file
#      to try to prevent them from running two pines at once.



# Part 1: Deciding what to do.
######################################################################
# If .athena-pine-noask exists,                     run athena pine
# If .athena-sipb-pine-noask exists, and is recent, run sipb pine
#  "     "          "          "   ,  and is old,   ask which pine
# If the user has never run pine,                   run athena pine
# If the user came here from the athena wrapper,    run sipb pine
# Else                                              ask which pine

mailer=
if [ -f $HOME/.athena-pine-noask ]; then
  mailer=pine
elif [ -f $HOME/.athena-sipb-pine-noask ]; then
  yearold=`find $HOME/.athena-sipb-pine-noask -mtime +365 -print`
  # The "find" only prints if the file is more than 365 days old.
  if [ -z "$yearold" ]; then
    mailer=sipb-pine
  fi
elif [ ! -f $HOME/.pinerc ]; then
  # User has never run pine before; just run Athena pine.
  mailer=pine
  touch $HOME/.athena-pine-noask
elif type pine | grep /usr/athena/bin/pine >/dev/null ; then
  # The user (probably) ran us by explicit path.
  # Do not set a *-noask file, because they probably came
  # here from option "2" or "4" of the athena wrapper.
  mailer=sipb-pine
fi



# Part 2: (Maybe) Ask the user which pine to run.
######################################################################
# There are two ways to have a zero length "$mailer".
# Either the user has a .pinerc but no *-noask file, or the user
#  has a .athena-sipb-pine-noask file that is more than a year old.
#  Both of those conditions means we need to ask which pine to run.

if [ -z "$mailer" ]; then
  while :; do
      cat <<'EOF'
You are running SIPB pine.  There is a newer version of Pine
installed on Athena, and we recommend that you run Athena's pine
instead of SIPB pine.  If you normally use SIPB pine, the biggest
difference you will notice is that Athena pine does not download
your mail into your Athena home directory.  (Your old mail is
still here.  It is in "Old Pine Mail" <~/mail> of the Folder List.)
EOF
    echo
    echo "Please choose one of the following options:"
    echo "  1. Let me use the Athena pine, but ask me again next time"
    echo "  2. Let me use the old SIPB pine, but ask me again next time"
    echo "  3. I want to use the Athena version of pine; don't ask me again"
    echo "  4. I want to use the old SIPB pine; don't ask me again"
    echo "  5. Cancel"
    echo ""
    printf "Please choose an option (1-5): "
    read choice
    case $choice in
    1)
      mailer=pine
      ;;
    2)
      mailer=sipb-pine
      ;;
    3)
      mailer=pine
      touch $HOME/.athena-pine-noask
      ;;
    4)
      mailer=sipb-pine
      touch $HOME/.athena-sipb-pine-noask
      ;;
    5)
      exit
      ;;
    *)
      echo
      echo "Option $choice not recognized; please try again."
      echo
      ;;
    esac
    if [ -n "$mailer" ]; then
      break
    fi
  done
fi



# Part 3: Start the appropriate pine
######################################################################
# For the case of athena pine, we try to run the raw athena binary,
#  and then try to run the athena script wrapper, and then fall back
#  to running our own pine with a (tweaked) copy of Athena's config file.
# For the case of sipb pine, we have a wrapper that warns the user
#  against running multiple sipb-pine's at once.  (Auto-inc'ing from
#  the server plus AFS lockfile bugs can result in lost mail when
#  two pines are writing to the ~/mail/inbox at once.)

case $mailer in
pine)
  if [ -f /usr/athena/bin/pine.real ]; then
    exec /usr/athena/bin/pine.real "$@"
  elif [ -f /usr/athena/bin/pine ]; then
    exec /usr/athena/bin/pine "$@"
  else
    echo "WARNING: Could not find athena's pine."
    echo "         Running (sipb) pine in safe, athena-compatible mode."
    sleep 3
# The blank value of "incoming-source-folders" is to negate the option,
#  if it happens to be set in an individual user's .pinerc file
    exec /bin/athena/attachandrun sipb pine-imap "$0" -P /mit/sipb/share/pine-athena.conf -incoming-source-folders= "$@"
  fi
  ;;
sipb-pine)
# Use a dialog to warn against running multiple pines at once.
  if [ -f $HOME/.pine-lock ]; then
    while :; do
      echo "You seem to have run another copy of pine recently, as process"
      cat $HOME/.pine-lock
      echo "Running more than one copy of pine at the same time can corrupt"
      echo "your inbox and cause your mail to be destroyed.  If you're"
      echo "certain that this old copy of pine is not still running, type"
      echo "'y' to start a new pine.  Otherwise, type 'n', and then go make"
      echo "certain that your old pine is no longer running."
      echo ""
      printf "Do you want to start pine now? (y/n): "
      read choice
      case $choice in
      n*|N*)
        exit
        ;;
      y*|Y*)
        break
        ;;
      *)
        echo
        echo "Option $choice not recognised; please try again."
        echo
        ;;
      esac
    done
  fi

  echo $$ on `hostname` \(`date`\) >$HOME/.pine-lock
  /bin/athena/attachandrun sipb pine-imap "$0" "$@"
  rm -f $HOME/.pine-lock
  ;;
esac
