#!/bin/bash
# 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.
# Latest change: Sam Okt 21 14:55:40 CEST 2006 [mika]
################################################################################

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

check4root || exit 1

LANG=C
LC_ALL=C
PN=$(basename "$0")
TMPFILE=$(mktemp)

function bailout(){
  rm -f "$TMPFILE"
  ewarn "Exiting as requested." ; eend 1
  exit "1"
}
trap bailout 1 2 3 15

TYPES=("valgrind" "reinstall because some files have been stripped" OFF \
"pgf" "reinstall because /usr/share/doc/texmf/latex/pgf has been removed" OFF \
"ion3" "reinstall because some files have been stripped" OFF)

einfo "Running some checks - this might take a few seconds..."

if [ -x /usr/bin/ion3 ] ; then
   debsums ion3 | grep -q FAILED || { unset TYPES[8]; unset TYPES[7]; unset TYPES[6] ; }
else
   unset TYPES[8]; unset TYPES[7]; unset TYPES[6]
fi

if [ -d /usr/share/doc/texmf/latex/pgf/version-for-tex4ht/ ] ; then
   debsums pgf | grep -q FAILED || { unset TYPES[5]; unset TYPES[4]; unset TYPES[3] ; }
 else
   unset TYPES[5]; unset TYPES[4]; unset TYPES[3]
fi

if [ -d /usr/lib/valgrind/x86-linux/ ] ; then
   debsums valgrind | grep -q FAILED || { unset TYPES[2]; unset TYPES[1]; unset TYPES[0] ; }
else
   unset TYPES[2]; unset TYPES[1]; unset TYPES[0]
fi

eend 0

if [ -z "$TYPES" ] ; then
   ewarn "Nothing to be done, everything has been fixed already. Exiting." ; eend 0
   exit 0
fi

dialog --title "$PN" \
      --single-quoted \
      --cancel-label 'Exit' --defaultno \
       --checklist "Please select the software packages you would like to re-install." 0 0 0 \
      "${TYPES[@]}" \
      2>$TMPFILE

retval=$?
choice=$(cat $TMPFILE)

if [ -z "$choice" ] ; then
   ewarn "No packages selected, nothing to be done." ; eend 0
   exit 0
fi

case $retval in
  0)
    einfo "Starting reinstallation of selected packages: $choice"
    apt-get update
    apt-get --reinstall install $choice
    einfo "Finished running ${PN}." ; eend $?
    ;;
  1)
    ewarn "Exiting as Cancel pressed." ; eend 1
    exit 1;;
  255)
    ewarn "Exit pressed, exiting."; eend 1
    exit 1;;
  *)
    eerror "Unexpected return code: $retval (ok would be 0)" ; eend 1
    exit 1;;
esac

rm -f $TMPFILE

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