#!/bin/bash

# Shell script to install Meta-HTML under RedHat 5.0 or newer.
VERSION=6.07

# default...
ARCH=i386
case `arch` in
  *86) ARCH=i386 ;;
  alpha) ARCH=alpha ;;
  sparc) ARCH=sparc ;;
esac

TARGET=RPMS/$ARCH/metahtml-${VERSION}-1.${ARCH}.rpm

# Get in the right directory if necessary first!
if [ ! -f $TARGET ] ; then
  relpath=$(echo $0 | sed 's:/[^/]*$::')
  [ -n "$relpath" ] && cd $relpath
fi

until [ -f $TARGET ] ; do
  # Now, as long as we are in the wrong directory, keep asking...
  echo 'This program must run from the CDROM.

Please type the name of the directory where the Meta-HTML RPMS directory
is located, or press Control-C to quit:
'
  read dir
  cd $dir
done

# The title that we show on all windows.
BT="                     Installing Meta-HTML $VERSION for $ARCH Linux"
DG=whiptail
# DG=dialog

D ()
{
  if [ -n "$O" ] ; then
    $DG --fb --backtitle "$BT" "$@" 2>$O || die 1
  else
    $DG --fb --backtitle "$BT" "$@" || die 1
  fi
  bluescreen
}
YN ()
{
  $DG --fb --backtitle "$BT" --yesno "$@"
  ret=$?
  bluescreen
  return $ret
}

die ()
{
  [ -s /tmp/ai.$$.log ] && {
    D --textbox "Meta-HTML installation Failed! (rpm errors):

  $(cat /tmp/ai.$$.log)" 21 72
  }
  if [ -x /usr/bin/tput ] ; then
    tput rmcup ; tput sgr0 ; clear
  else
    reset ; clear
  fi
  rm -f $O
  exit $1
}

rpm2dialog ()
{
  awk '
    function telldialog(string, percent)
    {
      printf("XXX\n %s\n%s\nXXX\n", percent, string)
      fflush()
      next
    }
    /^%f/ {
      sub("^%f ", "")
      sub(":.*$", "")
      packagename=$0
      telldialog(sprintf("Installing the %s package...", packagename), 0)
    }
    {
      sub("^%%", "")
      sub("\\..*$", "")
    }
    /100/ {
      telldialog(sprintf("Configuring the %s package...", packagename), 100)
    }
    {
      print
      fflush()
      next
    }'
}

findrpm ()
{
  (ls -1d RPMS/$ARCH/*; ls -1d RPMS/noarch/*) | \
    grep '/'"$1"'-[^-][^-]*-[^-][^-]*\.rpm'
}

TP=
[ -x /usr/bin/tput ] && {
  TP='[33;44m'"$(tput cup 0 0)$(tput clear)$(tput bold)$BT"
}
bluescreen ()
{
  echo -n $TP
}

O=/tmp/do.$$
touch $O || {
  echo "Could not touch file $O -- check permissions" >&2
  die 1
}

bluescreen

# welcome
old_BT="$BT" BT=""
D --msgbox "          Welcome to the Meta-HTML $VERSION Installation.

Meta-HTML is a programming language specifically designed for working \
within the World Wide Web environment.

Web pages are authored using HTML and Meta-HTML statements freely \
intermixed.  When a page is requested by a browser, the page is passed \
through the Meta-HTML interpreter, which dynamically processes any \
Meta-HTML statements to produce a final HTML page that is delivered \
to the browser.

More information on Meta-HTML is available from http://www.metahtml.com." 21 72

BT="$old_BT"

# package list selection

# First, packages that we WILL be installing...
# This package set should fulfill all interdependencies.
package_list="metahtml"

# "Optional" packages -- there should be no interdependencies between
# these packages
D --separate-output --checklist \
  "Each of these packages contains optional modules which may be installed as part of Meta-HTML.  They comprise dynamically loaded libraries which contain additional commands that you may find useful in the writing of your Meta-HTML Pages.

Since the modules do not make a running image of Meta-HTML any larger, the only reason you might have to not install them is if you are very tight on disk space -- in which case you might want to get more disk!

All of the modules together require less than a single megabyte of disk space.
" \
  20 75 8 \
  csv 'Comma Separated Values Module' on \
  directory 'Fancy HTML table style directory indexing' on \
  elsewhen 'Optional syntax for WHEN and IF commands' on \
  example 'The canonical example of how to write a module' on \
  lisp 'Some lisp-alike functions such as CAR and CDR' on \
  logs 'Hand coded Common Log File format parser' on \
  modmath 'Higher math functions (sqrt, atan, etc)' on \
  modmsql 'Access to mSQL databases' on \
  modmysql 'Access to MySQL databases' on \
  modpgsql 'Access to PostGres databases' on \
  modperl 'Embedded Perl commands in your page' on \
  parser 'Direct control over various actions of the parser' on \
  profiler 'Dump out function and execution profiling info' on \
  serverfuncs 'Build a TCP/IP server instantly' on \
  timer 'Handy functions for timing a block of code' on

package_list="$package_list $(cat $O)"

# Development packages?
if YN "Do you wish to install the Meta-HTML Module development source?" 10 60 ; then
  package_list="$package_list metahtml-module-src"
fi

# Make list of all packages to install...
plt=
for package in $package_list ; do
  plt="$plt `findrpm $package`"
done
package_list="$plt"

# Promised cancel button.
# Try to test for free disk space
FS=$(df /usr | tail -1 | awk '{print $4}')
NS=$(($(for i in $package_list; do rpm -qp --qf '%{size}\n' $i; done | \
       awk '{sum += $1} END {print sum}') / 1024))
if [ $FS -lt $NS ] ; then
  if YN "It appears that Meta-HTML will not fit on your /usr partition

Only $(($FS/1024))MB are free on /usr, but the packages you have chosen to install require $(($NS/1024+1))MB.

If you wish to start over and configure the installation differently, press return.

Otherwise, uninstall other software until $(($NS/1024+1))MB are free on /usr and then choose \"YES\" to attempt to install." 20 75 --defaultno ; then
      die 1
  fi
else
  if YN "The packages you have chosen will use $(($NS/1024+1))MB of the $(($FS/1024))MB available space in the filesystem containing the /usr directory.

Choose yes to install, or no to abort." 13 75 ; then : ; else
      die 0
  fi
fi

rpm --percent -U $package_list 2>/tmp/ai.$$.log | \
    rpm2dialog | \
    D --gauge "Please wait while RPM inspects your system..." 6 60 0

# if no error in the install
if [ ! -s /tmp/ai.$$.log ] ; then
  D --textbox "Meta-HTML $VERSION has been installed on your system.

Read the README file and docs/index.html for more information." 14 72
else
  die 1
fi

rm -f /tmp/ai.$$.log

die 0

