
# UNISRC_ID: @(#)massmail.sh	27.1	85/03/10  
# Script to mass mail to a list of users with each letter tagged,
# OR to mail a list of files to one user with subject == filename.

# Usage:  massmail [-n] file users...
#	  massmail -u [-n] user files...
#
#	-n means to use nmail instead of mail


# Initialize:

	PATH=/bin:/usr/bin:/usr/contrib/bin
	mailer=mail			# default mailer.
	form=file			# default form of invocation.
	sleep=10			# seconds per letter.


# Check arguments:

	if [ "${1:-null}" = "-u" ]
	then
		form=user
		shift
	fi
		
	if [ "${1:-null}" = "-n" ]
	then
		mailer=nmail
		shift
	fi

	if [ $# -lt 2 ]
	then
		echo "usage:\t$0 [-n] file users..."	>&2
		echo	   "\t$0 -u [-n] user files..."	>&2
		exit 1
	fi

	arg="$1"
	shift


# Send file to many users:

	if [ $form = file ]
	then
		echo "Sending $arg to:"
		distrib="$*"

		while true
		do
			user="$1"
			shift

			echo $user
		{	echo "\nDistribution:\t$distrib\nThis copy to:\t$user\n"
			cat "$arg"
		} |	$mailer "$user"

			if [ $# = 0 ]
			then				# done.
				break
			else
				sleep $sleep		# don't clog uux.
			fi
		done


# Send many files to one user:

	else
		echo "Sending to $arg:"

		while true
		do
			file="$1"
			shift

			echo $file
		{	echo "Subject:  file \"$file\"\n"
			cat "$file"
		} |	$mailer "$arg"

			if [ $# = 0 ]
			then				# done.
				break
			else
				sleep $sleep		# don't clog uux.
			fi
		done
	fi
