#!/bin/bash
# Filename:      ${GRML_FAI_CONFIG}/hooks/updatebase.GRMLBASE
# Purpose:       Updates the base packages of the system, prepare chroot for instsoft
# 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:?}

# visualize chroot inside zsh:
echo grml_chroot > "${target}"/etc/debian_chroot

echo "$HOSTNAME" > "${target}"/etc/hostname

if [ -n "${APT_PROXY:-}" ] ; then
  cat > "$target"/etc/apt/apt.conf.d/90grml-apt-proxy.conf <<EOF
Acquire::http { Proxy "$APT_PROXY"; };
EOF
fi

# install all apt related files
fcopy -M -i -B -v -r /etc/apt
fcopy -M -i -B -v -r /usr/share/keyrings

# install packages from a repository of a specific date
if [ -n "${WAYBACK_DATE:-}" ] ; then
  echo "Wayback date '$WAYBACK_DATE' identified, enabling for snapshot.debian.org usage."

  perl -pi -e 'BEGIN { $d="'"$WAYBACK_DATE"'"; }
    s#^(URIs:)\s+(.*://deb.debian.org/)(.*)\/?$#$1 http://snapshot.debian.org/archive/$3/$d#' \
    "${target}"/etc/apt/sources.list.d/debian.sources

  sed -i '/^Types:/a Check-Valid-Until: no' "${target}"/etc/apt/sources.list.d/debian.sources
fi

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