#!/bin/bash

version="0.01"
date="Tue Nov  9 00:40:30 GMT 1999"

###############################################################################
###############################################################################
#####
#####                   HOWTO
#####
###############################################################################
###############################################################################

# This program was written, and is maintained by David Webster (aka cognition)
# contact him on cognition@bigfoot.com if there are any problems.
#
# This program is ment to make using the howto documents easier, which in turn
# should help making Linux easier to use.
# 
# Feature suggestions/bugs/praise/flames can be sent to the above address.
# This program (and more from me) should be avaliable online from my web site:
# http://www.cognite.net/, check there for the latest version.
# 
# This program has been released under the GNU General Public License, if you 
# don't have a copy try searching Yahoo! for "GPL"
#
# Under the terms of the GPL, I cannot be held responsible for ANY damages
# problems, errors, etc. that may occur through the use/misuse of this
# program. If you have any doubt about the quality of this program, dont run
# it. OTOH, if you do find any problems let me know on the above address.
#
# Please direct spam to /dev/null, I've heard they can't get enough of it.
#
# cog.
#######################

# Defining default variables
HOWTO_DIR="/usr/doc/howto/html"
WORKING_DIR="/usr/local/bin/ldp"
# UPDATE_FILE="http://www.cognite.net/linux/howto/update.txt"
# commented out to make testing easier.
UPDATE_FILE="$WORKING_DIR/howtos/update.txt"
HOWTO_INDEX_PAGE="index.html"
HELP_TXT="help.txt"
HOWTO_SEARCH_DB="$WORKING_DIR/keyword.db.txt"
BROWSER="lynx"
#REMOTE_LOCATION="http://www.cognite.net/linux/howto"
REMOTE_LOCATION="$WORKING_DIR/howtos"
# If you want to test the update function you'll have to move some howto files 
# to the directory shown above. 
# Sanity checks for tailing '/'s will be included.


# Checking for a tmp dir
if [ ! -e $WORKING_DIR/tmp/. ]
then
mkdir $WORKING_DIR/tmp
fi

# Setting the tmp dir to a variable
TMP="$WORKING_DIR/tmp"

# defining the function USAGE which gives usage information
function USAGE
{
echo "USAGE: howto [option]"
echo "        OPTIONS:"
echo "		-c OR -config"
echo "			Configures this program 'howto' with your system's"
echo "			information, like the path of your HOWTO html files."
echo "		-i OR -install"
echo "			Installs some LDP functions to your system."
echo "		-k /keyword/ OR -keyword /keyword/"
echo "			Searches the LDP database for howto files containing"
echo "			the /keyword/, and gives you a HTML output."
echo "		-h OR -help"
echo "			Displays the contents of the LDP help file"
echo "		-u OR -update"
echo "			Checks for updated HOWTO documents from the LDP's "
echo "			website <http://www.linuxdoc.org/>."
echo ""
echo "If run with no options, howto will give you a HTML index of all avaliable HOWTO"
echo "documents."
echo ""
echo "Please report any bugs/errors to me on <cognition@bigfoot.com>"
}


# Defining the CONFIG function, which configures the program for this machine
function CONFIG
{
if [ -w /etc/ -o -w /etc/howtorc ]
then
   echo "This is a configuration controler, it'll step you through your configuration."
   echo "If you don't know the answer to a question, leave it blank and the default will"
   echo "be used."
   echo -n "Where are your english HTML HOWTO documents kept?"
   read ALPHA
   echo "What is the program's working directory? (It's normally the same as the dir"
   echo -n 'you extracted this file in to.)'
   read BETA
   echo -n 'Which Web Browser do you use? (e.g. Netcape)'
   read GAMMA
   echo "##LDP config file. for more information see /howto/"
   echo "HOWTO_DIR=$ALPHA"
   echo "WORKING_DIR=$BETA"
   echo "BROWSER=$GAMMA"
fi
}


# Checks for a config file
if [ ! -e /etc/howtorc ]
then
   echo "This program does not seem to have been configured on this machine."
   echo -n "Do you want me to run the configuration controller? [Y/N]"
   read ANS
   if [ $ANS = Y -o $ANS = yes ]
   then 
      CONFIG
   fi
else
   . /etc/howtorc
fi

# Processing command line arguments.
# If there are no cmd line arguments, open the howto index page.
if [ $# -eq 0 ]
then
   echo "Now opening the HOWTO index page..."
   $BROWSER $HOWTO_DIR/$HOWTO_INDEX_PAGE
# if the config argument has been supplied, run the CONFIG function
elif [ $1 = -c -o $1 = -config ]
then
   CONFIG
# if the keyword function has been used, search for the keyword
elif [ $1 = -k -o $1 = -keyword ]
then
   echo "Searching HOWTO keyword database for matches to $2"
   # Getting the relevent file names
   grep "$2" $HOWTO_SEARCH_DB | cut -d: -f1 >$TMP/tmp1
   # Getting the descriptions for the above file names
   grep "$2" $HOWTO_SEARCH_DB | cut -d: -f3 >$TMP/tmp3
   # Getting number of files
   FOO=`cat $TMP/tmp1|wc -l`
   BAR=" "
   FUB=${FOO#$BAR}
   NO_OF_MATCHES=`echo $FUB`
   MARK=0
   OUTPUT=$TMP/tmp2.html
   # Creating html output file
   cat $WORKING_DIR/.htmlheader >$OUTPUT
   echo "<P>" >>$OUTPUT
   echo "Search was for keyword: $2; Files matched = $NO_OF_MATCHES" >>$OUTPUT
   echo "</P>" >>$OUTPUT
   echo "<HR>" >>$OUTPUT
   while [ $MARK -ne $NO_OF_MATCHES ] 
   do
      let MARK=MARK+1
      FILE=`cat $TMP/tmp1 | head -$MARK | tail -1`
      DESCRIPTION=`cat $TMP/tmp3 | head -$MARK | tail -1`
      echo "<P>" >>$OUTPUT
      echo "<A href="$HOWTO_DIR/$FILE">$FILE</A>   - $DESCRIPTION " >>$OUTPUT 
      echo "">>$OUTPUT
      echo "</P>" >>$OUTPUT
   done
   cat $WORKING_DIR/.htmlfooter >>$OUTPUT
   $BROWSER $OUTPUT
   # removing unneeded files
   rm $OUTPUT
   rm $TMP/tmp1
# Displays the help file if asked for it.
elif [ $1 = -h -o $1 = -help ]
then
   echo "Displaying the LDP's help file"
   more $WORKING_DIR/$HELP_TXT
# This argument updates the howtos on the local machine
elif [ $1 = -u -o $1 = -update ]
then
   echo "This feature needs you to be connected to the internet, are you" 
   echo -n "currently connected? [Enter: Y/N]: "
   function ARE_YOU_CONNECTED
   {
      read ANSWER
      if [ $ANSWER = Y -o $ANSWER = Yes -o $ANSWER = y -o $ANSWER = yes ]
      then
      lynx -dump $UPDATE_FILE >$TMP/tmp6
      diff $TMP/tmp6 $WORKING_DIR/update.txt | grep ">" |cut -d: -f3 |grep -v "<" >$TMP/tmp7
      FOO=`cat $TMP/tmp7|wc -l`
      BAR=" "
      FUB=${FOO#$BAR}
      NO_OF_PAGES=`echo $FUB`
      MARK=0
      echo "Updating changed files from $REMOTE_LOCATION/ to $HOWTO_DIR/"
      echo "There are $NO_OF_PAGES pages that need updating."
      while [ $MARK -ne $NO_OF_PAGES ]
      do
         let MARK=MARK+1
         PAGE=`cat $TMP/tmp7 | head -$MARK | tail -1`
         echo "Now updating $PAGE. Page $MARK of $NO_OF_PAGES."
         if [ -w $HOWTO_DIR -o -w $HOWTO_DIR/$PAGE ]
         then
            wget $REMOTE_LOCATION/$PAGE --output-document=$HOWTO_DIR/$PAGE >/dev/null
         else
            echo "howto: ERROR, you do not have write Permissions to:" 
            echo "$HOWTO_DIR/$PAGE"
         fi
      done
      rm $TMP/tmp7
      rm $TMP/tmp6
      elif [ $ANSWER = N -o $ANSWER = No -o $ANSWER = n -o $ANSWER = no ]
      then
         echo "This function will not work without a internet connection, please connect"
         echo "and run this program again."
         exit
      else
         echo -n "That was an invalid option, please choose Yes or No:"
         ARE_YOU_CONNECTED
      fi
   }
   ARE_YOU_CONNECTED
# if the install option was specified, install various LDP functions
elif [ $1 = -i -o $1 = -install ]
then
   echo "Installing some LDP functions..."
   if [ -O /etc/skel -a -w /etc/skel ]
   then
      echo -n "Do you want to copy the help file to /etc/skel/?: [Y/N]"
      read CONTINUE
      if [ $CONTINUE = Y -o $CONTINUE = y -o $CONTINUE = yes ]
      then
         echo "Copying $HELP_TXT to /etc/skel/"
         cp $WORKING_DIR/$HELP_TXT /etc/skel/
      fi
   else
      echo "howto: ERROR, do not have ownership and write permissions to /etc/skel/"
   fi
   if [ -O /etc/motd -a -w /etc/motd ]
   then
      echo -n "Do you want to add a small line, about obtaining help, to your /etc/motd?"
      if [ $CONTINUE = Y -o $CONTINUE = y -o $CONTINUE = yes ]
      then
         echo "Adding the following line to your etc/motd file:"
         echo 'For information on how to get help, type "less $HELP_TXT"' | tee -a /etc/motd
      fi
   else
      echo "howto: ERROR, do not have ownership and write permissions to /etc/motd"
   fi
   if [ -O /etc/profile -a -w /etc/profile ]
   then
      echo -n "Do you wish to alias 'help' to 'howto -h' in you /etc/profile?"
      if [ $CONTINUE = Y -o $CONTINUE = y -o $CONTINUE = yes ]
      then
         echo "Now aliasing help to howto -h in your /etc/profile file."
         # more shells to be added soon, if needed.
         echo 'alias help="howto -h"' >> /etc/profile
      fi
   else
      echo "howto: ERROR, do not have ownership and write permissions to /etc/profile"
   fi
else
# if they get this far, they didn't specify a valid command line option, so:
      echo "howto: ERROR, you entered invalid options."
      USAGE
fi
# remove tmp files
rm $TMP/*>/dev/null
# end
