#!/bin/bash

# expected parameters:
#   distribution (without -pgdg, default "sid")
#   architecture (default "amd64")
#   stage (testing or production, default "testing")
# expected directories: chroots in /home/chroot
# needs dose3 >= 4

set -eu

error () {
  echo "Error: $@" >&2
  exit 2
}

fold () {
  local r
  for i in "$@"; do
    r="${r:+$r,}$i"
  done
  echo "${r:-}"
}

# read pgapt config
for dir in . .. $HOME/apt.postgresql.org; do
  test -f $dir/pgapt.conf || continue
  . $dir/pgapt.conf
  break
done
[ "$PG_MAIN_VERSION" ]

: ${distribution:=sid} ${architecture:=amd64} ${stage:=testing}
set_dist_vars $distribution

LISTSDIR="/home/chroot/$distribution-$architecture/var/lib/apt/lists"
[ -d "$LISTSDIR" ] || error "$LISTSDIR not found"
DISTSDIR="$REPO_HOST$REPO_URL_PATH/dists"

case $stage in
	production) DIST="$distribution$REPO_DIST_SUFFIX" ;;
	testing)    DIST="$distribution$REPO_DIST_SUFFIX-testing" ;;
	*) error "Bad stage $stage" ;;
esac

# download apt.pg.o packages files
wget --no-verbose --mirror $(for component in $COMPONENTS; do echo http://$DISTSDIR/$DIST/$component/binary-$architecture/Packages.bz2; done)
bunzip2 -fk $(for component in $COMPONENTS; do echo $DISTSDIR/$DIST/$component/binary-$architecture/Packages.bz2; done)
echo

# list of all packages
MAINPKG=$(awk '/^Package:/ { print $2 }' $DISTSDIR/$DIST/main/binary-${architecture}/Packages)

# exclude packages not installable on some architectures
case $architecture in
  i386|s390x)
    MAINPKG=$(echo "$MAINPKG" | grep -v "pgfaceting") # pg-roaringbitmap missing on s390x
    ;;
esac

# run debcheck
DEBCHECK="dose-debcheck -v -f -e"

# include all Packages files for "distribution" (main, universe, security) and "distribution-updates" (main, universe)
for FILE in $LISTSDIR/*_dists_${distribution}{,-updates}_*_binary-${architecture}_Packages; do
  [ -f "$FILE" ] || continue
  DEBCHECK="$DEBCHECK --bg $FILE"
done

# beta
if [ "${PG_BETA_VERSION:-}" ] && echo "$COMPONENTS" | grep -qw "$PG_BETA_VERSION"; then
  betapkg=$(fold $(echo "$MAINPKG" | grep -F "$PG_BETA_VERSION"))

  if [ "$betapkg" ]; then
    echo "### Running debcheck: main, packages matching '$PG_BETA_VERSION' (beta)"
    ( set -x; $DEBCHECK \
      --fg $DISTSDIR/$DIST/$PG_BETA_VERSION/binary-${architecture}/Packages \
      --fg $DISTSDIR/$DIST/main/binary-${architecture}/Packages \
      --checkonly $betapkg ) || EXIT=$?
    echo
  fi

  # exclude packages from further testing
  MAINPKG=$(echo "$MAINPKG" | grep -Fv "$PG_BETA_VERSION")
fi

# devel
if [ "${PG_DEVEL_VERSION:-}" ] && echo "$COMPONENTS" | grep -qw "$PG_DEVEL_VERSION"; then
  # production doesn't have PG-devel, test modules against -testing
  if [ "$stage" = "production" ]; then
    BGDIST="$DIST-testing"
  else
    BGDIST="$DIST"
  fi
  develpkg=$(fold $(echo "$MAINPKG" | grep -F "$PG_DEVEL_VERSION"))

  if [ "$develpkg" ]; then
    echo "### Running debcheck: main, packages matching '$PG_DEVEL_VERSION' (devel)"
    ( set -x; $DEBCHECK \
      --fg $DISTSDIR/$BGDIST/$PG_DEVEL_VERSION/binary-${architecture}/Packages \
      --fg $DISTSDIR/$DIST/main/binary-${architecture}/Packages \
      --checkonly $develpkg ) || EXIT=$?
    echo
  fi

  # exclude packages from further testing
  MAINPKG=$(echo "$MAINPKG" | grep -Fv "$PG_DEVEL_VERSION")
fi

# ignore elephant-shed in bookworm and lunar (and older)
MAINPKG=$(echo "$MAINPKG" | grep -Ev "^elephant-shed")

case $distribution in
  buster)
    MAINPKG=$(echo "$MAINPKG" | grep -Ev "^omnidb") # needs backports, but buster-backports got removed
    ;;
esac

# bpo
if [ "$HAS_BACKPORTS" ]; then
    echo "### Running debcheck: main, packages needing backports"
    # pgdg-buildenv: depends on clang-6.0 in stretch (but we include amd64/i386 only in our repository, not ppc64el)
    # omnidb: Needs Django 2.2 on buster
    # e-s-tmate needs tmate 2.4 on buster
    pkg=$(fold $(echo "$MAINPKG" | grep -Ee 'pgdg-buildenv' -e 'resource-agents-paf' -e 'postgresql-.*-pllua' -e 'omnidb-common' -e 'omnidb-server'))
    if [ "$pkg" ]; then
      ( set -x; $DEBCHECK \
        --bg $LISTSDIR/../backports/*_dists_${distribution}-backports_main_binary-${architecture}_Packages \
        --fg $DISTSDIR/$DIST/main/binary-${architecture}/Packages \
        --checkonly $pkg ) || EXIT=$?
    fi
    echo

    # exclude packages from further testing
    MAINPKG=$(echo "$MAINPKG" | grep -Ev -e 'pgdg-buildenv' -e 'resource-agents-paf' -e 'postgresql-.*-pllua' -e 'omnidb-common' -e 'omnidb-server')
fi

# main
echo "### Running debcheck: main"
pkg=$(fold $MAINPKG)
( set -x; $DEBCHECK \
  --fg $DISTSDIR/$DIST/main/binary-${architecture}/Packages \
  --checkonly $pkg ) || EXIT=$?
echo

# other components
PKG=""
for component in $COMPONENTS; do
  [ "$component" = "main" ] && continue
  P="$DISTSDIR/$DIST/$component/binary-${architecture}/Packages"
  PKG="$PKG $P"
done
if [ "${PKG:-}" ]; then
  echo "### Running debcheck: other components"
  ( set -x; $DEBCHECK \
    --bg $DISTSDIR/$DIST/main/binary-${architecture}/Packages \
    --fg $PKG ) || EXIT=$?
fi

exit ${EXIT:-0}
