#!/bin/bash
# Filename:      ${GRML_FAI_CONFIG}/scripts/GRMLBASE/03-get-sources
# Purpose:       download sources of Debian 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 or any later version.
################################################################################

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

if ifclass SOURCES ; then
  echo "Class SOURCES set, retrieving source packages."
else
  echo "Class SOURCES not set, nothing to do."
  exit 0
fi

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

set -u

ERRORS_LOG=$(mktemp)

bailout() {
  rm -f "$ERRORS_LOG"
}

$ROOTCMD apt-get update

apt_debug_option=""
if [ -n "${GRML_LIVE_DEBUG_APT:-}" ]; then
    apt_debug_option="-oDebug::Acquire::http=true"
fi

# Collect *source* package names
# shellcheck disable=SC2016 # Embedded $ is correct.
$ROOTCMD dpkg-query -W -f='${Source} ${Package}\n' | sed -e 's/^ //' | awk '{ print $1 }' | sort -u | \
  $ROOTCMD /bin/bash -c "cd \"${GRML_LIVE_SOURCESDIR}\" && xargs --max-args=32 --max-procs=12 apt-get $apt_debug_option --download-only source" 2> "${ERRORS_LOG}"

if grep -q '^E:' "${ERRORS_LOG}" ; then
  echo "Errors noticed while retrieving sources:" >&2
  cat "${ERRORS_LOG}" >&2
  bailout
  exit 1
elif grep -q '^W:' "${ERRORS_LOG}" ; then
  echo "Warnings noticed while retrieving sources (not failing the build though):"
  cat "${ERRORS_LOG}"
elif grep -q '.' "${ERRORS_LOG}" ; then
  echo "Unclassified problems noticed while retrieving sources:" >&2
  cat "${ERRORS_LOG}" >&2
  bailout
  exit 1
fi


bailout

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