#!/bin/bash

if [ "$initlname" = "" ]; then
  if [ "$lname" = "" ]; then
    initlname=0
  else
    initlname=1
  fi
fi

if type wget >/dev/null 2>/dev/null; then
  WGET=wget
else
  WGET="athrun gnu wget"
fi

if [ "$lname" = "" ]; then
  if [ "$1" = "" ]; then
    echo
    echo "Would you like to sign up:"
    echo "1. Your personal Athena account"
    echo "2. A locker that you control (a club, a course, etc)"
    echo "If you do not understand this question, you should answer '1'."
    printf "Please enter either '1' or '2' (without quotes): "
    read whofor
    if [ "$whofor" = 1 ]; then
        lname="${ATHENA_USER:-$USER}"
    elif [ "$whofor" = 2 ]; then
        echo
        echo "OK.  A locker of your choice that you control will be signed up."
        echo "Please enter the name of the selected locker below."
        echo "(For the locker /mit/sipb, you would enter sipb)."
        printf "Locker name: "
        read lname
    else
        echo
        echo "ERROR:"
        echo "You must select either '1' or '2'."
        exit 1
    fi
    while true; do
        if attach "$lname"; then
            break
        fi
        echo "$lname is not a valid locker name."
        printf "Locker name: "
        read lname
    done
  else
    lname="$1"
  fi
fi
lroot="/mit/$lname" 

attach "$lname" 2>/dev/null

ans=`$WGET -q -O- "http://scripts.mit.edu/~signup/fsla.php/mit/$lname"`
if [ "$ans" != "0" ]; then
  echo
  echo "ERROR:"
  echo "The scripts servers cannot verify the permissions of the locker <$lname>."
  echo "This is probably because your locker is not publicly listable."
  echo "You can remedy this signup problem and make your locker publicly"
  echo "listable by running \"fs setacl /mit/$lname system:anyuser l\""
  echo "(that's a lowercase L at the end)."
  echo ""
  echo "NOTE: This will make it possible for the public (including anyone"
  echo "viewing http://web.mit.edu/$lname) to see the names of your files"
  echo "and the list of people who have access to them, though it will not"
  echo "cause the contents of your files to be publicly readable.  If you"
  echo "are unwilling to have your locker listable by the public, please"
  echo "contact scripts@mit.edu for information about other ways to work"
  echo "around the problem, or see http://scripts.mit.edu/faq/122 for more"
  echo "detailed information."
  exit 1
fi

principal=`klist -5 | sed -n 's/^Default principal: // p'`
ans=`$WGET -q -O- "http://scripts.mit.edu/~signup/admof.php/$lname/$principal"`
if [ "$ans" != "yes" ]; then
  afsuser=`echo "$principal" | sed 's/@ATHENA.MIT.EDU$//'`
  echo
  echo "ERROR:"
  echo "It appears as though you are not an administrator of the locker <$lname>."
  echo "Try running \"fs sa /mit/$lname $afsuser all\" and starting over."
  echo "Contact scripts@mit.edu if you are unable to solve the problem."
  exit 1
fi

mkdir -p "/mit/$lname/.scripts-signup"

if [ ! -d "/mit/$lname/.scripts-signup" ]; then
  echo
  echo "ERROR:"
  echo "It appears as though you do not have write access to the locker <$lname>."
  echo "Contact scripts@mit.edu if you are unable to solve the problem."
  exit 1
fi

ans=`$WGET -q -O- "http://scripts.mit.edu/~signup/$lname"`
rmdir "/mit/$lname/.scripts-signup"

if [ "$ans" = "done" ]; then
  # nscd caches account nonexistence with a 5-second TTL.
  # (LDAP updates are more or less instant.)
  # Somehow, the server can wait up to 10 seconds...
  echo "Creating scripts.mit.edu account for $lname..."
  sleep 10
fi

if [ "$ans" != "done" ] && [ "$ans" != "username already taken" ]; then
  echo "ERROR:"
  echo "Signup reported the following error: \"$ans\"."
  echo "Contact scripts@mit.edu for assistance."
  exit 1
fi

success() {
  if [ "$initlname" -eq 0 ]; then
    echo
    echo "== SUCCESS =="
    echo "$lname is now signed up for $1."
    echo "$2"
    echo
  fi
}
