#!/bin/sh

VERSION='$Id: incdir,v 1.2 2001/07/25 04:29:29 kazu Exp $'
IDIR=.
MDIR=${HOME}/Maildir
MOVE="YES"

args=`getopt bd:i: $*`
if [ $? != 0 ]; then
    echo "usage: incdir [-b] [-d maildir] [-i inboxdir]"
    echo "    -b            backup mail to maildir/cur directory"
    echo "    -d maildir    path to maildir  (default: ${HOME}/Maildir)"
    echo "    -i inboxdir   path to inboxdir  (default: .)"
    echo
    echo "version: $VERSION"
    exit 1
fi
set -- $args
for i
do
    case "$i" in
    -b)
	MOVE="NO"; shift
	;;
    -d)
	MDIR="$2"; shift; shift
	;;
    -i)
	IDIR="$2"; shift; shift
	;;
    --)
	shift; break
	;;
    esac
done

if [ \( ! -d $MDIR/new \) -o \( $MOVE = "NO" -a ! -d $MDIR/cur \) ]; then
    echo "can't find $MDIR."; exit 1
fi
if [ ! -d $IDIR ]; then
    echo "can't find $IDIR."; exit 1
fi

SEQ=`ls -f1 $IDIR | egrep '^[1-9][0-9]*$' | sort -n | tail -1`
if [ "$SEQ"X = ""X ]; then
    SEQ=1
else
    SEQ=`expr $SEQ + 1`
fi

for f in $MDIR/new/*; do
    # sanity check
    [ ! -f $f ] && continue
    while [ -f $IDIR/$SEQ ]; do
	SEQ=`expr $SEQ + 1`
    done

    if [ "$MOVE"X = "YES"X ]; then
	mv $f $IDIR/$SEQ
    else
	cp -p $f $IDIR/$SEQ
	mv $f $MDIR/cur/`basename $f`:2,S
    fi
    echo $SEQ
    SEQ=`expr $SEQ + 1`
done

exit 0

# incdir - incorporate new mail from qmail user's maildir
#     author: Yasunari Momoi <momo@bug.org>
#     created: 2000/10/13
#
#
# Add the following to your emacs configuration file.
#
# (setq mew-mailbox-type 'mbox)   ; this applies also maildir
# (setq mew-mbox-command "incdir")
# (setq mew-mbox-command-arg "-d /path/to/your/maildir")
#
#
# Copyright (C) 2000 Yasunari Momoi.  All rights reserved.
# Copyright notice is the same as Mew's one.
