:
#       UTREE.MKLIST.SH
#       UTREE create tree directory list for quicker startup
#       3.00-um klin, Sat Apr 20 11:27:17 1991
#       Usage:          utree.mklist [-a]
#       Options:        -a      Read in all (including hidden) directories
#       Directory:      /usr/local/bin
#
#       Copyright (c) 1991 by Peter Klingebiel & UNIX Magazin Muenchen.
#       For copying and distribution information see the file COPYRIGHT.
#
#       SCCSID=@(#) utree.mklist.sh (utree 3.00-um) Apr 20 1991
#
#
#       Introduction of sort fields by Rolf Gebhardt
#
#       avoids funny sort orders, e.g.
#
#           /u/user/utree/utree.um
#           /u/user/utree/utree.um.1
#           /u/user/utree/utree.um.1/bin
#           /u/user/utree/utree.um.1/doc
#           /u/user/utree/utree.um/bin
#           /u/user/utree/utree.um/doc
#
#       the right sort order, which represents the directory-tree, is
#
#           /u/user/utree/utree.um
#           /u/user/utree/utree.um/bin
#           /u/user/utree/utree.um/doc
#           /u/user/utree/utree.um.1
#           /u/user/utree/utree.um.1/bin
#           /u/user/utree/utree.um.1/doc
#
#
#
LIST=$HOME/.utreelist           # List file
FIELDS=""                       # Sort Field List generated below
                                # dependent on number of directory levels

if   test $# -eq 0              # Check option
then
  AFLAG=
elif test $# -eq 1 -a "$1" = "-a"
then
  AFLAG=-a
else
  echo "Usage: utree.mklist [-a]"
  exit 1
fi

         # count the number of directory levels
         # (to do it with 'awk' would be more elegant, but the method
         #  used here is faster than 'awk')
         #
         # e.g. for '/u/user/wrk/bin/xxx' we get 5 slashes plus one \n,
         # so 'wc' returns 6
         # the number of fields to sort is 5
         # sort field 0 is always empty

nofields=`find $HOME -type d -print | tr -cd "\012/" |\
          sort -r | head -1 | wc -c | tr -d "\040\011"`
nofields=`expr $nofields - 1`

         #  (debug statement)
         #  echo "nofields = $nofields"
         #
         #  generate the sort-fields parameter for 'sort'
         #

while [ $nofields -gt 1 ]
do
  FIELDS="-$nofields +$nofields $FIELDS"
  nofields=`expr $nofields - 1`
done

FIELDS="+1 $FIELDS"

         # (debug statements)
         # echo "fields: $FIELDS"
         # exit

echo "# utree tree list created at `date`" >$LIST
if test "$AFLAG" = "-a"
then
  find $HOME -type d -print | sort -t/ $FIELDS >>$LIST
else
  find $HOME -type d -print | grep -v "/\." | sort -t/ $FIELDS >>$LIST
fi
exit 0

