#!/bin/csh -f
set dest = `grep path $HOME/.getmail/getmailrc | awk '{print $3}'`
set getmailpath = $HOME/bin/getmail/bin
#create destination directory (hardcoded in getmailrc)
if (! -e $dest) then
  mkdir $dest
endif
#create directories maildir needs
if (! -e $dest/cur) then
  mkdir $dest/cur
endif
if (! -e $dest/new) then
  mkdir $dest/new
endif
if (! -e $dest/tmp) then
  mkdir $dest/tmp
endif
$getmailpath/getmail $*
#start numbering messages at one after any existing messages in dest dir
set f = `ls $dest | awk '$1~ "^[0-9]+$" {print $1 + 1}' | sort -n | tail -1`
if ($f == '') then
  set f = 1;
endif
#loop through messages and rename them, if we have any
set i = $f;
set n = `ls -a $dest/new | wc | awk '{print $1}'`
if ($n != 2) then
  foreach j (`ls -rt $dest/new/`)
    mv $dest/new/$j $dest/$i
    set i = `expr $i + 1`
  end
endif
#get rid of maildir directories
if (-e $dest/cur) then
  rm -rf $dest/cur
endif
if (-e $dest/new) then
  rm -rf $dest/new
endif
if (-e $dest/tmp) then
  rm -rf $dest/tmp
endif
#scan messages like inc did!
if ($n != 2) then
  scan +$dest $f-last
endif
