#!/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
  bucardo) echo "Skipping $PACKAGE tests" ; exit ;;
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

cd /
set -x

${wrap-newpid-netns} \
schroot -c $CHROOT -u root sh <<-EOF
	set -eu
	export PG_SUPPORTED_VERSIONS="${PG_SUPPORTED_VERSIONS:=pgdg}" # import from outer environment
	[ "$PACKAGE" = "postgresql-8.2" ] && PG_SUPPORTED_VERSIONS="8.2" # needs libecpg5 and friends
	
	# make PG devel available if requested
	if [ "\$PG_SUPPORTED_VERSIONS" != "pgdg" ]; then
	  /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -h$PGDG_MIRROR -st -v\$PG_SUPPORTED_VERSIONS $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 [ "\$PG_SUPPORTED_VERSIONS" != "pgdg" ]; then
	  IS_EXTENSION_PACKAGE=\$(apt-cache showsrc $PACKAGE | grep '^Binary:' | grep -E 'postgresql-[1-9]' || :)

	  # convert list of supported versions to grep filter
	  for ver in \$(/usr/share/postgresql-common/supported-versions); do
	    FILTER="\${FILTER:-} -e \$ver"
	  done
	  # 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 \$FILTER | 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-partman) BINARIES="\$BINARIES postgresql-$PG_SUPPORTED_VERSIONS-pgtap" ;;
	    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" ;;
	  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 --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

