#!/bin/sh -e
#
# Handle frozen messages
#
# $Cambridge: hermes/conf/exim/sbin/frozen,v 1.7 2010/11/15 13:23:20 fanf2 Exp $

PATH=/opt/exim/bin:$PATH

if [ "x$2" = "x" ]
then
	sender='[^<]>'
else
	sender=" <[^>]*$2[^>]*>"
fi

awk_frozen () {
	exim -bp | awk "/$sender [*][*][*] frozen [*][*][*]/ { $* }"
}

case "$1" in
list)
	awk_frozen 'print'
	exit 0
	;;
head)
	awk_frozen '
		print "echo Queue-ID: " $3
		print "echo Return-Path: \"" $4 "\""
		print "exim -Mvh " $3
		print "echo"
	' | sh | less
	exit 0
	;;
view)
	awk_frozen '
		print "echo Queue-ID: " $3
		print "echo Return-Path: \"" $4 "\""
		print "exim -Mvc " $3
		print "echo"
	' | sh | less
	exit 0
	;;
pass)
	awk_frozen '
		print "exim -M " $3
	' | sh &
	exit 0
	;;
drop)
	awk_frozen '
		print "exim -Mrm " $3
	' | sh
	exit 0
	;;
bounce)
	awk_frozen '
		print "exim -Mg " $3
	' | sh
	exit 0
	;;
forward)
	awk_frozen '
		print "exim -Mes " $3 " postmaster@hermes.cam.ac.uk"
		print "exim -Mg " $3
	' | sh
	exit 0
	;;
*)
	cat <<EOF
usage: frozen <command> [sender]
	Apply the given command to all frozen messages whose sender
	address matches the given pattern, or all frozen messages if
	the pattern is omitted. The available commands are:
list
	show the messages' IDs and senders
head
	examine the message envelopes and headers
view
	examine the contents of the messages
pass
	unfreeze and deliver the messages
drop
	delete the messages
bounce
	generate delivery failure reports
forward
	send delivery failure report to postmaster
EOF
	exit 1
	;;
esac

echo whut?
exit 1
