#!/bin/sh
#
#  Helen Wong
#  July 14,1991
#
#
#  Modified by: juola Friday May 8
#	Updated priority descriptions
#
#  Modified by: dyker Friday 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 a Priority component and a priority in a mh message.

PATH=/usr/local/mh/bin:/bin:/usr/bin:/usr/ucb
component="Priority:"
form="-form /homes/troubmh/Mail/pscan.form"
usage="usage: $0 [msg] [+folder] priority
	msg: one msg
	priority: [A-z][0-9]
	Priorities:  
	   Type				   Effect
	A. Time Critical 		1. Accelerated
	B. Broken no workaround		2. Multiple Users
	C. Installs 			3. Single faculty/staff
	D. Broken with workaround	4. Single student/other"

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 >/dev/null 2 >&1
			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][0-9])	priority=""
		if [ -n  "$priority" ]
		then
			echo "$usage"
			exit 1
		else
			priority=`echo $1 | tr a-z A-Z`
		fi;;
        -help)  echo  "$usage"
		exit 0;;
	    
	    *)	echo  "$usage"
		exit 1;;

	esac
	shift

done

# set defaults
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 "$priority"  ]
then 
	echo	"$usage"
	exit 1
fi

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

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