#!/bin/sh
# Filename:      grml2hd-fix
# Purpose:       fix issues of grml live-cd on hard disk installation
# 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.
################################################################################

PN="$(basename $0)"
TMP=$(mktemp)
ERROR=$(mktemp)
DIALOG=dialog
CONFFILE='/etc/runlevel.conf'

. /etc/grml/script-functions
. /etc/grml/lsb-functions

check4root || exit 100

bailout(){
  rm -f "$TMP" "$ERROR"
  exit 1
}

trap bailout 1 2 3 15

INFO="Please select the software package you would like to re-install.

Notice: re-installing software packages requires a working
network connection as packages will be installed via apt-get.
"

# present only "broken" packages
einfo "Running some checks... this might take a few seconds..."

if [ -d /usr/share/doc/texlive-base-bin/pdftex/ -a -d /usr/share/doc/texlive-latex-base/ ] ; then
   if debsums texlive-base-bin 2>&1 | grep -qe FAILED -qe 'No such file' ; then
      TEXLIVE='texlive!reinstall because some doc files have been removed!on'
   fi
fi

if [ -x /usr/bin/ion3 ] ; then
   if debsums ion3 2>&1 | grep -qe FAILED -qe 'No such file' ; then
      ION3='ion3!reinstall because some files have been stripped!on'
   fi
fi

if [ -d /usr/share/doc/texmf/latex/pgf/version-for-tex4ht/ ] ; then
  if debsums pgf 2>&1 | grep -qe FAILED -qe 'No such file' ; then
      PGF='pdf!reinstall because /usr/share/doc/texmf/latex/pgf has been removed!on'
   fi
fi

if [ -d /usr/lib/valgrind/x86-linux/ ] ; then
   if debsums valgrind 2>&1 | grep -qe FAILED -qe 'No such file' ; then
      VALGRIND='valgrind!reinstall because some files have been stripped!on'
   fi
fi

# adjust setup
reinstall_packages(){
  if [ -n "$TEXLIVE" ] ; then
     choice="$choice texlive-base-bin texlive-latex-base texlive-latex-recommended"
  fi

  if [ -n "$ION3" ] ; then
     choice="$choice ion3"
  fi

  if [ -n "$PGF" ] ; then
     choice="$choice pgf"
  fi

  if [ -n "$VALGRIND" ] ; then
     choice="$choice valgrind"
  fi

  apt-get update
  apt-get --reinstall install $choice 2>$ERROR

  return $?
}

# the interface itself
oifs="$IFS"
IFS='!'
if [ -z "$TEXLIVE" -a -z "$ION3" -a -z "$PGF" -a -z "$VALGRIND" ] ; then
   einfo "Nothing to be done, everything OK already." ; eend 0
   exit 0
else
   $DIALOG --title "$PN" --checklist "$INFO" 0 0 0 $TEXLIVE $ION3 $PGF $VALGRIND 2>$TMP
fi
IFS="$oifs"

retval="$?"
case $retval in
    (0)   reinstall_packages ;;
    (1)   echo "Cancel pressed." ; exit 1 ;;
    (255) echo "ESC pressed."    ; exit 1 ;;
esac

retval=$?
case $retval in
    (0)
          $DIALOG --title "$PN" --stdout --msgbox "Reinstalling package(s) was successful." 0 0
          esyslog user.notice "$PN" "Reinstalling package(s) was successful."
          ;;
    *)
          $DIALOG --title "$PN" --stdout --msgbox "Error reinstalling package(s).

$(cat $ERROR)

" 0 0
          bailout 1
          esyslog user.notice "$PN" "Error reinstalling package(s)."
          ;;
esac

rm -f "$TMP" "$ERROR"
IFS="$oifs"

## END OF FILE #################################################################
# vim: ai tw=80 expandtab
