
# HPUX_ID: @(#)29.1   86/07/04
#          @(#)mailparts.sh	29.1      86/07/02
 # mailparts - mail sharfiles with "Part nn of mm" subject lines
#
# Usage:
#  mailparts [-V] [-s subj-title] [-I src-dir] [-N name] email-addr [-] ...
#
# Arguments:
# 	-	indicates a coverletter comes from stdin
#	-s	prefix "Part n of m" subject lines with <subj-title>
#	-I	look in src-dir for sharfiles (see -O in partition(1))
#	-N	look for sharfiles named <name>.* (see partition(1))
#	-V	verbose mode
#	email-addr	where to send the stuff

#----------------------------------------------------------------------
# Setup
#----------------------------------------------------------------------
tmp1=/tmp/mpart.A$$
tmp2=/tmp/mpart.B$$
DEBUG=0

cleanup="/bin/rm -f $tmp1 $tmp2; exit"
trap "$cleanup" 0 15 
trap "echo 'Aborted...some cleanup may be necessary' 1>&2; $cleanup" 1 2 
#----------------------------------------------------------------------
# Shell functions
#----------------------------------------------------------------------
usage() {
	echo "Usage:"
	echo "  mailparts [-V] [-s subj-title] [-I src-dir] \c"
	echo "[-N name] email-addr ... [-]"
	exit
}

abort_err() {
	# Goes to stderr
	echo "error: $1 - aborting" 1>&2
	exit
}

#----------------------------------------------------------------------
# Examine all arguments
#----------------------------------------------------------------------
sourcedir="."
verbose=0
read_stdin=0
sharf_name="sharf"
email_addr=""
subj_title=""

set -- `getopt "hHI:N:s:V-" $*`

while [ -n "$1" ]
do case $1 in
	-[hH])
		usage;;
	-)
		read_stdin=1
		shift;;
	-N)
		# user has sharfiles named a particular way
		shift
		sharf_name=$1
		shift;;
	-I)
		# user has sharfiles created in specific place
		shift;
		if [ ! -d $1 ]
		then
			abort_err "$1: not a directory"
		elif [ ! -r $1 ]
		then
			abort_err "$1: not a writable directory"
		fi
		sourcedir=$1
		shift;;
	-V)
		verbose=1
		shift;;

	-s)
		shift
		subj_title=$1
		shift;;

	--)	shift;;
	-*)
		usage;;
	*)
		# dir-name as an argument
		email_addr="$email_addr $1"
		shift;;
	esac
done

# check and make sure he gave an address
if [ -z "$email_addr" ]
then
	usage
fi

# D E B U G //////////////////////////
if [ $DEBUG -gt 0 ]
then
	echo "sourcedir=$sourcedir"
	echo "verbose=$verbose"
	echo "read_stdin=$read_stdin"
	echo "sharf_name=$sharf_name"
	echo "email_addr=|$email_addr|"
	echo "subj_title=|$subj_title|"
fi

#----------------------------------------------------------------------
#  Do the stuff
#----------------------------------------------------------------------

cd $sourcedir

# find out which mailer to use
if [ -z "$MAILER" ]
then
	# default
	MAILER=mailx
fi

# Find all the files to send and how many there are
find . -name "$sharf_name.*" -type f -print > $tmp1
total=`wc $tmp1 | awk '{print $1}'`
if [ $total -eq 0 ]
then
	abort_err "No files named $sourcedir/$sharf_name.* found"
fi

# Put together the subject-line
if [ -n "$subj_title" ]
then
	subj_prefix="$subj_title: Part"
	cover_title="$subj_title: Cover letter"
else
	subj_prefix="Part"
	cover_title="Cover letter"
fi

# Do we have a cover-letter to read?
if [ $read_stdin -gt 0 ]
then
	# Copy stdin to a file
	cat > $tmp2
	if [ $verbose -gt 0 ]
	then
		# Show the user the command we're using, and the cover letter
		echo "$MAILER -s \"$cover_title\" $email_addr << @EOF"
		cat $tmp2
		echo "@EOF"
		echo
	fi
	$MAILER -s "$cover_title" $email_addr
fi

# Now send each piece
count=1
for sharfile in `cat $tmp1`
do
	if [ $verbose -gt 0 ]
	then
		# Show the user the command we're using
		echo "$MAILER -s \"$subj_prefix $count of $total\"\c"
		echo "$email_addr < $sharfile"
	fi
	$MAILER -s "$subj_prefix $count of $total" $email_addr < $sharfile
	count=`expr $count + 1`
done
