#!/bin/csh -f

# A simple script for getting general-purpose library staff info.
# Grew out of a desire for functionality similar to the good parts
# (ho ho) of Geac Electronic Mail (GEM), which the libraries have
# used in the past.

# - Matches logon name with a real name from the Libraries' internal
#   staff list (maintained by Admin Services) and user can look people
#   up either way. Does a crude similar-to report for 'close' misses
# - Gives a list of 'official' libraries mail groups if the term
#   'mailgroups' is entered as search term
# - Gives membership and administrator of specified group(only 
#   'official' library groups, flagged with "-lib" extension)   

# This script can be run from the command line, but is also called
# by the 'Staff Looker-upper' section og the Libraries Staff Gopher.
# This is pretty crude stuff, but it works well enough for its limited 
# original purpose. Still, it should be perl-ified for greater 
# functionality as Administrative Services wants to add more kinds of info 
# to the staff list. 
  
if ($#argv == 0) then      		#if no argument specified by user
echo "liblook : you need to specify the terms to lookup"
echo "usage:  liblook <term1> <term2>"
echo "        type liblook -help for more info" 
exit (0)
endif

/usr/ucb/test -r /mit/mitlibs/mailstuff/stafflist   #no use if file not accessible
if ($status != 0) then 
echo "Sorry, file(s) not currently available."
exit (0)

else
set name = $argv[1]                   	
if ($#argv == 1) then                   #if one command line argument  
					
switch ($argv[1])

	case *-lib                      #.. and it ends in "-lib"
	  grep $argv[1] /mit/mitlibs/mailstuff/stafflist > /tmp/liblook.$$  
	  if ($status > 0) then 	# no match
	  goto helper
	  else
	  echo ""
	  cat /tmp/liblook.$$	#cat the tmp file from grep
	  echo "Members are -"
        #930719 Took out some stuff below because of matching problems
        # (fgrep found sbritton and sbritt for a group even though sbritt 
        # was only member)
        # To be fixed..
	 echo ""
	 echo "Email -"
	  echo ""
	  # blanche -noauth $argv[1]  >/tmp/liblook.$$ 
         #930719 - Just do this instead for now..
         # -noauth is so it will work through gopher 
         /usr/athena/bin/blanche -noauth $argv[1] 
	  # fgrep -f /tmp/liblook.$$  /mit/mitlibs/mailstuff/stafflist 
	  echo ""			#grep the list listing through our
          endif				#master file to get full-name output
	  rm /tmp/liblook.$$	
          echo "Administrator:" `/usr/athena/bin/blanche -noauth -i $argv[1] | grep -w Owner | awk '{print $3}'`
	  exit (0)
	  breaksw
   
	case "mailgroups"		#to list current groups
        echo ""
	echo "Mailgroup --                      Athena mail Address"
	echo ""
	grep -e -lib /mit/mitlibs/mailstuff/stafflist #find lines with "-lib" 
	echo ""
        exit (0)
	breaksw

	case "-help"
	cat /mit/mitlibs/mailstuff/liblook_help
	exit (0)
	breaksw
			
	case "-all"
	cat /mit/mitlibs/mailstuff/stafflist
	exit(0)
	breaksw

	default:
	grep -i $name /mit/mitlibs/mailstuff/stafflist > /tmp/liblook.$$  #look for a name
	if ($status > 0) goto helper 
	echo ""
	echo "Name -                    Mail Address -     Phone # -   Athena mail -"
	echo ""
	# the grep -v pipe is so commented lines in stafflist don't print
	cat /tmp/liblook.$$ | grep -v ^"#"
	echo "" 
	rm /tmp/liblook.$$
	exit (0)

endsw

else
	 set name2 = $argv[2]		#if two arguments given
	 set temp = "yep"	
  	
		grep -i $name /mit/mitlibs/mailstuff/stafflist > /tmp/liblook.$$
	 	grep -i -w -s $name2 /tmp/liblook.$$   #first, grep one through the other
		if ($status == 0) then	    #to find exact match	
			echo ""
 			echo "Name -                    Mail Address -     Phone # -   Athena mail -"
                	echo ""
                	grep -i -w $name2 /tmp/liblook.$$   #display it
			echo ""
			rm /tmp/liblook.$$
			exit 0
		else			#if no exact match, do next best
			grep -i $name /mit/mitlibs/mailstuff/stafflist > /tmp/liblook.$$
			if ($status > 0) then	#no match on argv[1]
			set temp = "nope"	#hold status for later
			goto second_name	#check second argument
			else			#a match
			echo ""
			echo "No Exact Match found for your terms, however"
			echo""
			echo "..the first name you entered matches or may be close to -"
         		echo ""
			cat /tmp/liblook.$$ |grep -v ^"#"	#display it
			echo ""
			goto second_name	#now check second argument
			endif
		endif

second_name:
                        grep -i $name2 /mit/mitlibs/mailstuff/stafflist > /tmp/liblook.$$
                        if ($status > 0) then	
			rm /tmp/liblook.$$
			exit (1)
			else 			#a match on second argument
                        echo ""
                        echo "..the second name you entered matches or may be close to -"
                        echo ""
                        cat /tmp/liblook.$$ | grep -v ^"#"	#display it
			echo ""
			rm /tmp/liblook.$$
			exit (0)
                       	endif

helper:
		echo "Nothing found in Library Staff List or"
                echo "official Library Committee / Group Listings.."
                echo "Lets check to see if there's a group"
		echo "at MIT named $argv[1] ..."
                echo ""
                /usr/athena/bin/blanche -noauth -i $argv[1]
                  if ($status > 0) then
		  exit (1)
 		else 
                  /usr/athena/bin/blanche -noauth $argv[1];
                  exit(0)
		  endif
               
endif


























