#!/bin/sh

# This script is very similar to the script in the athena
# release, but if the user uses .athena-sipb-pine-noask, we make
# sure that we re-ask them their preference once per year.

mailer=
if [ -f $HOME/.athena-pine-noask ]; then
  mailer=pine
elif [ -f $HOME/.athena-sipb-pine-noask ]; then
  noask=`find $HOME/.athena-sipb-pine-noask -not -mtime +365 -print`
  if [ -z "$noask" ]; then
    # The "find" ensures that the file is less than 365 days old.
    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
fi

if [ -f $HOME/.pine-lock ]; then
echo You seem to have run another copy of pine recently, as process
cat $HOME/.pine-lock
cat <<'EOF'
Running more than one copy of pine at the same time can corrupt your inbox
and cause your mail to be destroyed.  If you\'re certain that this old copy
of pine is not still running, type \'y\' to start a new pine.  Otherwise,
type \'n\', and then go make certain that your old pine is no longer
running.

Do you want to start pine now (y/n)?
EOF
  choice=""
  while [ "$choice" != "y" ]; do
    read choice
    case $choice in
    n)
      exit
      ;;
    y)
      break
      ;;
    *)
      echo
      echo "Option $choice not recognised; please try again."
      echo
      ;;
    esac
  done
fi

# 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.
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

echo $$ on `hostname` `date` >$HOME/.pine-lock

case $mailer in
pine)
  if [ -f /usr/athena/bin/pine.real ]; then
    /usr/athena/bin/pine.real "$@"
  elif [ -f /usr/athena/bin/pine ]; then
    /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
    /bin/athena/attachandrun sipb pine.sipb "$0" -P /mit/sipb/share/pine-athena.conf -incoming-source-folders= "$@"
  fi
  ;;
sipb-pine)
  /bin/athena/attachandrun sipb pine.sipb "$0" "$@"
  ;;
esac

rm -f $HOME/.pine-lock
