#!/bin/bash
# set -x
  set -e

export TZ=UTC
export LANG=C
export LC_ALL=C
export LC_COLLATE=POSIX

msgid_append_string=$$.$LOGNAME@`hostname -f`
current_in_reply_to=
deep_thread=0
mail_tmp_dir=`mktemp -d /tmp/tmp.XXXXXXXXXX`
counter=0
maxfiles=0
message_file=
series_file=
patch_directory=
d=

usage () {
cat <<EOU
usage: ${0##*/} -d message.txt -s seriesfile
 send a single email per patch file listed in the quilt seriesfile
 the first mail contains the contents of message.txt,
 every patch is a followup to this mail

 each patch must contain:
Subject: some description
To: <recipent@host.name>, <lkml@kernel.name>
Cc: <otherrecipent@otherhost.name>, <yetanotherrecipent@yetanotherhost.name>

description

patch

EOU
}
#
sleep_a_second() {
	while test "$1" = "`date +%s`"
	do
		sleep 0.1
	done
}
send_mail() {
	local current_subject
	local current_to_recipents
	local current_cc_recipents
	local current_mail_dir
	local current_message_id
	local patch_file
	local patch_number
	local mutt_myhdr

	patch_file=$1
	patch_number=$2
	current_mail_dir=`mktemp -d $mail_tmp_dir/$(date +%Y%m%d%H%M%S).${counter}.XXXXXXXXXX`
	current_message_id="${current_mail_dir##*/}.$msgid_append_string"
	sed -n  -e "
	/^Subject:/ {
	s@^Subject:@@
	w $current_mail_dir/subject.txt
	d
	}
	/^To:/ {
	s@^To:@@
	w $current_mail_dir/to.txt
	d
	}
	/^Cc:/ {
	s@^Cc:@@
	w $current_mail_dir/cc.txt
	d
	}
	p
	" < $patch_file > $current_mail_dir/mailbody.txt
	if test -s $current_mail_dir/subject.txt ; then
		read current_subject < $current_mail_dir/subject.txt
	fi
	if test -s $current_mail_dir/to.txt ; then
		read current_to_recipents < $current_mail_dir/to.txt
	fi
	if test -s $current_mail_dir/cc.txt ; then
		read current_cc_recipents < $current_mail_dir/cc.txt
	fi
	if test "$current_subject" = "" ; then
		echo "$patch_file has no Subject, using filename"
		current_subject=$patch_file
	fi
	mutt_myhdr="my_hdr Message-Id: <$current_message_id>"
	if test "$current_in_reply_to" != "" ; then
		mutt_myhdr="$mutt_myhdr ; my_hdr In-Reply-To: <$current_in_reply_to>  "
	fi
	echo "$counter/$maxfiles $current_subject"
	mutt \
		-e "$mutt_myhdr" \
		-s "[PATCH $counter/$maxfiles] $current_subject" \
		-b olh@suse.de \
		-c "${current_cc_recipents#Cc:*}" \
		"${current_to_recipents#To:*}" < $current_mail_dir/mailbody.txt
	if test "$current_in_reply_to" = "" -o "$deep_thread" = "1" ; then
		current_in_reply_to=$current_message_id
	fi

}
#
until [ "$#" = "0" ] ; do
	case "$1" in
		-d|--description)
			message_file=$2
			shift 2
		;;
		-s|--series)
			series_file=$2
			shift 2
		;;
		-h|--help|-v|--version)
		usage
		exit 0
		;;
		*)
		usage
		exit 1
		;;
	esac
done
if test "$message_file" = "" -o "$series_file" = "" ; then
	usage
	exit 1
fi
#
if ! test -f "$message_file" ; then
	usage
	exit 1
fi
if ! test -f "$series_file" ; then
	usage
	exit 1
fi
case "$series_file" in
	*/*)
	patch_directory=${series_file%/*}
	patch_directory=`cd $patch_directory ; pwd -P`
	;;
	*)
	patch_directory=./
	;;
esac

while read line
do
	case "$line" in
		\#*) continue ;;
	esac
	if ! test -f "$patch_directory/$line"
	then
		echo "a patch named '$line' does not exist in $patch_directory" 
		exit 1
	fi
	echo "$line" >> $mail_tmp_dir/series
	: $(( maxfiles++ ))

done < $series_file
#
#
#
date
send_mail $message_file 0

sleep_a_second `date +%s`
while read patch
do
	: $(( counter++ ))
	send_mail $patch_directory/$patch $counter
	sleep_a_second `date +%s`
done < $mail_tmp_dir/series

date
