#/bin/bash

# Written as an excercise for sed, but actually does something useful.
# It is very slow, but who cares anyway.
# On my Celery 300MHz Notebook it takes about 50sec to complete, so 
# be patient
# 
# (c) 2000 Tilmann Bitterberg
# Mon Nov 20 02:51:52 CET 2000

########## CONFIGURE ############
 PROCROOT="/proc"                           # e.g /home/tibit/test/proc
#PROCROOT="/home/tibit/kernel/proc"         # e.g /home/tibit/test/proc
HEXPRINT="xxd"				   # e.g /usr/bin/hexdump
GSED="sed"
########## END CONFIGURE ############

# all dirs
function getdirs () {

  x=`echo $1|$GSED 's,.*/,,'` # strip fullpathname
  y=`echo $1|$GSED 's/..//;s,/,|,g'`
  echo "<P><HR size=2><P><TABLE bgcolor=blue><TR><TD><FONT size=+3 color=white>"
  echo "<A NAME=\"$y\">Node $x</A></FONT>"| $GSED 's/Node \./Root Node/g'
  echo "</TD></TR></TABLE><P>" 

  SEDDIR='s,..,,;s,/,|,g;s|.*|& <A HREF="#&"><B>&</B></A><BR>|'
  SEDFIXUP='s:\([^>]*>\)[^<]*|\([^<]*\):\1\2:'

  # Note: Puts name at the beginning for sorting, gets deleted afterwards
  #                                           !!
  SEDSYM='s|\(\([^ ]*/\)*[^:]*\):.*to \(.*\)$|\1 <A HREF="#\2\3"><I>\1 -\&gt; \3|;s,/,|,g;s|$|</I></A><BR>|'
  (
    find $1 -maxdepth 1 -type l | $GSED 's,\./,,' | xargs file 2>/dev/null | \
      $GSED -e "$SEDSYM" -e "$SEDFIXUP" 2>/dev/null
    find $1 -maxdepth 1 -type d 2>/dev/null|
    $GSED -e "$SEDDIR" -e "$SEDFIXUP" 
  ) | sort | $GSED 's|^[^ ]* ||'
}

function getproperty () {
  cd $1
  a=0;
  x=`echo $1|$GSED 's,\./,,'`
  echo "<P><TABLE bgcolor=lightblue><TR><TD><FONT size=+1>Properties of $x</FONT></TR></TD></TABLE>"
  echo "<TABLE>"
  for i in `find . -maxdepth 1 -type f | $GSED 's,\./,,;s, ,|,g'`; do
     if [ `expr $a % 2` = 0 ]; then
       echo "<TR bgcolor=\"#A9A9A9\">"
     else
       echo "<TR bgcolor=\"#FFFFFF\">"
     fi 
     echo "<TD><B>\"`echo $i|sed 's,|, ,g'`\"</B><PRE>"
     $HEXPRINT "`echo $i|$GSED 's,|, ,g'`"| $GSED '$s|$|</PRE></TD></TR>|'
     a=`expr $a + 1`
  done
  echo "</TABLE>"
  cd -
}

function getrest () {
  cd $1
  for i in `find . -type d`; do
     getdirs $i | $GSED '4d'
     getproperty $i
  done
  cd -
}

function getindex () {
  cd $1
  echo "<PRE>"
  echo "<A HREF=\"#.\">Root Node</A>"
  find . -type d | $GSED 'ss..ss;/^$/d;/^.$/d;s/^\.$//' | $GSED '
    h;           # save for later use
    s,/,|,g
    x
    :t;\|/|s|\(    *\)*[^/]*/|\1    |;tt
    \|^ |!s|^|/|
    G
    s,\( *\)\([^\n]*\)\n\(.*\),\1<A HREF="#\3">\2</A>,
    '
  echo "</PRE>"
  cd -
}

function getappendix () {
  cd $1
  echo "<H3>Appendix</H3><PRE><A HREF=\"#.\">Root Node</A>"
  find . | $GSED 'ss..ss;/^$/d;/^.$/d;s/^\.$//' | $GSED '
    :t;\|/|s|\(    *\)*[^/]*/|\1    |;tt
    \|^ |!s|^|/|
    '
  echo "</PRE>"
  cd -
}
# MAIN
olddir=`pwd -P`
cd $PROCROOT
NAME=`cat device-tree/name`
echo "<HTML><HEAD><TITLE>Device Tree $NAME</TITLE></HEAD><BODY bgcolor=white>"
echo "<H1>Device Tree $NAME</H1>generated on `date`<H3>Index</H3>"
getindex device-tree
getrest device-tree
cd $PROCROOT
# getappendix device-tree
echo "</BODY></HTML>"
cd $olddir
