#!/bin/sh

# addkits/delkits script for ups/upd v4.x kits node
# author: Marc Mengel mengel@fnal.gov
# $Id: addkits,v 1.2 1998/05/28 18:37:17 mengel Exp $

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# site specific files

ftpd=/etc/ftpd				# location of wu-ftpd configs
wwwconf=/scratch1/www/kits/conf		# location of webserver config files
setups=/scratch1/etc/setups.sh		# setups.sh file to use
server=fnkits.fnal.gov			# our advertised server name
logfile=/scratch1/log/addkits.log	# logfile of actions
adminmail=compdiv@fnal.gov		# admin mail copy goes here

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# debugging
#ftpd=./ftpd				# location of wu-ftpd configs
#wwwconf=./conf				# location of webserver config files
#setups=/usr/local/etc/setupsII.sh	# setups.sh file to use
#server=fnkits.fnal.gov			# our advertised server name
#logfile=./log/addkits.log		# logfile of actions
#adminmail=$USER			# admin mail copy goes here
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# subroutines to ask for an answer with error checking,
# and to install file.new as file

getval() {
	var=$1
	msg=$2
	host=$3
        notok=true
	while $notok
        do
	    /bin/echo "$msg \c"
	    read $var
	    case x`eval "echo \\\$$var"`x in
	    xx)    echo "Answer must be non-null.";;
	    *\ *)  echo "Answer must not contain spaces.";;
	    *.*.*) notok=false;;
	    *) 	   notok=$host;;
	   esac
	done
}

installnew() {
	file=$1
	ln $file $file.bak
        ln -f $file.new $file
	rm -f $file.new
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# get inputs: machine name and user name

getval machine 	"Enter machine name:"	true
getval user 	"Enter user name:"	false 

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# now add/delete the system and user to the appropriate file lists

case $0 in
*addkits)
    actioned="registered"
    grep "registeredhost.*$machine" $ftpd/registeredhosts ||
      echo "class registeredhost anonymous $machine" >> $ftpd/registeredhosts

    echo "$user@$machine" >> $ftpd/registeredusers

    grep "allow $machine" $wwwconf/registeredhosts || 
       echo "allow $machine" >> $wwwconf/registeredhosts
    ;;

*delkits)
    actioned="unregistered"
    grep -v $user@$machine < $ftpd/registeredusers > $ftpd/registeredusers.new
    installnew  $ftpd/registeredusers.new
    
    # only delete the machine from the list if there are no
    # users at that machine left

    if grep $machine $ftpd/registeredusers > /dev/null 2>&1
    then
      grep -v $machine < $ftpd/registeredhosts > $ftpd/registeredhosts.new
      installnew $ftpd/registeredhosts

      grep -v $machine < $wwwconf/registeredhosts > $wwwconf/reigsteredhosts.new
      installnew  $wwwconf/reigsteredhosts.new
    fi
    ;;
esac

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Now rebuild "complicated" files from parts, splicing the
# host list in the middle

cat $ftpd/ftpaccess.1 $ftpd/registeredhosts $ftpd/ftpaccess.2 \
	> $ftpd/ftpaccess.new
installnew $ftpd/ftpaccess

cat $wwwconf/access.conf.1 $wwwconf/registeredhosts $wwwconf/access.conf.2 \
	> $wwwconf/access.conf.new
installnew $wwwconf/access.conf

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Now restart the webserver so access.conf changes happen
. $setups
ups restart apache

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Now notify the user

/usr/lib/sendmail -bm -t <<EOF
To: $user@$machine
Cc: $adminmail

The user $user on host $machine has been $actioned
for upd and ftp access on $server.

				Sincerely,
					the "addkits" script
EOF

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Log it
echo "$user@$machine $actioned `date`" >> $logfile

exit 0
