# # Iceberg # http://cgrayson.github.com/iceberg/ # # (c) Chris Grayson # ICEBERG_VERSION=1.9.9 USAGE="$0: Usage: $0 [ -d du_input_filename | -l (use local JavaScript) | -p (plain text output) | -t tmp_dir ]" GEN_DATE=`date +"%m-%d-%Y"` CDN_URL=http://cgrayson.github.com/iceberg ICEBERG_JS_FILE=iceberg.js ICEBERG_CSS_FILE=iceberg.css ICEBERG_JS=${CDN_URL}/javascripts/${ICEBERG_JS_FILE} ICEBERG_CSS=${CDN_URL}/stylesheets/${ICEBERG_CSS_FILE} num_divs=0 ICEBERG_INPUT_FILE="iceberg_input.txt" ICEBERG_TEMP_FILE="iceberg_input.tmp" DU_INPUT_FILE="" HTML=1 TMPDIR="." while getopts ":d:lpt:" option do case $option in d ) DU_INPUT_FILE="$OPTARG" if [ ! -f "$DU_INPUT_FILE" ] then echo "Error: du input file '$DU_INPUT_FILE' not found" echo "$USAGE" exit 3 fi ;; l ) # Use local JavaScript & CSS (developer use) ICEBERG_JS=$ICEBERG_JS_FILE ICEBERG_CSS=$ICEBERG_CSS_FILE ;; p ) HTML=0 ;; t ) TMPDIR="$OPTARG" if [ ! -d "$TMPDIR" ] then echo "Error: tmp dir '$TMPDIR' not found" echo "$USAGE" exit 3 fi ;; * ) echo "Unknown option $option" echo $USAGE exit 3 ;; esac done # shift past all dash-options shift $(($OPTIND - 1)) if [ -n "$1" ] then ROOT=${1%%/} # strip rightmost "/", if present shift else ROOT="." fi if [ ! -d "$ROOT" ] then echo "Error: root dir '$ROOT' not found" echo $USAGE exit 4 fi create_input_file() { if [ -z "$DU_INPUT_FILE" ] then DU_INPUT_FILE="$TMPDIR/$ICEBERG_TEMP_FILE" du -h "$ROOT" > $DU_INPUT_FILE RM_DU_INPUT=1 # clean up this tmp file later (only if not provided by user) fi # wish I knew how to use a variable to pipe commands to... echo "reverse utility test" | tail -r > /dev/null 2>&1 if [ $? -eq 0 ] then cat "$DU_INPUT_FILE" | tail -r > "$ICEBERG_INPUT_FILE" else echo "reverse utility test" | tac > /dev/null 2>&1 if [ $? -eq 0 ] then cat "$DU_INPUT_FILE" | tac > "$ICEBERG_INPUT_FILE" fi fi if [ "$RM_DU_INPUT" = 1 ] then rm "$DU_INPUT_FILE" fi } indent() { # save up to 35% in filesize by skipping indentation in HTML output if [ "$HTML" -eq 0 ] then i=$1 while [ $i -gt 0 ] do echo -n "$2 " let i=$i-1 done fi echo $3 } # $1 - previous level number # $2 - current level number # $* - row record (size string, tab, path string) rowprint() { prevlevel=$1 currlevel=$2 shift 2 args="$*" size="${args%%[ ]*}" # get first field (i.e., trim off leftmost whitespace and following) filename="${args##*/}" # get final file/dirname (i.e., trim up to & incl. rightmost "/") if [ "$HTML" -eq 1 ] then if [ "$currlevel" -gt "$prevlevel" ] then indent $prevlevel "" "
$size $filename
" let num_divs=$num_divs+1 else indent $prevlevel "" "
$size $filename
" fi else indent $prevlevel "|" "$*" fi } endrows() { if [ "$HTML" -eq 1 ] then end_prev=$1 currlevel=$2 while [ "$end_prev" -gt "$currlevel" ] do indent $end_prev "" "
" let num_divs=$num_divs-1 let end_prev=$end_prev-1 done fi } dirsize() { IFS=" " prevrec='xxx' prevlevel=-1 create_input_file for rec in `cat $ICEBERG_INPUT_FILE` do levelstr1="${rec##*[ ]}" # get the 2nd field, from the leftmost tab to EOL levelstr2="${levelstr1//[^\/]/}" # remove all characters but the "/"s level="${#levelstr2}" # count the remaining "/"s if [ "$prevlevel" -ge 0 ] then rowprint $prevlevel $level $prevrec if [ "$level" -lt "$prevlevel" ] then endrows $prevlevel $level fi fi prevlevel=$level prevrec="$rec" done level=`echo "$rec" | cut -f2 | tr -dc "/" | wc -c` if [ "$level" -lt "$prevlevel" ] then endrows $prevlevel $level fi rowprint $prevlevel $level $prevrec while [ "$num_divs" -gt 0 ] do endrows 9 8 done rm "$ICEBERG_INPUT_FILE" } if [ "$HTML" -eq 1 ] then cat < Iceberg: $ROOT
generated on $GEN_DATE by Iceberg v.${ICEBERG_VERSION}
Loading...
HERE_DOC fi dirsize if [ "$HTML" -eq 1 ] then echo "
" echo "
" echo "" fi