#!/bin/sh
#
# $Id: detach.sh,v 1.2 2006/08/11 17:30:01 jdreed Exp $

hessuffix=filsys.ns.athena.mit.edu
lockername=$1

if [ -z "$lockername" -o "$#" -gt 1 ]; then
  echo "Usage: $0 lockername"
  exit 1
fi

case "$lockername" in
-*)
  echo "Usage: $0 lockername"
  exit 1
  ;;
*)
  ;;
esac  

if [ ! -d /mit ]; then
  echo "/mit doesn't exist!"
  exit 1
fi

# sort lets us deal with ordered filsys records, like the sipb locker.
set -- `host -t txt $lockername.$hessuffix | sed -e 's/.*\"\(.*\)\".*/\1/' | sort -nk5`
lockertype=$1
lockerpath=$2
lockermode=$3
lockermount=$4

# This is stupid, but 'host' doesn't exit with a non-zero exit value if the 
# server returns NXDOMAIN.
if [ "$lockertype" = "Host" ]; then
  echo "Locker $lockername does not exist."
  exit 1
fi

if [ "$lockertype" != "AFS" ]; then
  echo "Locker type $lockertype not supported."
  exit 1
fi

underslashmit=`echo $lockermount | awk '/^\/mit\//'`
if [ -z "$underslashmit" ]; then
  echo "Locker $lockername mounts on $lockermount, not under /mit."
  exit 1
fi

# Don't recreate link if it already exists.
if [ ! -h $lockermount ]; then
    echo "Locker does not appear to be mounted."
    exit 1
else
    rm $lockermount
    if [ $? != 0 ]; then
	echo "Failed to remove mountpoint $lockermount"
	exit 1
    fi
fi

echo "$0: $lockermount detached for filesystem $lockername"


exit 0
