#! /bin/csh -f
# Last edited on 2008-02-04 16:50:19 by stolfi

set usage = "$0 VMFOLDER MHFOLDER"

# Converts file ~/vm-folders-to-convert-to-mh/VMFOLDER 
# to MH format in directory ~/Mail/MHFOLDER

set path = ( /usr/bin /usr/ucb /usr/bsd /bin )

set vmfolder = "$1"; shift
set mhfolder = "$1"; shift

if ( $#argv != 0 ) then
  echo "usage: ${usage}"; exit 1
endif

set vmfile = "${HOME}/vm-folders-to-convert-to-mh/${vmfolder}"
set vmsave = "${HOME}/vm-folders-converted-to-mh/${vmfolder}"
set mhdir = "${HOME}/Mail/${mhfolder}"
set vmtemp = "/tmp/$$.vm" 

if ( ! ( -d ${mhdir} ) ) then
  echo "${mhdir} does not exist"; exit 1
endif

if ( -r ${vmfile}.gz ) then
  gunzip ${vmfile}.gz
endif

if ( ! ( -r ${vmfile} ) ) then
  echo "${vmfile} does not exist"; exit 1
endif

if ( -r ${vmsave} ) then
  echo "${vmsave} already exists"; exit 1
endif

if ( -r ${vmsave}.gz ) then
  echo "${vmsave}.gz already exists"; exit 1
endif

cat ${vmfile} \
  | sed -f ${STOLFIHOME}/lib/fix-from-line-in-vm-fcc.sed \
  | nawk -f ${STOLFIHOME}/lib/fake-dates-in-mail-folder.nawk \
  > ${vmtemp}
diff ${vmfile} ${vmtemp} \
  | ${STOLFIHOME}/bin/prettify-diff-output
inc +${mhfolder} -file ${vmtemp}
gzip ${vmfile}
mv ${vmfile}.gz ${vmsave}.gz
/bin/rm ${vmtemp}
${STOLFIHOME}/bin/fix-missing-dates-in-mh-messages ${mhdir}
