#!/bin/csh -f
#
# This script takes a list of regular mail files (such as those
# from mh) and makes an rmail file out of them.
#
# Usage: files_to_rmail rmailfile file [file [file [....]]]
#

if ($#argv < 2) then
   echo "Usage: files_to_rmail rmailfile file [file [file [....]]]"
   exit 1
endif

set rmail = $1
shift
set files = ($argv)

if (-e $rmail) then
   echo "$rmail exists; renaming to $rmail.$$."
   mv $rmail $rmail.$$
   if ($status) then
       echo "An error ($status) occurred; exiting."
       exit 1
   endif
   cat << EOF > $rmail
BABYL OPTIONS:
Version: 5
Labels:
Note:   This is the header of an rmail file.
Note:   If you are seeing it in rmail,
Note:    it means the file has no messages in it.
EOF
   echo -n "" >> $rmail
endif

foreach file ($files)
   echo ${file}
   awk '{if (NF == 0) exit; else print $0;}' ${file} > ${file}.long
   grep -v '^Received:' < ${file}.long | grep -v '^Message-Id:'\
	> ${file}.short
   echo "" >> $rmail
   echo "1,," >> $rmail
   cat ${file}.long >> $rmail
   echo "" >> $rmail
   echo "*** EOOH ***" >> $rmail
   cat ${file}.short >> $rmail
   set length = (`wc -l ${file}`)
   set hlength = (`wc -l ${file}.long`)
   set length = $length[1]
   @ length -= $hlength[1]
   tail -$length ${file} >> $rmail
   echo -n "" >> $rmail
end

exit 0
