#!/bin/sh
#install -- install the Linux KB CD-Rom on the target machine.
#[-v = verbose]

TMP=/tmp

#defaults
HOST=localhost
INSTALL=FULL
DIR=/opt/linuxkb
HTDIG_VER=htdig-3.1.1

cd `dirname $0`
FROM=$PWD

### CHECK FOR ROOT ACCESS

if [ "$UID" != "0" ]; then
  echo -e \\afatal: not superuser
  exit
fi

### VERBOSE?

[ "$1" = "-v" ] && VERBOSE=1

cat << EOF
Linux Knowledge Base CD-Rom install program
This CD-Rom is a standalone version of the Linux KB at
http://linuxkb.cheek.com/.
EOF

[ $VERBOSE ] && echo "Verbose mode on.  Be prepared for lots of spew."
echo

### GET OPTIONS

cat << EOF
Minimal [30M] or Full [230M] install?  Minimal will run from the CD,
and require it to be mounted when accessing the KB.
EOF

echo -n "[m/F]: "
read INPUT

if [ "$INPUT" = "m" -o "$INPUT" = "M" ]; then
  INSTALL=MINIMAL
else
  INSTALL=FULL
fi

[ $VERBOSE ] && echo $INSTALL install chosen.

cat << EOF

Where do you want the program installed?  Default is $DIR.
EOF

echo -n "[$DIR]: "
read INPUT

if [ "$INPUT" != "" ]; then
  DIR=$INPUT
fi

[ $VERBOSE ] && echo Installing to $DIR

if [ ! -d "$DIR" ]; then

  mkdir -p $DIR

  if [ $? != 0 ]; then
    echo -e \\afatal: could not create $DIR
    exit
  fi

fi

### CONFIGURE WEB SERVER

if [ "$INSTALL" = "FULL" ]; then
  HTML=$DIR/html
else
  HTML=$FROM/html
fi

CGI=$DIR/cgi-bin

cat << EOF

A web server needs to be configured to serve html and cgi documents.  By
default, Apache will do this.  If you have Apache installed and are not
currently using it, I can configure this for you [this will overwrite
any existing Apache configuration].  Should I configure Apache for you?
If you have previously configured Apache for the Linux KB, you can choose
No here.
EOF

echo -n "[Y/n]: "
read INPUT

if [ "$INPUT" = "n" -o "$INPUT" = "N" ]; then
  [ $VERBOSE ] && echo "No automatic Apache configuration"
  cat << EOF

You will need to create a new web server with a Document Root of:
$HTML
and a CGI directory of:
$CGI
Press [ENTER] when this is done, and I will continue.  Alternatively,
type ^C to exit, configure the web server, and rerun this install script.
EOF
  echo -n -e \\a
  read
else
  [ $VERBOSE ] && echo "Automatic Apache configuration"
  CONF=/etc/httpd/apache/conf
  cat << EOF

Please tell me where the Apache config files are located.  Red Hat seems
to use /etc/httpd/conf, whereas Caldera Systems uses
/etc/httpd/apache/conf.
EOF
  echo -n "[$CONF]: "
  read INPUT
  if [ "$INPUT" != "" ]; then
    CONF=$INPUT
  fi

  [ $VERBOSE ] && echo Saving original srm.conf...
  cp -f $CONF/srm.conf $CONF/srm.conf.linuxkb

  if [ $? != 0 ]; then
    echo -e \\afatal: could not save $CONF/srm.conf.linuxkb
    exit
  fi

  grep -i -B 9999 ^DocumentRoot $CONF/srm.conf.linuxkb | \
  grep -i -v ^DocumentRoot > $CONF/srm.conf
  echo DocumentRoot $HTML >> $CONF/srm.conf
  echo \#KB DocumentRoot changed by Linux KB install >> $CONF/srm.conf
  echo \#KB original is below >> $CONF/srm.conf
  echo -n -e \#KB\  >> $CONF/srm.conf
  grep -i -A 9999 ^DocumentRoot $CONF/srm.conf.linuxkb >> $CONF/srm.conf

  cp -f $CONF/srm.conf $TMP/srm.conf.linuxkb

  if [ $? != 0 ]; then
    echo -e \\afatal: could not save $TMP/srm.conf.linuxkb
    exit
  fi

  grep -i -B 9999 ^ScriptAlias $TMP/srm.conf.linuxkb | \
  grep -i -v ^ScriptAlias > $CONF/srm.conf
  echo ScriptAlias /cgi-bin/ $CGI/ >> $CONF/srm.conf
  echo \#KB ScriptAlias changed by Linux KB install >> $CONF/srm.conf
  echo \#KB original is below >> $CONF/srm.conf
  echo -n -e \#KB\  >> $CONF/srm.conf
  grep -i -A 9999 ^ScriptAlias $TMP/srm.conf.linuxkb >> $CONF/srm.conf

  rm -f $TMP/srm.conf.linuxkb
  [ $VERBOSE ] && echo Changes made.

  [ $VERBOSE ] && echo Saving original access.conf...
  cp -f $CONF/access.conf $CONF/access.conf.linuxkb

  if [ $? != 0 ]; then
    echo -e \\afatal: could not save $CONF/access.conf.linuxkb
    exit
  fi

  cat << EOF >> $CONF/access.conf
#KB: Added by Linux KB install
<Directory $HTML>
Options Indexes FollowSymLinks
AllowOverride None
order allow,deny
allow from all
</Directory>

<Directory $CGI>
AllowOverride None
Options ExecCGI
</Directory>
#KB: end of changes
EOF

  [ $VERBOSE ] && echo Changes made.
fi

### COPY HTML

if [ "$INSTALL" = "FULL" ]; then
  echo Copying html files...

  if [ $VERBOSE ]; then
    cp -av $FROM/html $DIR
  else
    cp -a $FROM/html $DIR
  fi

  if [ $? != 0 ]; then
    echo -e \\afatal: could not copy html files
    exit
  fi
fi

### [RE]START APACHE

[ $VERBOSE ] && echo Attempting to [re]start Apache...

if [ -r /var/run/httpd.pid ]; then
  kill -1 `cat /var/run/httpd.pid`

  if [ $? != 0 ]; then
    echo -e \\awarning: could not restart apache
    cat << EOF
Please restart by hand using something like:
/etc/rc.d/init.d/httpd stop
/etc/rc.d/init.d/httpd start
Press [ENTER] when done:
EOF
    read
  else
    echo Apache restart successful.
  fi

else

  cat << EOF
The Apache webserver needs to be reinitialized.
Please restart it by hand using something like:
/etc/rc.d/init.d/httpd stop
/etc/rc.d/init.d/httpd start
Press [ENTER] when done:
EOF
  read

fi

### INSTALL HT://DIG

[ $VERBOSE ] && echo Installing $HTDIG_VER to $DIR/source

if [ ! -d $DIR/source ]; then

  mkdir -p $DIR/source

  if [ $? != 0 ]; then
    echo -e \\afatal: could not create $DIR/source
    exit
  fi

fi

if [ $VERBOSE ]; then
  tar xvzf $FROM/source/$HTDIG_VER.tar.gz -C $DIR/source
else
  tar xzf $FROM/source/$HTDIG_VER.tar.gz -C $DIR/source
fi

if [ $? != 0 ]; then
  echo -e \\afatal: ht://dig untar failed
  exit
fi

CONFIGOPTS="--prefix=$DIR/htdig"

cd $DIR/source/$HTDIG_VER

if [ $VERBOSE ]; then
  echo configuring ht://dig...
  ./configure $CONFIGOPTS
else
  ./configure --quiet $CONFIGOPTS
fi

if [ $? != 0 ]; then
  echo -e \\afatal: ht://dig configure failed
  exit
fi

[ $VERBOSE ] && echo changing CONFIG file...

cat << EOF > CONFIG
prefix=                $DIR/htdig
exec_prefix=           \${prefix}
DEST=                  \$(prefix)
BIN_DIR=               \$(exec_prefix)/bin
CONFIG_DIR=            \$(DEST)/conf
COMMON_DIR=            \$(DEST)/common
DATABASE_DIR=          \$(DEST)/db
DEFAULT_CONFIG_FILE=   \$(CONFIG_DIR)/linuxkb.conf
CGIBIN_DIR=            $CGI
IMAGE_DIR=             $HTML
IMAGE_URL_PREFIX=        
SEARCH_DIR=            $HTML
SEARCH_FORM=           index.html
EOF

if [ $? != 0 ]; then
  echo -e \\afatal: changing ht://dig CONFIG file failed
  exit
fi

[ $VERBOSE ] && echo changing CONFIG file successful.

if [ $VERBOSE ]; then
  echo making ht://dig...
  make
else
  make -s
fi

if [ $? != 0 ]; then
  echo -e \\afatal: ht://dig make failed
  exit
fi

[ $VERBOSE ] && echo ht://dig make successful.

if [ $VERBOSE ]; then
  echo installing ht://dig...
  make install
else
  make -s install
fi

if [ $? != 0 ]; then
  echo -e \\afatal: ht://dig install failed
  exit
fi

[ $VERBOSE ] && echo ht://dig install successful.

if [ $VERBOSE ]; then
  echo cleaning up ht://dig...
  make clean
else
  make -s clean
fi

if [ $? != 0 ]; then
  echo -e \\afatal: ht://dig clean up failed
  exit
fi

[ $VERBOSE ] && echo ht://dig clean up successful.

cd $FROM

[ $VERBOSE ] && echo creating and copying Linux KB config files...

cat << EOF > $DIR/htdig/conf/linuxkb.conf
# ht://dig configuration file
# generated from Linux Knowledge Base install
# visit the Linux KB on the web for more info
# http://linuxkb.cheek.com/

database_dir:           $DIR/htdig/db

start_url:              http://$HOST/http/ http://$HOST/ftp/

exclude_urls:           /cgi-bin/ .cgi
maintainer:             linuxkb@cheek.com
max_head_length:        10000
search_algorithm:       exact:1 synonyms:0.5 endings:0.1

next_page_text:         <img src=/buttonr.gif border=0 align=middle \
width=30 height=30 alt=next>

no_next_page_text:

prev_page_text:         <img src=/buttonl.gif border=0 align=middle \
width=30 height=30 alt=prev>

no_prev_page_text:

page_number_text:       "<img src=/button1.gif border=0 \
align=middle width=30 height=30 alt=1>" \
  "<img src=/button2.gif border=0 \
align=middle width=30 height=30 alt=2>" \
  "<img src=/button3.gif border=0 \
align=middle width=30 height=30 alt=3>" \
  "<img src=/button4.gif border=0 \
align=middle width=30 height=30 alt=4>" \
  "<img src=/button5.gif border=0 \
align=middle width=30 height=30 alt=5>" \
  "<img src=/button6.gif border=0 \
align=middle width=30 height=30 alt=6>" \
  "<img src=/button7.gif border=0 \
align=middle width=30 height=30 alt=7>" \
  "<img src=/button8.gif border=0 \
align=middle width=30 height=30 alt=8>" \
  "<img src=/button9.gif border=0 \
align=middle width=30 height=30 alt=9>" \
  "<img src=/button10.gif border=0 \
align=middle width=30 height=30 alt=10>"

no_page_number_text:    "<img src=/button1.gif border=2 \
align=middle width=30 height=30 alt=1>" \
  "<img src=/button2.gif border=2 \
align=middle width=30 height=30 alt=2>" \
  "<img src=/button3.gif border=2 \
align=middle width=30 height=30 alt=3>" \
  "<img src=/button4.gif border=2 \
align=middle width=30 height=30 alt=4>" \
  "<img src=/button5.gif border=2 \
align=middle width=30 height=30 alt=5>" \
  "<img src=/button6.gif border=2 \
align=middle width=30 height=30 alt=6>" \
  "<img src=/button7.gif border=2 \
align=middle width=30 height=30 alt=7>" \
  "<img src=/button8.gif border=2 \
align=middle width=30 height=30 alt=8>" \
  "<img src=/button9.gif border=2 \
align=middle width=30 height=30 alt=9>" \
  "<img src=/button10.gif border=2 \
align=middle width=30 height=30 alt=10>"
EOF

if [ $? != 0 ]; then
  echo -e \\afatal: creating Linux KB config files failed.
  echo $DIR/htdig/conf/linuxkb.conf
  exit
fi

cp -f $DIR/htdig/conf/linuxkb.conf $DIR/htdig/conf/linuxkb_lynx.conf

if [ $? != 0 ]; then
  echo -e \\afatal: creating Linux KB config files failed.
  echo $DIR/htdig/conf/linuxkb_lynx.conf copy
  exit
fi

cat << EOF >> $DIR/htdig/conf/linuxkb_lynx.conf

# KB: extra directives for lynx
search_results_header:        \${common_dir}/header.lynx.html
search_results_footer:        \${common_dir}/footer.lynx.html
EOF

if [ $? != 0 ]; then
  echo -e \\afatal: creating Linux KB config files failed.
  echo $DIR/htdig/conf/linuxkb_lynx.conf cat
  exit
fi

cat << EOF > $DIR/.config
CGI=$CGI
DIR=$DIR
FROM=$FROM
HOST=$HOST
HTDIG_VER=$HTDIG_VER
HTML=$HTML
INSTALL=$INSTALL
TMP=$TMP
VERBOSE=$VERBOSE
EOF

if [ $? != 0 ]; then
  echo -e \\afatal: creating Linux KB config files failed.
  echo $DIR/.config
  exit
fi

#symlink db's here on minimal install

if [ $VERBOSE ]; then
  cp -av htdig/common/* $DIR/htdig/common

  if [ "$INSTALL" = "FULL" ]; then
    cp -av htdig/db/* $DIR/htdig/db
  else
    ln -svf $FROM/htdig/db/* $DIR/htdig/db
  fi

else
  cp -a htdig/common/* $DIR/htdig/common

  if [ "$INSTALL" = "FULL" ]; then
    cp -a htdig/db/* $DIR/htdig/db
  else
    ln -sf $FROM/htdig/db/* $DIR/htdig/db
  fi

fi

if [ $? != 0 ]; then
  echo -e \\afatal: Linux KB config files copy failed
  exit
fi

[ $VERBOSE ] && echo Linux KB config files copy successful.

cat << EOF

The Linux Knowledge Base CD-Rom installation was successful!  To use,
point your web browser to http://$HOST/.
Lynx users use http://$HOST/lynx.html.

Thank you!

Joseph Cheek, Director
Cheek Consulting
http://linuxkb.cheek.com/
EOF
