#!/bin/bash
# Filename:      ${GRML_FAI_CONFIG}/hooks/instsoft.GRMLBASE
# Purpose:       Grml specific software installation in the chroot, executed after updatebase
# 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 or any later version.
################################################################################

set -u
set -e

# shellcheck source=/dev/null
. "$GRML_LIVE_CONFIG"

# FAI sets ${target}, but shellcheck does not know that.
target=${target:?}

echo "hooks/instsoft.GRMLBASE running for action ${FAI_ACTION}"

# work around /etc/kernel/postinst.d/zz-update-grub failing
# inside openvz environment, see #597084
if [ '/usr/sbin/update-grub' = "$($ROOTCMD dpkg-divert --truename '/usr/sbin/update-grub')" ]; then
  echo "Diverting update-grub executable"
  $ROOTCMD dpkg-divert --rename --add /usr/sbin/update-grub
  $ROOTCMD ln -s /bin/true /usr/sbin/update-grub
fi

# work around a bug which causes openvz to freeze when grub-probe is invoked
if [ '/usr/sbin/grub-probe' = "$($ROOTCMD dpkg-divert --truename '/usr/sbin/grub-probe')" ]; then
  echo "Diverting grub-probe executable"
  $ROOTCMD dpkg-divert --rename --add /usr/sbin/grub-probe
  $ROOTCMD ln -s /bin/true /usr/sbin/grub-probe
fi

# Divert update-initramfs for performance.
if [ '/usr/sbin/update-initramfs' = "$($ROOTCMD dpkg-divert --truename '/usr/sbin/update-initramfs')" ]; then
  echo "Diverting update-initramfs executable"
  $ROOTCMD dpkg-divert --rename --add /usr/sbin/update-initramfs
  $ROOTCMD ln -s /bin/true /usr/sbin/update-initramfs
fi

# Do not fail running softupdate for reported bugs.
[ -d "${target}"/etc/apt/apt.conf.d ] || mkdir "${target}"/etc/apt/apt.conf.d
if [ -e "${target}"/etc/apt/apt.conf.d/10apt-listbugs ]; then
    mv "${target}"/etc/apt/apt.conf.d/10apt-listbugs "${target}"/etc/apt/apt.conf.d/10apt-listbugs.disabled
fi

if [ "$FAI_ACTION" = "softupdate" ] ; then
   # /etc/resolv.conf is usually a symlink, pointing out of the chroot.
   # Make it a file with known contents.
   rm -f "${target}"/etc/resolv.conf
   cat /etc/resolv.conf >> "${target}"/etc/resolv.conf

   if ! $ROOTCMD test -x /usr/bin/aptitude ; then
     $ROOTCMD apt-get -y install aptitude
   fi

   # make sure we can upgrade automatically,
   # even with unsigned repos, but only if user wants it
   if [ "${FAI_ALLOW_UNSIGNED:-}" = "1" ] ; then
     APTGET_OPTS="${APTGET_OPTS:-} --allow-unauthenticated"
     APTITUDE_OPTS="${APTITUDE_OPTS:-} --allow-untrusted"
   fi

   # make sure we don't fail when configuration files changed
   APTGET_OPTS="${APTGET_OPTS:-} -o DPkg::Options::=--force-confdef -o DPkg::Options::=--force-confmiss -o DPkg::Options::=--force-confnew"
   APTITUDE_OPTS="${APTITUDE_OPTS:-} -o DPkg::Options::=--force-confdef -o DPkg::Options::=--force-confmiss -o DPkg::Options::=--force-confnew"

   if $ROOTCMD test -x /usr/bin/aptitude ; then
      if $ROOTCMD aptitude --help | grep -q safe-upgrade ; then
         # shellcheck disable=SC2086 # APTITUDE_OPTS needs word-splitting.
         APT_LISTCHANGES_FRONTEND=none APT_LISTBUGS_FRONTEND=none $ROOTCMD aptitude -y $APTITUDE_OPTS safe-upgrade
      else
         # shellcheck disable=SC2086 # APTITUDE_OPTS needs word-splitting.
         APT_LISTCHANGES_FRONTEND=none APT_LISTBUGS_FRONTEND=none $ROOTCMD aptitude -y $APTITUDE_OPTS upgrade
      fi
   else
      # shellcheck disable=SC2086 # APTGET_OPTS needs word-splitting.
      APT_LISTCHANGES_FRONTEND=none APT_LISTBUGS_FRONTEND=none $ROOTCMD apt-get -y $APTGET_OPTS --force-yes upgrade
   fi

   exit  # make sure we don't continue behind the following "fi"
fi

# fresh installation, not softupdate.

# no hacks here, for now.

# }}}

## END OF FILE #################################################################
# vim:ft=sh expandtab ai tw=80 tabstop=4 shiftwidth=2
