:
##########################################################################
# Title      :	mailex - extract file from mail
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1995-01-23
# Requires   :	who
# Category   :	Mail
# SCCS-Id.   :	@(#) mailex	1.3 04/01/06
##########################################################################
# Description
# Bugs
#	mailx does not recognize duplicate mails
##########################################################################

PN=`basename "$0"`			# program name
VER='1.3'

AWK=awk
[ -x /bin/nawk ] && AWK=/bin/nawk

: ${USER:=`who am i | cut -d' ' -f1`}
: ${MailBox:=${MAIL:-/var/mail/$USER}}

usage () {
    echo >&2 "$PN - extract ftpmail files from mail, $VER (stv '94)
usage: $PN [-s] [-f mail_box] ftp_filename new_filename
    -l:    list ftpmail files in mailbox
    -s:    be silent
    -f:    name of input mail file (default is $MailBox)

If no parameter is given all ftpmail-files in the mailbox
are listed (like parameter -l)."
    exit 1
}

err () {
    for i
    do echo "$PN: $i" >&2
    done
}

fatal () { err "$@"; exit 1; }
msg ()   { if [ "$silent" != y ]; then err "$@"; fi; }

# ListFiles (mailbox, ...)
ListFiles () {
    $AWK '
	($0 ~ /^From: /) {
	    while ( getline && $0 !~ /^From / )
	    {
		if ( $0 ~ /^Subject:.*\[[0-9][0-9]* of [0-9][0-9]*\]/ )
		{
		    # Subject: [001 of 035] murx.firma.de:/pub/dict/german (get german)
		    n = split ($0, line)
		    nr    = substr (line [2], 2) + 0
		    total = substr (line [4], 1, length (line [4])-1) + 0
		    if ( split (line [5], tmp, ":") == 2 )
		    {
			host  = tmp [1]
			path = tmp [2]
			n = split (path, tmp, "/")
			file = tmp [n]
		    }
		    else exit
		    Files [path]  = total
		    FCount [path]++
		    break
		}
	    }
	}
	END {
	    for ( i in Files )
		print i, Files [i], FCount [i]
	}
    ' "$@"
}

ExtractFile () {
    : ${1?} ${2?}
    $AWK '
	BEGIN {
	    Base = "'$Tmp'"
	}
	($0 ~ /^From: /) {
	    while ( getline && $0 !~ /^From / )
	    {
		if ( $0 ~ /^Subject: \[[0-9][0-9]* of [0-9][0-9]*\]/ )
		{
		    # Subject: [001 of 035] murx.firma.de:/pub/dict/german (get german)
		    n = split ($0, line)
		    nr    = substr (line [2], 2) + 0	# part number
		    ltotal = substr (line [4], 1, length (line [4])-1) + 0
		    if ( split (line [5], tmp, ":") == 2 )
		    {
			host  = tmp [1]; path = tmp [2]
			n = split (path, tmp, "/")
			file = tmp [n]
			if ( path != "'"$2"'" )
			{
			    file="";
			    break
			}
		    }
		    else break

		    fname = Base nr ""
		    nfiles++
		    total = ltotal
		    if ( "'$silent'" != "y" )
			printf "processing ftp mail (%3d/%3d, file %s)\n",
			    nr, total, path | "cat >&2"
		    while ( getline && $0 !~ /^ftpmail-begin/ )
			;
		    while ( getline && $0 !~ /^ftpmail-end/ )
			print >> fname
		    close fname
		}
	    }
	}
	END { printf "%d\t%d\t\"%s\"\n", nfiles, total, file }
    ' "$1"
}

Tmp=${TMPDIR:=/tmp}/ms$$.
trap "rm -f ${Tmp}* > /dev/null 2>&1" 0
trap "exit 2" 1 2 3 15

silent=n
list=n
File=
NewFile=
while [ $# -gt 0 ]
do
    case "$1" in
	-f)	MailBox="$2"; shift;;
	-s)	silent=y;;
	-h)	usage;;
	-l)	list=y;;
	*)
		if [ -z "$File" ]
		then File="$1"
		elif [ -z "$NewFile" ]
		then NewFile="$1"
		else usage
		fi;;
    esac
    shift
done

[ -n "$MailBox" -a -r "$MailBox" ] || fatal "cannot open $MailBox"

if [ -z "$File" ]
then					# list files in mailbox
    ListFiles $MailBox
    exit 0
fi

[ "$list" = y ] && ListFiles $MailBox

eval set -- `ExtractFile "$MailBox" "$File"`

n="${1:-0}"
[ $n -lt 1 ] && fatal "File $File not found in mail file $MailBox"

total="${2:-0}"

msg "processing $total files"

Out="${Tmp}all"
> $Out
Nr=0
missing=0
while [ $Nr -lt "$total" ]
do
    Nr=`expr $Nr + 1`
    In="$Tmp$Nr"    
    [ -r "$In" ] || {
	msg "WARNING: part $Nr of file $File is missing - ignored"
	missing=`expr $missing + 1`
	continue
    }
    cat "$In" >> $Out && rm -f $In > /dev/null 2>&1
done

# uudecode will extract file with this name:
Name=`$AWK '/^begin / { print $3; exit 0; }' < $Out `
uudecode $Out
if [ -n "$NewFile" -a "$NewFile" != "$Name" ]
then
    mv $Name $NewFile
else
    NewFile="$Name"
    : ${NewFile:=$PN.out}
fi

[ "$missing" -gt 0 ] &&
    err "file may be corrupted - $missing out of $total parts are missing"
msg "file \"$File\" is written to \"$NewFile\""
exit 0
