#!/bin/bash

# Shell script to install the Applixware product

# Note to users: if you find a bug in this shell script and are able to
# fix it, or if you simply want to change something, copy the script to
# some writable directory and edit it.  Run it, and it will ask you what
# directory the CDROM is at, and then it will continue with the installation
# appropriately.

# Get in the right directory if necessary first!
if [ ! -f RPMS/i386/applixdemo-4.4.1-1.i386.rpm ] ; then
  # If we can't find the applix package, first try the directory that
  # the script was called in.
  relpath=$(echo $0 | sed 's:/[^/]*$::')
  [ -n "$relpath" ] && cd $relpath
fi

until [ -f RPMS/i386/applixdemo-4.4.1-1.i386.rpm ] ; 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 CDROM is mounted, or
press control-C to quit:
'
  read dir
  cd $dir
done

# Keep things consistent...
BT='Applixware 4.4.1 (Demo Release) for Linux'
DG=bin/whiptail

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 --msgbox 'Applix install failed with the following error from rpm:

'"$(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
    }'
}

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

# assume we're on a RedHat system
DEPS=

# assume sufficiently recent rpm, since this is being distributed with
# Redhat 5.2
RPM=y
RPMUP=n

T=-4.4.1-1.i386.rpm
# unless modified later, default to installing clipart
CLIP=on
# default to not installing other languages
AXL=off
# language defaults...
ENG_DEF=on
FRN_DEF=off
GRM_DEF=off

# not an upgrade
UP=n

D --msgbox 'Thank you for trying out Applixware 4.4.1 (Demo Release) for Linux!

Applixware needs a minimum of 118MB free disk space; if you install everything, you will need approximately 473MB.  When you choose which pieces to install, you will be told approximately how much disk space each piece takes.  You will be given a chance to cancel the installation before any software is installed on your system.

The demonstration release of Applixware has certain functional limitations: Words and Presents/Graphics only display the first page of documents, and Spreadsheets is limited to 20 rows by 20 columns.  For more information about purchasing Applixware 4.4.1 for Linux, please visit our web site at http://linux.applixware.com/
' 21 72

# User can potentially install a symlink without being root on the machine,
# but otherwise they need to be root.  Check now...
if [ $(id -u) -gt 0 ] ; then
  D --msgbox 'I am sorry, but the upgrade must be done as root.' 12 72
  die 2
fi

rm $O
until [ -s "$O" ] ; do
  D --separate-output --checklist --noitem \
    'Please choose at least one of the following languages, each of which takes approximately 52MB:' 14 75 3 \
    English $ENG_DEF \
    français $FRN_DEF \
    Deutsch $GRM_DEF
done

LANGS=$(cat $O)

numlangs=$(echo $LANGS | wc -w)
case $numlangs in
  *1*)
    if YN 'Would you like to install approximately 65MB of online documentation?' 10 75 ; then
      BKS=applixdemo-books-$(echo $LANGS | tr 'A-Z' 'a-z')
    else
      BKS=
    fi
    ;;
  *)
    D --separate-output --checklist --noitem \
      'For which languages would you like to install full online documentation?
Each language takes approximately 65MB' 15 75 3 \
     $(for i in $LANGS ; do echo "$i" off ; done )
    for language in $LANGS ; do
      BKS="$BKS applixdemo-books-$(echo $language | tr 'A-Z' 'a-z')"
    done
    ;;
esac

# Set up defaults for language dictionaries
FB=$AXL
DB=$AXL
for language in $LANGS ; do
  case $language in
    français)
      FB=on
      ;;
    Deutsch)
      DB=on
      ;;
  esac
done

D --separate-output --checklist \
  'Which extra language dictionaries would you like to install?
(American English is installed by default.)' \
  20 70 10 \
  Brazilian 'Brazilian Portuguese (.5MB)' $AXL \
  British 'British English (.8MB)' $AXL \
  Danish '(.5MB)' $AXL \
  Dutch '(1.7MB)' $AXL \
  Finnish '(.7MB)' $AXL \
  French '(.7MB)' $FB \
  Canadian-French '(.6MB)' $FB \
  German '(3MB)' $DB \
  Italian '(.5MB)' $AXL \
  Norwegian 'Norwegian Bokmċl (.8MB)' $AXL \
  Nynorsk 'Norwegian Nynorsk (.4MB)' $AXL \
  Portuguese '(.5MB)' $AXL \
  Spanish '(1MB)' $AXL \
  Swedish '(2.8MB)' $AXL \
  Swiss-German '(1.2MB)' $DB

for dict in $(cat $O) ; do
  DICTS="$DICTS applixdemo-dict-$(echo $dict | tr 'A-Z' 'a-z')"
done

cliparg=
if [ "$CLIP" = off ] ; then
  cliparg="--defaultno"
fi
if YN 'Would you like to install approximately 40MB of clipart?' 9 65 $cliparg ; then
  CLIP=on
else
  CLIP=off
fi

until [ -n "$PRE" ] ; do
  D --separate-output --inputbox \
  'By default, Applixware is installed with an install prefix of /opt, which puts the software in /opt/applix/ with a link from /opt/bin.  You may enter some other install prefix here if you like.

To put Applixware in /usr/local/bin and /usr/local/lib/applix, type
/usr/local/lib here.

To put Applixware in /usr/bin and /usr/lib/applix, type /usr/lib here:' \
  17 75 "/opt"
  PRE=$(cat $O)
  if [ $(echo $PRE | dd bs=1 count=1 2>/dev/null) != / ] ; then
    PRE=/$PRE
  fi
  if [ ! -d "$PRE" ] ; then
    D --msgbox "$PRE"' does not exist.' 10 72
    PRE=
  fi
done

# Make list of all packages to install...
a=applixdemo
pl=$a$T
[ "$CLIP" = on ] && pl="$pl $a-clipart$T"
for language in $LANGS ; do
  pl="$pl $a-$(echo $language | tr 'A-Z' 'a-z')$T"
done
for book in $BKS ; do
  pl="$pl $book$T"
done
for dict in $DICTS ; do
  pl="$pl $dict$T"
done
plt=
for package in $pl ; do
  plt="$plt RPMS/i386/$package"
done
pl=$plt

# Promised cancel button.  If rpm is available, report on disk space
# appropriately at the same time...
if [ "$RPM" = y ] ; then
  # Test for free disk space if RPM is available
  FS=$(df $PRE | tail -1 | awk '{print $4}')
  NS=$(($(for i in $pl; do rpm -qp --qf '%{size}\n' $i; done | \
       awk '{sum += $1} END {print sum}') / 1024))
  if [ $FS -lt $NS ] ; then
    if YN 'Applixware will not fit on your chosen target partition, '"$PRE"'

Only '$(($FS/1024))'MB are free on '"$PRE"', 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 '"$PRE"' 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 '"$PRE"' directory.

Choose yes to install, or no to abort.' 13 75 ; then : ; else
      die 0
    fi
  fi
else
  if YN 'Press yes to install the packages on your system, or no to abort.' 10 75 ; then : ; else
    die 3
  fi
fi

rpm $DEPS --prefix $PRE --percent -U $pl 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 --msgbox 'Applixware 4.4.1 (Demo Release) for Linux has been successfully installed on your system.' 12 72
  echo Demo Release > $PRE/applix/axdata/productid.dat
fi

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

die 0

