#!/bin/sh
##
## <san2(at)linuxchannel.net>
## -- 2008.02.14: some bug fixed
## -- 2002.04.09: new build
##
## only tree V1.2 and lower
## if tree V1.3 and later(1.4b), use the '-H ""' option

gettreelist() {
    addopt=$1
    treelist=`tree -f -F $addopt 2>/dev/null | sed \
        -e "s;-- \./;-- /;g" \
        -e "s;$;${eds};" \
        -e "s;^\./$;.;" \
        -e "s;^/$;;"`
    IFS='--'
    total=`echo $treelist | tail -n 1`
    treelist=${treelist%$total}
    IFS=$IFS_ORIG
}

maketree() {

    type=$1
    file="$2.$type"
    IFS=$IFS_MY

    case "$type" in
        html)
            BR="<BR>"
            for f in $treelist ; do
                f=`echo $f | sed -e "s;\[;__T__;g" -e "s;\;;__S__;g"`
                fullpath=${f##*-- }
                linkpath=${fullpath% ->*}
                linkpath=${linkpath%\**}
                link=`echo "$f" | sed -e "s;${fullpath};<A HREF='${linkpath}'>${fullpath}</A>;" \
                    -e "s;__T__;[;g" -e "s;__S__;\;;g"`
                newtree=${newtree}${IFS}${link}
            done
            ;;
        txt)
            viewhtml="${IFS}[X] See html version : $2.html"
            newtree=$treelist
            ;;
    esac

    > $file
    echo "[X] build date : $builddate${BR}" > $file
    echo "[X] http://ftp.linuxchannel.net directory tree hierachy.${viewhtml}${BR}" >> $file
    echo "$BR" >> $file
    echo "<PRE>" >> $file
    echo "[${total%/}]" >> $file
    echo "$newtree" >> $file
    echo "</PRE>" >> $file

    IFS=$IFS_ORIG

    ## inital, delete $newtree
    ##
    newtree=''    ## unset newtree
    viewhtml=''    ## unset viewhtml
    BR=''        ## unset BR
}

LANG=C
builddate=`date '+%Y-%m-%d (%X %p), %A %Z' 2>/dev/null`
IFS_ORIG=$IFS
IFS_MY='
'

#cd ~

## make full list ###################
##
gettreelist
maketree "txt" "tree-full"
maketree "html" "tree-full"

## make directory list ##############
##
eds='/'
gettreelist "-d"
maketree "txt" "tree"
maketree "html" "tree"

#ln -sf tree.txt README.txt

exit 0