#!/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/jenkins/jenkins/workspace/apt.postgresql.org /home/buildd/workspace/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
	  sed -i -e "s/main/main \$PG_SUPPORTED_VERSIONS/" /etc/apt/sources.list.d/pgdg.list
	fi
	case $PACKAGE in
	  postgresql-${PG_BETA_VERSION:-})
	    sed -i -e "s/main/main ${PG_BETA_VERSION:-}/" /etc/apt/sources.list.d/pgdg.list ;;
	  postgresql-${PG_DEVEL_VERSION:-})
	    sed -i -e "s/main/main ${PG_DEVEL_VERSION:-}/" /etc/apt/sources.list.d/pgdg.list ;;
	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
	
	( set -x
	  apt-get -y update || { sleep 60; apt-get -y update; }
	  apt-get -y -o DPkg::Options::=--force-confnew dist-upgrade
	)
	
	# when testing beta/devel, manually install packages since the source
	# package doesn't list the binaries
	if [ "\$PG_SUPPORTED_VERSIONS" != "pgdg" ]; then
	  # 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 |
	    fgrep \$FILTER | sort -u)
	
	  # skip PG devel tests if packages have not been built yet
	  if [ -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" ;;
	  esac
	
	  # prefer our packages over the base distro ones
	  ( set -x;
	    apt-get install -t $distribution-pgdg-testing -y -o DPkg::Options::=--force-confnew \
	    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
	if [ -x /usr/bin/autopkgtest ]; then
	  ( set -x; autopkgtest "$PACKAGE" -- null ) || EXIT=\$?
	else
	  ( set -x; adt-run --apt-source "$PACKAGE" --- null ) || EXIT=\$?
	fi
	case \${EXIT:-0} in
	  8) echo "###" "NOT BUILT" "###"; exit 0 ;; # no tests in this package, or all non-superficial tests were skipped
	  *) exit \${EXIT:-0} ;;
	esac
EOF

