#!/bin/sh
#         @(#) tree      2.0 04/08/00
#                                         original version by Jordi Sanfeliu
#                                         email: mikaku@arrakis.es
#
#         Initial version:  1.0  30/11/95
#         Next version   :  1.1  24/02/97   Now, with symbolic links
#         Patch by       :  Ian Kjos, to support unsearchable dirs
#                           email: beth13@mail.utexas.edu
#         File support   :  by Matthias Arndt
#                           email: matthiasarndt@gmx.net
#
#         script published by Linux Gazette (http://www.linuxgazette.com)
#
#         Tree is a tool for viewing the directory tree (obvious :-) )
#
search () {
        for dir in `echo *`
        do
                if [ -d $dir ] ; then
                        zz=0
                        while [ $zz != $deep ]
                        do
                                echo -n "|   "
                                zz=`expr $zz + 1`
                        done
                        if test -L $dir ; then
                                echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
                        else
                                echo "+---$dir/"
                                if cd $dir ; then
                                        deep=`expr $deep + 1`
                                        search    # with recursivity ;-)
                                        numdirs=`expr $numdirs + 1`
                                fi
                        fi
                else
# support to display the files here...
                        if [ -f $dir ] ; then
                                zz=0
                                while [ $zz != $deep ]
                                do
                                        echo -n "|   "
                                        zz=`expr $zz + 1`
                                done
                                echo "+---$dir"
                        fi
                fi
        done
        cd ..
        if [ $deep ] ; then
                swfi=1
        fi
        deep=`expr $deep - 1`
}

# - Main -
# save the current directory to the stack
# the old version would put you inside the dir you gave
#on the command line
pushd $PWD >/dev/null
if [ $# = 0 ] ; then
        cd `pwd`
else
        if test $1 = "-?"
        then
                echo "Usage: $0 [optional starting dir]"
                exit 1
        fi
        cd $1
fi
echo "Initial directory = `pwd`"
swfi=0
deep=0
numdirs=0
zz=0

while [ $swfi != 1 ]
do
        search
done
echo "Total directories = $numdirs"
# restore the directory
popd >/dev/null
