#!/bin/csh -f

#
# send multiple telegrams
#
# features:
# 	predefined recipiant list aliases (telgroups)
#	automatic inclusion on a salutation (salutations)
#	automatic addessing for countries based on declaration
#	automatic breakage of long messages into smaller ones.
#

set nation = nation		# must contain "nation" data
set relations = telgroups	# who are your friends?
set salu = salutations		# how do you address them?
set empire = et			# program that gets you into empire

switch ($#argv) 
case 0:
	echo -n "to? "
	set to = ($<)
case 1:
	if (! $?to) then
		set to = $argv[1]
	endif
	set message = /tmp/foo$$
	touch /tmp/foo$$
	chmod 600 /tmp/foo$$
	vi /tmp/foo$$
case 2:
	if (! $?to) then
		set to = $argv[1]
	endif
	if (! $?message) then
		set message = ($argv[2])
	endif
	echo -n "verify? "
	set verify = ($<)
	breaksw
case 3:
	set to = $argv[1]
	set message = ($argv[2])
	set verify = ($argv[3])
	breaksw
default:
	echo "mtel: [to [message [y/n]]]"
	exit 1
endsw

set recip = `echo TO $to  | awk -f mtel.awk $nation $relations -  `
echo "To: "$recip
set CC = `echo CC $to  | awk -f mtel.awk $nation $relations -  `
echo "Cc: "$CC

echo "" > /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
echo "" >> /tmp/ogm$$
chmod 600 /tmp/ogm$$

foreach i ($recip)
	echo tele $i >> /tmp/ogm$$
	touch > /tmp/tgm$$
	chmod 600 /tmp/tgm$$
	egrep "^$i	" $salu | sed 's/^[0-9]*	//' >>/tmp/tgm$$
	cat $message >> /tmp/tgm$$
	if ($#CC > 1) echo "Cc: $CC" >> /tmp/tgm$$
	echo "% 450 % . % tele @ $i % " | \
		awk -f split.awk - /tmp/tgm$$ >>/tmp/ogm$$
	echo . >> /tmp/ogm$$
end
	
if ($verify == "y") then
	more /tmp/ogm$$
	echo ""
	echo -n "use? "
	set ans = ($<)
	if ($ans != "y") then
		mv -f /tmp/ogm$$ dead.telegram
		exit
	endif
endif

$empire < /tmp/ogm$$

mv -f /tmp/ogm$$ dead.telegram

/bin/rm -f /tmp/ogm$$ /tmp/msg$$ /tmp/foo$$
