#!/bin/sh

# openprop/closeprop script for ups/upd v4.x kits node
# author: Marc Mengel mengel@fnal.gov
# $Id: openprop,v 1.2 1998/05/13 14:56:48 mengel Exp $

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

ftpd=/etc/ftpd				# location of wu-ftpd configs
logfile=/scratch1/log/openprop.log	# logfile of actions
server=fnkits.fnal.gov			# our public name
adminmail=compdiv@fnal.gov		# email copies go here
genpasswd=/usr/local/systools/library/adduser.work/genpasswd
					# program to roll a password

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# debugging
#ftpd=./ftpd				# location of wu-ftpd configs
#logfile=./log/openprop.log		# logfile of actions
#server=fnkits.fnal.gov			# our public name
#adminmail=mengel@fnal.gov		# email copies go here
#genpasswd="echo bozo xbozox"		# program to roll a password
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# subroutines to ask for an answer with error checking,
# and to install file.new as file

getval() {
	var=$1
	msg=$2
	address=$3
        notok=true
	while $notok
        do
	    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=$address;;
	   esac
	done
}

installnew() {
	file=$1
	ln $file $file.bak
        ln $file.new $file
	rm -f $file.new
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# get inputs: product, and user to notify if openprop

getval product 	"Enter product:"	false

case $0 in
*openprop) getval email 	"Enter user email:"	true;;
esac

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# figure out the group name -- mostly uppercase of product, 
# but vx* are clumped under VX_DART
case $product in
vx*) group="vx_dart";;
*)   group=$product;;
esac 

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# now roll a new password for the group if closing, there's none.

case $0 in
*openprop)  actioned="opened for $email"
	    set : `$genpasswd`
	    newpass=$2
	    cryptpass=$3
	    ;;
*closeprop) actioned="closed"             
	    cryptpass='x'
	    ;;
esac

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# now actually change the password

if sed -e "s/^$group:[^:]*/$group:$cryptpass/" \
	< $ftpd/ftpgroups > $ftpd/ftpgroups.new 
then 
    installnew $ftpd/ftpgroups
else
    echo "Unable to update group password."
    echo "Was $group the right spelling?"
    exit 1
fi


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Now notify the user if its openprop

case $0 in
*openprop)

/usr/lib/sendmail -bm -t <<EOF
To: $email
Cc: $adminmail

$product has been $actioned on $server.

If you are downloading via ftp, use the ftp commands:
    quote site group $group
    quote site gpass $newpass
before downloading the file.

If you are installing via upd, give group id $group 
and passsword $newpass to upd when prompted.

			    Sincerely,
				    the "openprop" script
EOF
;;
esac

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# Log it
echo "$product $actioned `date`" >> $logfile

exit 0
