#!/bin/sh
#
# Bscan -- display count of messages in "bulk" folders
#
# Normally invoked by xlbiff.  Not intended for general use.
#
# $Id: Bscan,v 1.5 2003/11/11 20:40:54 esm Exp $
#

# Width of the FOLDER (left hand) side of the box
WIDTH_L=13
# Width of the COUNT (right hand) side.  3 digits is plenty for me.
WIDTH_R=3

# Don't clobber existing context... that makes it impossible for us to
# read messages from the command line.
MHCONTEXT=$HOME/Mail/context.tmp.$$; export MHCONTEXT

# Last top-level dir
last_dir=''

# Find all folders with unseen messages
for i in `flist -all -recurse -sequence unseen -noshowzero -fast`; do
    # For a given folder, e.g. M/zaurus/oz...
    dir=`expr $i : "\([^/]\+\)"`		# Parent directory (M)
    folder=`expr $i : "[^/]\+/\(.*\)"`		# rest of it        (zaurus/oz)

    # Extract the number of unseen messages in this folder
    count=`flist +$i | sed -e 's/^.* \([0-9]\+\) in seq.*$/\1/'`

    # Same parent directory as last time?  Omit dir, but indent by same amount
    #
    # Otherwise, display the
    if [ "$last_dir" = "$dir" ]; then		# Same parent as last time
	printf "%*s " ${#dir} " "
    else if [ "$folder" = "" ]; then		# Diff, but top-level folder
	printf "%s " $dir
    else					# Diff, 2-or-more-level folder
	printf "%s/" $dir
    fi
    fi

    # Show the rest of the folder name, right-padding for alignment.  Show
    # the unseen message count.
    fmt=$(expr $WIDTH_L - ${#dir} - 1)
    printf "%-*s%*d\n" "$fmt" "$folder" $WIDTH_R "$count"

    # Remember this dir for next time
    last_dir=$dir
done

rm -f $MHCONTEXT
