#! /bin/sh
#
#	Usage: promail sitename [ truncname ]
#
#	This script is part of the upcp (Unix-to-ProLine Copy) system.
#	Its job is to move mail from the local uucp spool directory
#	for the ProLine site and put it into the coresponding upcp
#	spool directory.  This script is called from the uucp account
#	so that it has permission to move the files.
#
#	The optional truncname argument is required for some systems
#	that truncate site names to 8 characters.  For these systems
#	give the full site name as the first argument, and the truncated
#	name as the second.

site=$1				# actual (full) site name (e.g. pro-apple2)
uusite=$2			# UUCP directory name (e.g. pro-appl)
owner=upcp			# owner of upcp files

if test "$uusite" = ""
then
	uusite=$site
fi

upspool=/usr/spool/upcp/$site		# site's upcp spool directory
uuspool=/usr/spool/uucp/$uusite		# site's UUCP spool directory

# Change to the site's UUCP spool directory and start a loop for
# each control file.  Process the data file by adding a Ppath: field
# based on the rmail target.  Finally, set modes and ownership on
# the new UPCP destination file, then delete the UUCP trio.

cd $uuspool
for i in C.*
do
	if test "$i" = "C.*"
	then
		exit 0
	fi

	dfile=`sed -e 2d -e 's/^.[ ][ ]*\(D.[^ ]*\).*/\1/' $i`
	xfile=`sed -e 1d -e 's/^.[ ][ ]*\(D.[^ ]*\).*/\1/' $i`
	ppath=`sed -n '/^C rmail/s/^C rmail \([^ ]*\).*/\1/p' $xfile`
	msg=$upspool/$dfile

	( sed -e '/^Ppath:/d' \
		-e '/^$/q' \
		-e "/^Apparently-To:/i\\
Ppath: $site!$ppath" \
		-e "/^To:/i\\
Ppath: $site!$ppath" $dfile ;
		sed '1,/^$/d' $dfile ) > $msg

	chmod go-rw $msg
	chown $owner $msg
	rm -f $i $dfile $xfile
done
