#!/bin/sh
# Filename:      install-packages-office
# Purpose:       install office related packages
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

if [ $(id -u) -ne 0 ]; then
   echo "Error: Please run this script as user root. Exiting"
   exit 1
fi

if [ "$1" = '-force' ]; then
  echo "Option force detected - installing all packages without any further questions."
  FORCE="-y --allow-unauthenticated"
fi

LANG=C
LC_ALL=C
PN=`basename "$0"`
TMPFILE=$(mktemp)
TMPFILE2=$(mktemp)
ERRORFILE=$(mktemp)
APTFILE=$(mktemp)
SOURCES_LIST='/etc/apt/sources.list.useful'

# main program
interface(){
  dialog --title "$PN" \
        --single-quoted \
        --checklist "Please select the software packages you would like to install." 0 0 0 \
        "GIMP"                 "Install The GNU Image Manipulation Program" OFF \
        "OpenOffice"           "Install the full-featured office productivity suite" OFF \
        "German files for OO"  "Install german thesaurus and language package for OpenOffice.org" OFF \
        "English files for OO" "Install english thesaurus and language package for OpenOffice.org" OFF \
        "KOffice"              "Install the KDE Office Suite" OFF \
  2>$TMPFILE

retval=$?
choice=`cat $TMPFILE`

if echo $choice | grep -q "GIMP"; then
    echo "gimp" >> $TMPFILE2
fi

if echo $choice | grep -q "OpenOffice"; then
    echo "openoffice.org" >> $TMPFILE2
fi

if echo $choice | grep -q "German files for OO"; then
   echo "myspell-de-at" >> $TMPFILE2
   echo "openoffice.org-thesaurus-de" >> $TMPFILE2
   echo "openoffice.org-l10n-de" >> $TMPFILE2
fi

if echo $choice | grep -q "English files for OO"; then
    echo "myspell-en-us " >> $TMPFILE2
    echo "openoffice.org-thesaurus-en-us" >> $TMPFILE2
fi

if echo $choice | grep -q "KOffice"; then
   echo "kchart" >> $TMPFILE2
   echo "kdegraphics" >> $TMPFILE2
   echo "kformula" >> $TMPFILE2
   echo "koffice" >> $TMPFILE2
   echo "koshell" >> $TMPFILE2
fi

case $retval in
  0)
    ;;
  1)
    echo "Exiting as Cancel pressed.";
    exit 1;;
  255)
    echo "Esc pressed, exiting.";
    exit 1;;
  *)
    echo "Unexpected return code: $retval (ok would be 0)"
    exit 1;;
esac
}

interface

if [[ `cat $TMPFILE2 | tr -d '\n'` == '' ]] ; then
  dialog --title "$PN" --msgbox "No packages(s) selected." 0 0
else
  echo "Starting installation of selected packages."
  echo
  echo "Creating $SOURCES_LIST and running apt-get update..."
  echo

  cat >$SOURCES_LIST <<EOT
deb     http://deb.grml.org/ grml-stable  main
deb     http://ftp.de.debian.org/debian        unstable        main contrib non-free
EOT
  cat $APTFILE >> $SOURCES_LIST

  apt-get $FORCE -o Dir::Etc::SourceList=$SOURCES_LIST update 2>>$ERRORFILE

  echo
  echo "Running apt-get install for selected packages..."
  echo
  apt-get $FORCE -o Dir::Etc::SourceList=$SOURCES_LIST install `cat $TMPFILE2` 2>>$ERRORFILE

  echo
  echo "Finished running install-packages-office."
  echo "A log of installed packages is available at $TMPFILE2, and the error log (if any) is available at $ERRORFILE"
fi

rm -f $TMPFILE $APTFILE

## END OF FILE #################################################################
