#!/bin/sh

# run autopkgtest in schroot
# input: $1 (=PACKAGE), PG_SUPPORTED_VERSIONS (optional)

set -eu

PACKAGE="$1"
CHROOT="chroot:${distribution:=sid}-pgdg-${architecture:=amd64}-sbuild"

case $PACKAGE in
  pgwatch)
    case $distribution/$architecture in
      noble/arm64|noble/ppc64el) echo "Skipping $PACKAGE on $distribution/$architecture (package copied from plucky which is amd64-only)"; exit ;;
    esac ;;
esac

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

if [ "$PG_SUPPORTED_VERSIONS" = "$PG_BETA_VERSION" ] || [ "$PG_SUPPORTED_VERSIONS" = "$PG_DEVEL_VERSION" ]; then
  is_beta_devel="beta_devel"
  need_component="$PG_SUPPORTED_VERSIONS"
fi
if [ "$PACKAGE" = "postgresql-8.2" ]; then
  PG_SUPPORTED_VERSIONS="8.2"
  need_component="$PG_SUPPORTED_VERSIONS"
fi

cd /
set -x

${wrap-newpid-netns} \
schroot -c $CHROOT -u root sh <<-EOF
	set -eu
	
	# make PG devel available if requested
	if [ "${need_component:-}" ]; then
	  /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -h$PGDG_MIRROR -st -v${need_component:-} $distribution
	fi
	case $PACKAGE in
	  postgresql-8.2) # needs libecpg5 from 8.2 component
	    /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -h$PGDG_MIRROR -st -c 8.2 $distribution ;;
	  postgresql-${PG_BETA_VERSION:-})
	    /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -h$PGDG_MIRROR -st -v${PG_BETA_VERSION:-} $distribution ;;
	  postgresql-${PG_DEVEL_VERSION:-})
	    /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -h$PGDG_MIRROR -st -v${PG_DEVEL_VERSION:-} $distribution ;;
	esac

	if [ "${HAS_BACKPORTS:-}" ] && [ "${BACKPORTS:-false}" != "false" ]; then
	  echo "${mirror_backports:-}" > /etc/apt/sources.list.d/backports.list
	  echo "Package: *" > /etc/apt/preferences.d/backports.pref
	  echo "Pin: release a=$distribution-backports" >> /etc/apt/preferences.d/backports.pref
	  echo "Pin-Priority: 500" >> /etc/apt/preferences.d/backports.pref
	fi
	
	# tell debconf and ucf not to ask any questions
	export DEBIAN_FRONTEND=noninteractive

	( set -x
	  apt-get -y update || { sleep 60; apt-get -y update; }
	  apt-get -y dist-upgrade
	)
	
	# when testing beta/devel, manually install packages since the source
	# package doesn't list the binaries
	if [ "${is_beta_devel:-}" ]; then
	  IS_EXTENSION_PACKAGE=\$(apt-cache showsrc $PACKAGE | grep '^Binary:' | grep -E 'postgresql-[1-9]' || :)

	  # find all binaries built from this source matching supported versions
	  BINARIES=\$(cat /var/lib/apt/lists/*_Packages |
	    grep-dctrl --eregex -S "^$PACKAGE( .*)?$" --no-field-names -s Package |
	    grep -F $PG_SUPPORTED_VERSIONS | sort -u)
	
	  # skip tests if this is an extension package, and packages for the beta version have not been built yet
	  if [ "\$IS_EXTENSION_PACKAGE" ] && [ -z "\$BINARIES" ]; then
	    echo "###" "NOT BUILT" "###" "No version $PG_SUPPORTED_VERSIONS packages for $PACKAGE found, exiting"
	    exit 0
	  fi
	
	  # extra test dependencies (normally handled by debian/tests/control.in)
	  case $PACKAGE in
	    mimeo|pg-gvm|pg-partman) BINARIES="\$BINARIES postgresql-$PG_SUPPORTED_VERSIONS-pgtap" ;;
	    pgfaceting) BINARIES="\$BINARIES postgresql-$PG_SUPPORTED_VERSIONS-roaringbitmap" ;;
	    pgloader) BINARIES="\$BINARIES postgresql-$PG_SUPPORTED_VERSIONS-ip4r" ;;
	    postgresql-common) BINARIES="\$BINARIES postgresql-plpython3-$PG_SUPPORTED_VERSIONS postgresql-plperl-$PG_SUPPORTED_VERSIONS postgresql-pltcl-$PG_SUPPORTED_VERSIONS postgresql-$PG_SUPPORTED_VERSIONS-jit" ;;
	  esac
	
	  # prefer our packages over the base distro ones
	  ( set -x;
	    apt-get install -t $distribution-pgdg-testing -y \
	    postgresql-$PG_SUPPORTED_VERSIONS postgresql-server-dev-$PG_SUPPORTED_VERSIONS \
	    \$BINARIES
	  )
	fi
	
	# restrict tests to major version contained in package (chroots have postgresql-all installed)
	case $PACKAGE in
	  postgresql-??|postgresql-?.?) export PG_VERSIONS=${PACKAGE#*-} ;;
	esac
	
	# run autopkgtest
	( set -x;
	  apt-cache showsrc "$PACKAGE"
	  autopkgtest --user "$USER" --timeout-copy=900 "$PACKAGE" -- null
	) || EXIT=\$?
	echo "autopkgtest exit status is \${EXIT:=0}"
	case \$EXIT in
	  2) exit 0 ;; # at least one test was skipped (or at least one flaky test failed)
	  8) echo "###" "NOT BUILT" "###"; exit 0 ;; # no tests in this package, or all non-superficial tests were skipped
	  *) exit \$EXIT ;;
	esac
EOF

