#!	/bin/csh -f
#
# Copyright 1993, 1994, 1995 by the Regents of the University of California.
# see the file "Copyright" in the distribution for conditions of use.
#
#
#	mailing list routines
#	arg is "create", "remove", "edit", or "display"
#
if( $#argv != 1 ) then
	echo "Usage: $0 [create|remove|edit|display]"
	exit 1
endif
#
set awkf = `mktemp -pml`
if( $status ) exit 1
#
cat << "EOT" > $awkf
BEGIN {  ok_chars = "abcdefghijklmnopqrstuvwxyz0123456789"; }
{
	if( FILENAME == "/usr/lib/aliases.inc/.cons" ) {
		ok_spec = $0;
	} else {
		n = length( $0 );
		if( n < 9 ) {
			print "*** Name must be at least 9 characters in length";
			exit 1;
		}
		if( n > 30 ) {
			print "*** Name must be less than 30 characters in length";
			exit 1;
		}
		if( index( $0, " " ) ) {
			print "*** Name may not contain blanks";
			exit 1;
		}
		for( i = 0; i <= n; i++ ) {
			c = substr( $0, i, 1 );
			if( index(ok_chars,c) == 0 && index(ok_spec,c) == 0 ) {
				printf "*** Invalid character \"%s\" in name\n", c;
				exit 1;
			}
		}
		exit 0;
	}
}
"EOT"
#
retry:
echo " "
echo -n Enter name of mailing list to $1 or '"quit" to quit: '
set name = $<
#
set y = ( $name )
if( $#y == 0 ) goto done
if( $#y > 1 ) then
	echo "*** Blanks not allowed in names"
	goto retry
endif
if( $name == "quit" ) goto done
#
echo $name | awk -f $awkf /usr/lib/aliases.inc/.cons -
if( $status ) goto retry;
#
#
#	now perform desired operation
#
set file = /usr/lib/aliases.inc/$name
#
if( $1 == "create" ) then
	if( -e $file ) then
		set y = `ls -l $file`
		if( $y[3] == $USER ) then
			echo "*** You already own mailing list" '"'$name'".'
			goto retry
		else
			echo "*** A mailing list" '"'$name'"' owned by $y[3] already exists.
		endif
		goto retry;
	endif
	maillist $name
else if( $1 == "remove" ) then
	if( ! -e $file ) then
		echo "*** There is no mailing list named" '"'$name'".'
		goto retry
	endif
	set y = `ls -l $file`
	if( $y[3] != $USER ) then
		echo "*** You don't own" '"'$name'"', it is owned by $y[3].
		goto retry;
	endif
	echo Removing mailing list '"'$name'"'.
	maillist -r $name
else if( $1 == "display" ) then
	if( ! -e $file ) then
		echo "*** There is no mailing list named" '"'$name'".'
		goto retry
	endif
	if( ! -r $file ) then
		echo "*** Mailing list can not be read."
	else
		if( -z  $file ) then
			echo "*** This list is currently empty."
		else
			$PAGER $file
		endif
	endif
else if( $1 == "edit" ) then
	if( ! -e $file ) then
		echo "*** There is no mailing list named" '"'$name'".'
		goto retry
	endif
	set y = `ls -l $file`
	if( $y[3] != $USER ) then
		echo "*** You don't own" '"'$name'"', it is owned by $y[3].
		goto retry;
	endif
#		edits what "file" is set to
	set embedded
	source $MENU_LIB/edit_file
else
	echo "*** error - unrecognizable parameter"
endif

done:
if( -e $awkf ) /bin/rm $awkf
