#!/bin/sh
#
# $Id: attach.sh,v 1.2 2006/08/11 17:12:49 jdreed Exp jdreed $

hessuffix=filsys.ns.athena.mit.edu
quiet=0
bash=0
add=0
prepend=0
remove=0

output() {
    [ $quiet = 1 ] || echo "$*"
}

while getopts "bP:fr" opt; do
    case $opt in
	P)
	    if [ "$OPTARG" = "add" ]; then
		add=1
		quiet=1
	    fi
	    ;;
	b)
	    bash=1
	    ;;
	f)
	    prepend=1
	    ;;
	r)
	    remove=1
	    ;;
    esac
done

shift `expr $OPTIND - 1`



lockername=$1

echo $0 | grep -q afslocker
if [ $? = 0 ]; then
    echo "The 'afslocker' command has been renamed to 'attach'.  A compatibilty" >&2
    echo "symlink has been installed in this release.  Future releases of" >&2
    echo "this package may not support the 'afslocker' command.  Please" >&2
    echo "transition to using the 'attach' command." >&2
fi

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

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

if [ ! -d /mit ]; then
  output "/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
  output "Locker $lockername does not exist."
  exit 1
fi

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

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

# Don't recreate link if it already exists.
if [ ! -h $lockermount ]; then
  ln -s $lockerpath $lockermount
  if [ $? != 0 ]; then
    output "Failed making symlink.  Make sure /mit is writeable."
    exit 1
  fi
fi

# auth if the locker's hesiod information says we should.
if [ "$lockermode" != "n" ]; then
  # If this produces any errors, just let the user see them.
  aklog $lockerpath
fi

output "$0: $lockerpath attached to $lockermount for filesystem $lockername"

if [ $add = 1 ]; then
    for p in $ATHENA_SYS `echo $ATHENA_SYS_COMPAT | sed -e 's/:/ /g'`; do
	if [ -d "$lockermount/arch/$p/bin" ]; then
	    if [ $remove = 1 ]; then
		np=`echo $PATH | sed -e "s|$lockermount/arch/$p/bin||"`
	    else
		echo $PATH | grep -q $lockermount/arch/$p/bin
		[ $? = 0 ] && break
		if [ $prepend = 1 ]; then
		    np="$lockermount/arch/$p/bin:$PATH"
		else
		    np="$PATH:$lockermount/arch/$p/bin"
		fi
	    fi
	    if [ $bash = 1 ]; then
		echopath="PATH=$np; export PATH;"
	    else
		echopath="setenv PATH $np;"
	    fi
	    break
	fi
    done
    if [ -d "$lockermount/man" ]; then
	if [ $remove = 1 ]; then
	    mp=`echo $MANPATH | sed -e "s|$lockermount/man||"`
	else
	    echo $MANPATH | grep -q $lockermount/man
	    if [ $? != 0 ]; then
		mp="$MANPATH:"
		[ $prepend = 1 ] && mp=":$MANPATH"
		[ -z "$MANPATH" ] && mp=""
		if [ $prepend = 1 ]; then
		    mp="$lockermount/man${mp}"
		else
		    mp="${mp}$lockermount/man"
		fi
	    else
		mp=$MANPATH
	    fi
	fi
	echoman="setenv MANPATH $mp;" 
	[ $bash = 1 ] && echoman="MANPATH=$mp; export MANPATH;"
    fi
    if [ -d "$lockermount/info" ]; then
	if [ $remove = 1 ]; then
	    mp=`echo $INFOPATH | sed -e "s|$lockermount/info||"`
	else
	    echo $INFOPATH | grep -q $lockermount/info
	    if [ $? != 0 ]; then
		mp="$INFOPATH:"
		[ $prepend = 1 ] && mp=":$INFOPATH"
		[ -z "$INFOPATH" ] && mp=""
		if [ $prepend = 1 ]; then
		    mp="$lockermount/info${mp}"
		else
		    mp="${mp}$lockermount/info"
		fi
	    else
		mp=$INFOPATH
	    fi
	fi
	echoinfo="setenv INFOPATH $mp;" 
	[ $bash = 1 ] && echoinfo="INFOPATH=$mp; export INFOPATH;"
    fi


    echo "$echopath $echoman $echoinfo"
fi

exit 0
