#!/bin/csh -f
# An improved version to follow...

unset noclobber noglob

set tmp = "$0"
set tmp = "$tmp:t.$$"

if ($#argv < 1 || $#argv > 2) then
	echo "usage: $0 message [Mailing_list_file]"
	exit 1
endif

if (! -e "$argv[1]") then
	echo "$argv[1] doesn't exist..."
	exit 1
endif

set foo=(`wc -c "$argv[1]"`)
if ($foo[1] > 512) then
	echo "$argv[1] too long (> 512 characters)..."
	exit 2
endif

if (! $?country) then
	if ($?COUNTRY) then
		set country="$COUNTRY"
	else
		echo -n "Country name? "
		set country="$<"
	endif
endif

onintr end
stty -echo
echo -n "Your name? "
set password="$<"
stty echo
echo " "

if ($#argv > 1) then
	if (! -e "$argv[2]") then
		echo "Mailing list file $argv[2] does not exist..."
		exit 3
	endif
	awk '{print "echo \"tel", $0 "\" | cat - '$argv[1]' >> '$tmp'; echo \".\" >> '$tmp'"}' $argv[2] | csh -f
	goto send
endif

echo -n "Selective mailing? [n] "
set send="$<"
if ("$send" !~ [Yy]*) unset send

@ i = 1
while ($i < 32)

	if ($?send) then
		echo -n "Send message to $i? [y] "
		if ("$<" !~ [nN]*) then
			echo "tel $i" >> $tmp
			cat "$argv[1]" >> $tmp
			echo "." >> $tmp
		endif
	else
		echo "tel $i" >> $tmp
		cat "$argv[1]" >> $tmp
		echo "." >> $tmp
	endif

@ i++
end

send:
empire $country $password < $tmp > /dev/null

rm $tmp

exit 0

end:
stty echo
rm -f $tmp >& /dev/null
echo "Aborting..."
