#!/bin/bash

set -e

if [ -z "${APT_CMD}" ] ; then
  echo "ERROR: Missing mandatory environment variable '\$APT_CMD', cannot continue!" >&2
  exit 1
fi

# work around MT#16183 -> https://bugs.debian.org/776152
tmpfile=$(mktemp)
$APT_CMD update 2>&1 | tee -a "${tmpfile}"

if grep -q 'Failed to fetch' "${tmpfile}" ;  then
  echo "ERROR: failed to fetch repository files, exiting." >&2
  exit 1
fi

# workaround TT#16509
if grep -Eq '^E: |^Err ' "${tmpfile}" ;  then
  echo "ERROR: repository metadata update failed, see errors:" >&2
  grep -E '^E: |^Err ' "${tmpfile}"
  exit 1
fi

rm -f "${tmpfile}"
