#!/bin/ksh
#
# $Revision: 1.2 $ $Date: 2012-07-26 17:01:23-04 $
# $Source: /home/vogelke/bin/RCS/inum2mbox,v $
# $Host: sys7.com $
# $UUID: da5b68bb-7251-378d-8951-907128bb7a77 $
#
#<inum2mbox: reads numbered email msgs (usually inodes), writes to mbox
#
# When running on lots of directories:
#   find . -maxdepth 1 -type d -printf "touch -d '%t' %p\n" > TIMES
#
#   Use a small driver:
#     #!/bin/ksh
#     list='0101 0102 0103 0104 ...'
#     for dir in $list; do
#         ( cd $dir; pwd; inum2mbox; touch -d yesterday mailfetch; )
#     done
#     sh TIMES
#     exit 0

export PATH=/home/vogelke/bin:/usr/local/bin:/usr/sfw/bin:/opt/sfw/bin:/bin
umask 022

keep='-XCc: -XDate: -XFrom: -XIn-reply-to: -XList-Archive:
 -XList-Help: -XList-Id: -XList-Post: -XList-Subscribe:
 -XList-Unsubscribe: -XMessage-Id: -XPrecedence: -XReferences:
 -XReply-To: -XSubject: -XTo: -XX-Mailman-Version:'

# may or may not want to keep these...
mime='-XMIME-Version: -XContent-Type: -XContent-Transfer-Encoding:'

mbox=/tmp/mailfetch$$

case "$#" in
    0) list=$(ls | grep '^[0-9][0-9]*$') ;;
    *) list="$*" ;;
esac

for msg in $list
do
    qp $msg |
      cm |
      formail -f -IReturn-Path -ISender |
      formail -k $keep $mime |
      formail -ds >> $mbox
done

mv $mbox mailfetch
test -s mailfetch && rm $list
exit 0
