#!/bin/sh
# -*-coding: latin-1-unix;-*-
#
# dree -- Visualize directory trees
#
# Copyright (C) 2001.
#
# $Compile: ./dree$
# $Maintained at: http://www.visualco.de$
# $Author: Andreas Spindler <info@andreasspindler.de>$
# $Writestamp: 2016-04-20 12:08:34$

###############################################################################
# Options
#
Usage() {
    cat <<EOF 1>&2
Usage: `basename $0` [-a|-n PATTERN|-N PATTERN|-d] [dirname]...
EOF
}

Help() {
    Usage
    cat <<EOF 1>&2

Options:
    -a          Print all names.  Default is to only print only directory names.
    -n PATTERN  Find only files matching pattern.
    -N PATTERN  Like -n, but negate pattern.
    -v          Verbose.
    -h          Help.

Prerequisites:
    find, sort, sed

The pattern is applied to the  whole pathname. The metacharacters '*', '?', and
'[]'  are available.  Braces and  the '.'  character are  not recognized  to be
special. For example  the pattern "*htm" finds only files  ending in "htm", not
ending in "html". For details see the fnmatch library function.

Maintained at <http://www.visualco.de>

EOF
}

optqad=1 optname='-path' optnamearg=* opttype='-type d'
while getopts 'adn:N:vhH' opt; do
    case $opt in
        a) opttype=;;
        n) optnamearg=$OPTARG;;
        N) optnamearg=$OPTARG; optname='! -path';;
        v) optverbose=1;;
        [hH]) Help; exit 0;;
        *) echo "Unknown option '$1'" 1>&2; Usage; exit 1;;
    esac
done
shift $(($OPTIND - 1)) # correct the index so the filename argument is always $1
dirnames=${*:-.}
findargs="$opttype $optname"
#echo "'$*'   findargs='$findargs'   dirnames='$dirnames'"; exit 1

###############################################################################
# Action
#
if [ $optqad ]; then
    # Run find, sort filenames, then use sed to replace
    #   "SUBDIR/"       => "+----"
    #   "+---- +----"   => "+ +----"
    #
    # The replacement trick is a common one to get a quick-and-dirty tree. It
    # operates only linewise. So the directories names are sorted and printed
    # just like any filename.

    for dir in $dirnames; do
        if [ $optverbose ]; then echo "Printing tree for directory '$dir':"; fi
        find $dir $findargs "$optnamearg" -print |  \
            sort -f |                               \
            sed -e 's:[^/]*/:|---- :g'              \
                -e 's:---- |:     |:g'
    done
else
    ls -lsAF --group-directories-first --color $dirnames
fi

# Local Variables:
# coding: iso-8859-1-unix
# fill-column: 80
# End: