#!/bin/bash
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#generates index.html master help page. called from petget, rc.update, createpuppy.
#if no passed param, regenerate entire index.html.
# $1=insert|remove optional first param (without, default is insert).
# rest of passed params is a specification for finding the help files:
#  inserting: $2 is path prefix of the expanded new package.
#  removing: $2 is path/filename of .files, list of installed files.
#v3.02 2nov2007 BK: totally rewritten. note: ignoring any passed params.
#v3.02 /usr/share/doc/index.html now has javascript dropdown menus.
#ignoring any passed params, just regenerate entire index.html.

WKGDIR="`pwd`"

#not really the best place for a homepage database...
HOMEPAGEDB='abiword http://www.abisource.com/
chtheme http://plasmasturm.org/code//gtk-chtheme/
hardinfo http://hardinfo.berlios.de/HomePage
gparted http://gparted.sourceforge.net/
grub http://www.gnu.org/software/grub/
alsa http://www.alsa-project.org/main/index.php/Main_Page
xarchive http://xarchive.sourceforge.net/
gexec http://www.electricmonk.nl/index.php?page=gExec
fotox http://kornelix.squarespace.com/fotox/
picpuz http://kornelix.squarespace.com/picpuz/
asunder http://littlesvr.ca/asunder/
zmixer http://zwin.org/projects/zmixer/
gcurl http://www.gnomefiles.org/app.php/gCurl
dkop http://kornelix.squarespace.com/dkop/
gnomeppp http://www.gnomefiles.org/app.php?soft_id=41
gnome-ppp http://www.gnomefiles.org/app.php?soft_id=41
gftp http://gftp.seul.org/'

#search for installed pkgs with descriptions...
#search .desktop files...
PKGINFO1="`ls -1 /usr/share/applications | sed -e 's%^%/usr/share/applications/%' | xargs cat - | grep '^Name=' | cut -f 2 -d '='`"
#search pkg database...
PKGINFO2b=""
PKGINFO3=""
PKGINFO2="`grep '" on "' /root/.packages/packages.txt | cut -f 4 -d '"'`" #'geany
[ -s /root/.packages/livepackages.txt ] && PKGINFO2b="`grep '" on "' /root/.packages/livepackages.txt | cut -f 4 -d '"'`" #'geany
[ -s /root/.packages/alienpackages.txt ] && PKGINFO3="`grep '" on "' /root/.packages/alienpackages.txt | cut -f 4 -d '"'`" #'geany
PKGINFODB="$PKGINFO2
$PKGINFO2b
$PKGINFO3"
PKGINFODB="`echo "$PKGINFODB" | sort -u`"
NAMELISTsd=" `echo "$PKGINFODB" | cut -f 1 -d ' ' | tr '\n' ' '`"
##find binaries without descriptions...
#PKGINFO4="`ls -1 /bin`"
#PKGINFO5="`ls -1 /sbin`"
#PKGINFO6="`ls -1 /usr/bin | grep -v '\['`"
#PKGINFO7="`ls -1 /usr/sbin`"
#PKGINFO8="`ls -1 /usr/X11R7/bin`"
#PKGINFONODESCR="$PKGINFO4
#$PKGINFO5
#$PKGINFO6
#$PKGINFO7
#$PKGINFO8"
##sort the list...
#PKGINFONODESCR="`echo "$PKGINFONODESCR" | sort -u`"

#echo -n "" >/tmp/newinfoindex.xml
EXCLLISTsd=" pupdvdtool wallpaper pupctorrent pschedule prename pprocess pmirror pfind pcdripper pbackup 0rootfs_skeleton "

cp -f /usr/share/doc/index.html.top /tmp/newinfoindex.xml

#dropdown menu for apps in menu...
echo '<p>桌面菜单里的可用软件:</p>' >>/tmp/newinfoindex.xml
echo '<center>
<form name="form">
<select name="site" size="1" onchange="javascript:formHandler()">
' >>/tmp/newinfoindex.xml
echo "$PKGINFO1" |
while read ONEINFO
do
 NAMEONLY="`echo "$ONEINFO" | cut -f 1 -d ' ' | tr [A-Z] [a-z]`"
 EXPATTERN=" $NAMEONLY "
 nEXPATTERN="^$NAMEONLY "
 [ "`echo "$EXCLLISTsd" | grep -i "$EXPATTERN"`" != "" ] && continue
 [ "`echo "$NAMELISTsd" | grep -i "$EXPATTERN"`" = "" ] && continue
 HOMESITE="http://en.wikipedia.org/wiki/${NAMEONLY}"
 REALHOME="`echo "$HOMEPAGEDB" | grep -i "$nEXPATTERN" | head -n 1 | cut -f 2 -d ' '`"
 [ "$REALHOME" != "" ] && HOMESITE="$REALHOME"
 echo "<option value=\"${HOMESITE}\">${ONEINFO}" >> /tmp/newinfoindex.xml
done
echo '</select>
</form>
</center>
' >> /tmp/newinfoindex.xml

#dropdown menu for all installed pkgs...
echo '<p>所有安装在Puppy里的pet包:</p>' >>/tmp/newinfoindex.xml
echo '<center>
<form name="form2">
<select name="site2" size="1" onchange="javascript:formHandler2()">
' >>/tmp/newinfoindex.xml
echo "$PKGINFODB" |
while read ONEINFO
do
 [ "$ONEINFO" = "" ] && continue
 NAMEONLY="`echo "$ONEINFO" | cut -f 1 -d ' ' | tr [A-Z] [a-z]`"
 EXPATTERN=" $NAMEONLY "
 nEXPATTERN="^$NAMEONLY "
 [ "`echo "$EXCLLISTsd" | grep -i "$EXPATTERN"`" != "" ] && continue
 HOMESITE="http://en.wikipedia.org/wiki/${NAMEONLY}"
 REALHOME="`echo "$HOMEPAGEDB" | grep -i "$nEXPATTERN" | head -n 1 | cut -f 2 -d ' '`"
 [ "$REALHOME" != "" ] && HOMESITE="$REALHOME"
 echo "<option value=\"${HOMESITE}\">${ONEINFO}" >> /tmp/newinfoindex.xml
done
echo '</select>
</form>
</center>
' >> /tmp/newinfoindex.xml

##dropdown menu for all executables...
#echo '<p>All executable files in Puppy:</p>' >>/tmp/newinfoindex.xml
#echo '<center>
#<form name="form">
#<select name="site" size="1" onchange="javascript:formHandler()">
#' >>/tmp/newinfoindex.xml
#echo "$PKGINFONODESCR" |
#while read ONEINFO
#do
# [ "`echo "$ONEINFO" | grep -E 'NOTUSED|FULL|\.bin$|config$|README|OLD|\.glade$'`" != "" ] && continue
# EXPATTERN=" $ONEINFO "
# [ "`echo "$EXCLLISTsd" | grep -i "$EXPATTERN"`" != "" ] && continue
# echo "<option value=\"http://linux.die.net/man/${ONEINFO}\">${ONEINFO}</option>" >> /tmp/newinfoindex.xml
#done
#echo '</select>
#</form>
#</center>
#' >> /tmp/newinfoindex.xml

#now complete the index.html file...
cat /usr/share/doc/index.html.bottom >> /tmp/newinfoindex.xml
mv -f /tmp/newinfoindex.xml /usr/share/doc/index.html



#just exit...
exit
#################################


HLPPREFIX="";ADDMODE=""
if [ $1 ];then
 [ "$1" = "insert" ] && ADDMODE="insert"
 [ "$1" = "remove" ] && ADDMODE="remove"
 HLPPREFIX="$@"
 [ "$ADDMODE" = "insert" ] && HLPPREFIX="`echo -n "$@" | sed -e 's/^insert //g'`"
 [ "$ADDMODE" = "remove" ] && HLPPREFIX="`echo -n "$@" | sed -e 's/^remove //g'`"
 [ "$ADDMODE" = "" ] && ADDMODE="insert" #default.
 #extract all the current links...
 grep '<li><a href="/usr/share/doc' /usr/share/doc/index.html > /tmp/index-middle-old
fi

if [ "$HLPPREFIX" = "" ];then
 #regenerate everything...
 HLPPGS="`find /usr/share/doc -maxdepth 5 -name \*.htm -o -name \*.html -o -name \*.txt | tr '\n' ' '`"
 SYMLNKS="`find /usr/share/doc -maxdepth 1 -type l | tr "\n" " "`"
 for ONELNK in $SYMLNKS
 do
  if [ -d $ONELNK ];then
   ONEHLP="`find $ONELNK -maxdepth 5 -name \*.htm -o -name \*.html -o -name \*.txt | tr '\n' ' '`"
   HLPPGS="${HLPPGS} ${ONEHLP}"
  fi
 done
else
 if [ "$ADDMODE" = "insert" ];then
  cd $HELPPREFIX
  HLPPGS="`find ./usr/share/doc -maxdepth 5 -name \*.htm -o -name \*.html -o -name \*.txt | sed -e 's/^\.//g' | tr '\n' ' '`"
  SYMLNKS="`find ./usr/share/doc -maxdepth 1 -type l | tr "\n" " "`"
  for ONELNK in $SYMLNKS
  do
   if [ -d $ONELNK ];then
    ONEHLP="`find $ONELNK -maxdepth 5 -name \*.htm -o -name \*.html -o -name \*.txt | sed -e 's/^\.//g' | tr '\n' ' '`"
    HLPPGS="${HLPPGS} ${ONEHLP}"
   fi
  done
  cd $WKGDIR
 else #remove
  HLPPKGS="`cat $HELPPREFIX | grep '^/usr/share/doc/' | tr '\n' ' '`"
 fi
fi

#generate top half...
cp -af /usr/share/doc/index-skeleton-top /usr/share/doc/index.html

#application help links...
echo -n "" > /tmp/index-middle
echo "Generating master help page /usr/share/doc/index.html..."
for ONEPG in $HLPPGS
do
 [ "$ONEPG" = "/usr/share/doc/index.html" ] && continue
 [ ! "`echo -n "$ONEPG" | grep --extended-regexp '/HOWTO|ANNOUNCE|CHANGES|README|LICENCE'`" = "" ] && continue
 echo "Processing $ONEPG" 
 ONEBASE="`basename $ONEPG`"
 ONENAME="`echo -n "$ONEBASE" | cut -f 1 -d '.'`"
 ONEDIR="`dirname $ONEPG`"
 
 #what about some short description?
 #search packages.txt and alienpackages.txt...
 APATTERN="^\"${ONENAME}\-"
 cp -f /root/.packages/packages.txt /tmp/allpackages.txt.tmp
 [ -f /root/.packages/alienpackages.txt ] && cat /root/.packages/alienpackages.txt >> /tmp/allpackages.txt.tmp
 MENUDESCR="`cat /tmp/allpackages.txt.tmp | grep -i "${APATTERN}" | head -n 1 | cut -f 4 -d '"' | cut -f 2 -d ':' | sed -e 's/^ //g'`" #'Geany
 rm -f /tmp/allpackages.txt.tmp
 #search .desktop files...
 APATTERN="^${ONENAME}[-.]"
 [ "$MENUDESCR" = "" ] && MENUDESCR="`ls -1 /usr/share/applications | grep -i "${APATTERN}" | head -n 1 | cut -f 2-9 -d '-' | cut -f 1 -d '.' | tr '-' ' '`"
 MENUNAME="`ls -1 /usr/share/applications | grep -i "${APATTERN}" | head -n 1 | cut -f 1 -d '-'`"
 [ ! "$MENUDESCR" = "" ] && MENUDESCR=", $MENUDESCR"
 [ ! "$MENYNAME" = "" ] && ONENAME="$MENUNAME"
 
 if [ "$ONENAME" = "index" ];then
  ONENAME="`basename $ONEDIR`"
 fi
 
 echo -e "<li><a href=\"${ONEPG}\">${ONENAME}</a>${MENUDESCR}</li>" >> /tmp/index-middle
done

[ "$ADDMODE" = "insert" ] && cat /tmp/index-middle-old >> /tmp/index-middle
if [ "$ADDMODE" = "remove" ];then
 #/tmp/index/middle has entries that are to be removed from /tmp/index-middle-old...
 diff /tmp/index-middle-old /tmp/index-middle | grep '^< ' | sed -e 's/^< //g' > /tmp/index-middle-new
 sync
 mv -f /tmp/index-middle-new /tmp/index-middle
fi
cat /tmp/index-middle | sort --field-separator='>' --key=2 --unique >> /usr/share/doc/index.html
sync

#generate bottom half...
cat /usr/share/doc/index-skeleton-bottom >> /usr/share/doc/index.html

###END###
