#!/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
}

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

: ${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"
BPODIR="/home/chroot/$distribution-$architecture/var/lib/apt/backports"
DISTSDIR="$REPO_HOST$REPO_URL_PATH/dists"

trap 'rm -f ${BPOSRC:-} ${TMP:-}' EXIT

remove_packages ()
{
  FILE="$1"
  shift
  for pkg in "$@"; do
    TMP="$FILE.$$"
    grep-dctrl --not -S --eregex "$pkg" $FILE > $TMP
    mv $TMP $FILE
  done
}

# 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
  PKG="${PKG:-} $FILE"
done

# include pgdg 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 http://$DISTSDIR/$DIST/$component/source/Sources.bz2; done)
bunzip2 -fk $(for component in $COMPONENTS; do echo $DISTSDIR/$DIST/$component/binary-$architecture/Packages.bz2 $DISTSDIR/$DIST/$component/source/Sources.bz2; done)
echo

# packages not depending on backports
PKG="$PKG $DISTSDIR/$DIST/main/binary-${architecture}/Packages"
MAINSRC=$DISTSDIR/$DIST/main/source/Sources

if [ "$HAS_BACKPORTS" ]; then
    [ -f $PGAPTDIR/jenkins/packages.backports ] || error "$PGAPTDIR/jenkins/packages.backports not found"
    remove_packages $MAINSRC $(cat $PGAPTDIR/jenkins/packages.backports*)

    # packages depending on backports
    BPOSRC=$(mktemp ${DIST}_bpo_source_Sources.XXXXXX)
    for pkg in $(cat $PGAPTDIR/jenkins/packages.backports*); do
      grep-dctrl -S $pkg < $MAINSRC >> $BPOSRC || :
    done

    # include backports packages file
    [ -d "$BPODIR" ] || error "$BPODIR not found"
    for FILE in $BPODIR/*_dists_${distribution}-backports_main_binary-${architecture}_Packages ; do
      [ -f "$FILE" ] || error "$FILE not found"
      BPO="$FILE"
    done
fi

# packages not tested on specific architectures
case $architecture in
  i386)
    remove_packages $MAINSRC h3-pg pgfaceting pg-roaringbitmap "postgresql-.*-age" timescaledb
    ;;
  ppc64el)
    remove_packages $MAINSRC buildapp cffi cl-unicode pgloader sbcl plv8
    ;;
  s390x)
    remove_packages $MAINSRC buildapp cffi cl-unicode pgloader sbcl plv8 pgfaceting pg-roaringbitmap timescaledb
    ;;
esac

# packages not tested on certain dists
# python-werkzeug needs DH 12 and python3-sphinx-issues
case $distribution in
  buster|stretch|eoan|disco|bionic|xenial)
    remove_packages $MAINSRC python-werkzeug ;;
esac
# vip-manager needs newer Go version to compile, but .debs are copied to older dists
case $distribution in
  buster|groovy|focal)
    remove_packages $MAINSRC vip-manager ;;
esac
# pg-ldap-sync prometheus-sql-exporter need buster-backports which got removed
case $distribution in buster) remove_packages $MAINSRC pg-ldap-sync prometheus-sql-exporter ;; esac
# zstd is too old on stretch, buster and bionic
case $distribution in stretch|buster|bionic) PROFILES="pkg.postgresql.nozstd" ;; esac

# run builddebcheck
BUILDDEBCHECK="dose-builddebcheck -v -f -e --deb-native-arch=$architecture ${PROFILES:+--deb-profiles=$PROFILES}"

echo "### Running builddebcheck: main, packages not needing backports"
( set -x; $BUILDDEBCHECK $PKG $MAINSRC ) || EXIT=1
echo

if test -s "${BPO:-}"; then
  echo "### Running builddebcheck: main, packages needing backports"
  grep '^Package:' $BPOSRC || :
  ( set -x; $BUILDDEBCHECK $PKG $BPO $BPOSRC ) || EXIT=1
  echo
fi

for component in $COMPONENTS; do
  [ "$component" = "main" ] && continue
  sources="$DISTSDIR/$DIST/$component/source/Sources"
  [ -s $sources ] || continue # skip empty file
  echo "### Running builddebcheck: $sources"
  grep '^Package:' $sources
  ( set -x; $BUILDDEBCHECK $PKG $sources ) || EXIT=1
  echo
done

exit ${EXIT:-0}
