#! /bin/sh

VERSION=0.11

help=n
version=n
format=""
while getopts hVF: opt ; do
    case "$opt" in
        h) help=y ;;
        V) version=y ;;
        F) format="$OPTARG" ;;
    esac
done
shift $(($OPTIND - 1))

if [ "$version" = "y" ]; then
    echo "lsmaildir (part of lsmail tools) $VERSION"
    echo "Copyright (C) 2011 Olivier Schwander <olivier.schwander@chadok.info>"
    echo "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>."
    echo "This is free software: you are free to change and redistribute it."
    echo "There is NO WARRANTY, to the extent permitted by law."
    exit 0
fi

if [ "$help" = "y" ]; then
    echo "Usage:"
    echo "    lsmaildir [options] [path]"
    echo ""
    echo "Options:"
    echo "    -h  Display this help text"
    echo ""
    echo "    -V  Display the version number"
    echo ""
    echo "    -F  Display results using the given format (%s will be replaced by the"
    echo "        name of the mailboxe)"
fi

BASE=$1
if [ -z "$BASE" ]; then
    BASE=.
fi

for cur in $(find $BASE -type d -name cur); do
    mb=$(dirname $cur)
    if [ -d $mb/cur -a -d $mb/new -a -d $mb/tmp ]; then
        mb=$(echo $mb | sed s!$BASE/!!)
	     if [ -n "$format" ]; then
				printf -- "$format" $mb
	     else
				echo $mb
	     fi
    fi
done

