#!/bin/sh -e
# Prints address book for emacs-easypim utility
# # Michael Pozhidaev <msp@altlinux.org>
# Date: 2011-03-20;

THIS="${0##*/}"

if [ "$1" == '--help' ]; then
    cat <<EOF
$THIS: prints address book for emacs-easypim utility.
For proper work of this tool environment variable \$EASYPIMDIR must have non empty value.
EOF
    exit 0
fi

if [ -z "$EASYPIMDIR" ]; then
    echo "$THIS:error:directory EASYPIMDIR is not set" >&2
    exit 1
fi

exec 2> /dev/null

EASYPIMDIR="$(dirname "$EASYPIMDIR")/$(basename "$EASYPIMDIR")"

find "$EASYPIMDIR/." -type d '!' -iname '.*' | sort |
while read DIR; do
    echo
    echo '--------------------------------------------------'
    basename "$DIR"
    echo
    find "$DIR" -type f | sort |
    while read l; do
	if [ "$(cat "$l" | wc -l)" -ge 2 ]; then
	    echo "$(basename "$l")+"
	else
	    echo "$(basename "$l"): $(cat "$l")"
	fi
    done 
done
