#! /bin/sh
# tear prefix [file...] - tear RFC822 header and body apart
#       output files are $1hdr and $1body
PATH=/bin:/usr/bin; export PATH

case $# in
0)
        echo "usage: tear prefix [file...]" >&2
        exit 1
        ;;
esac

hdr="$1hdr"
body="$1body"
shift

>>$hdr                                  # create files just in case
>>$body
case $# in
0)      args="-" ;;     # awk needs a filename due to cmd. line assignments
*)      args="$@" ;;
esac
awkprog="BEGIN {hdr=\"$hdr\"; body=\"$body\"; inbody = 0}
        (inbody == 0) && (\$0 ~ /^[^ \\t].*:/ || (\$0 ~ /^[ \\t]/ && NR > 1)) { print \$0 >hdr; next }
                                        { inbody = 1; print \$0 >body }"
exec awk "$awkprog" $args
