#!/bin/sh
#
#  Helen Wong
#  July 14, 1991
#
#  Modified by: dyker Fri Mar 20 13:13:48 MST 1992
#	cleaned-up/took-out lots of superfluous stuff
#	fixed setting of folder
#	fixed the ed stuff - (some machines don't deal with /)
#	made it so it deals with only the first 25 lines
#	consolidated mhpath commands so it's only executed once
#	added recognition of last, first, next, prev, and cur as valid msgs
#
#  This program inserts an Assigned-To component and login names in a mh
#  message.

PATH=/usr/local/mh/bin:/bin:/usr/bin:/usr/ucb
component="Assigned-To:"
form="-form /homes/troubmh/Mail/pscan.form"
usage="usage: $0 [msg] [+folder] name-list
	msg: one msg
	name-list: login name(s)"

if [ $# -eq 0 ]
then
	echo  "$usage"
	exit 1
fi

while [ $# -ne 0 ]
do
	case $1 in
	  
	  +*)	if [ -n "$folder"  ]
		then
			echo "folder: one folder"
			exit 1
		else
			folder=$1
			mhpath $folder
			if [ $? -ne 0  ]
			then
				echo  "$folder does not exist"
				exit 1
			fi
		fi;;
	
	[0-9]*|last|first|cur|next|prev)
		if [ -n "$msg"  ]
		then
			echo "$usage"
			exit 1
		else
			msg=$1
		fi;;

	[A-z]*)	name_list="$name_list $1";;

		
	     *)	echo  "$usage"
		exit 1;;

	esac
	shift

done

folder="${folder-+${FOLDER-`folder -fast`}}"

msg=${msg-cur}
msgpath=`mhpath $folder $msg 2>/dev/null`
if [ "$msgpath" = "" ] 
then 
	echo  "Current message does not exist"
	exit 1
fi

if [ -z "$name_list"  ]
then 
	echo	"$usage"
	exit 1
fi


annotate="anno $folder $msg -nodate -inplace -component $component -text "$name_list""

head -25 $msgpath | grep -s "^$component"
if [ $? -eq 0  ] 	
then
	ed -s $msgpath <<END
	1,25s/^$component.*/$component $name_list/
	w
	q
END
else 
	$annotate
fi
 	
scan $folder $msg $form
