#!/bin/sh

# Launch sbuild from jenkins
# Input:
#  .dsc source package in current directory
#  JOB_NAME=$pkg-binaries or PACKAGE=pkg
#  architecture=amd64/i386
#  distribution=foo (caveat: sbuild -d foo-pgdg will be called)
#  optionally: DEB_BUILD_OPTIONS
#  optionally: DEB_PG_SUPPORTED_VERSIONS (defaults to pgdg)
#  optionally: autopkgtest=skip
#  optionally: BACKPORTS=true
#  optionally: wrap= (disables newpid-netns)
#  optionally: binnmu_reason=text binnmu_version=version
# Output:
#  .changes and .deb in current directory
#  autopkgtest.xml in current directory
# Requirements:
#  schroot $foo-pgdg-$arch-sbuild
#  gid sbuild (for sbuild as such and for /var/lock/sbuild-package)
#  adt-sbuild available in chroot (in pgdg-buildenv package)
#  /var/tmp bind mount in schroot fstab for retrieving adt-run summary
#  sbuild needs to pass through DEB_* variables (default)
#  "deb" resolves to some debian mirror
#  newpid (and newpid-netns) is installed on host and netns-setup was run

set -eu

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

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

# 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

# find dsc to build
if [ -z "${PACKAGE:-}" ]; then
  PACKAGE="${JOB_NAME%%-binaries*}"
fi
set -- ${PACKAGE}_*.dsc
DSC="$1"
[ "${2:-}" ] && error "There is more than one \${PACKAGE}_*.dsc in $PWD"

# exit early if package is arch:all only and we are not on amd64
if [ "$architecture" != "amd64" ] && egrep -q '^Architecture: all$' $DSC; then
  echo "Package is arch:all only and we are not on amd64" # needs https://issues.jenkins-ci.org/browse/JENKINS-59730 fixed first: "###" "NOT BUILT" "###"
  adtsummary2junit /dev/null > autopkgtest.xml
  exit
fi

# check if chroot exists
schroot -l | grep -q $chroot || \
  error "There is no schroot definition for $chroot"

# decide if we should build arch:all packages
case $architecture in
  amd64)
    ARCH_ALL="--arch-all" ;; # always build arch:all binaries here
  *)
    ARCH_ALL="--no-arch-all"
    if [ "${autopkgtest:-}" != "skip" ] && egrep -q '^Testsuite: .*autopkgtest' $DSC; then
      echo "Package has Testsuite: autopkgtest, will build arch:all binaries even on $architecture. We'll throw them away at the end."
      ARCH_ALL="--arch-all"
      FILTER_ARCH_ALL="true"
    fi ;;
esac

# build profiles
case $PACKAGE in
  postgresql-?.?|postgresql-??)
    case $distribution in
      # enable cassert on sid
      sid) DEB_BUILD_PROFILES="pkg.postgresql.cassert${DEB_BUILD_PROFILES:+ $DEB_BUILD_PROFILES}" ;;
    esac
    ;;
esac
export DEB_BUILD_PROFILES="pgdg $distribution${DEB_BUILD_PROFILES:+ $DEB_BUILD_PROFILES}"

# resolve alternative build-dependencies on dists other than sid
case $distribution in
	sid) ;;
	squeeze) RESOLVE_ALTERNATIVES="--resolve-alternatives" ;; # sbuild+aptitude would want multi-arch
	*) RESOLVE_ALTERNATIVES="--resolve-alternatives --build-dep-resolver=aptitude" ;;
esac

# enable backports
if [ "$HAS_BACKPORTS" ] && [ "${BACKPORTS:-}" ] && [ "$BACKPORTS" != "false" ]; then
  BPOREPO="$mirror_backports"
fi

# disable parallel builds
export DEB_BUILD_OPTIONS="parallel=1${DEB_BUILD_OPTIONS:+ $DEB_BUILD_OPTIONS}"

# set DEB_PG_SUPPORTED_VERSIONS
: ${DEB_PG_SUPPORTED_VERSIONS:=pgdg}
export DEB_PG_SUPPORTED_VERSIONS

if [ "$DEB_PG_SUPPORTED_VERSIONS" != "pgdg" ]; then
  # extra build dependencies
  ADD_DEPENDS="--add-depends=postgresql-server-dev-$DEB_PG_SUPPORTED_VERSIONS --chroot-setup-commands=sbuild-pgdg-components"
  if grep -q 'Build-Depends: .*postgresql-all' $DSC; then
    ADD_DEPENDS="$ADD_DEPENDS --add-depends=postgresql-$DEB_PG_SUPPORTED_VERSIONS"
  fi
fi

# prepare temp file for adt-run results
if [ "${autopkgtest:-}" != "skip" ]; then
  ADT_TEMP_DIR=$(mktemp -d /var/tmp/$PACKAGE.XXXXXX)
  trap "rm -rf $ADT_TEMP_DIR" EXIT
  export DEB_ADT_SUMMARY="$ADT_TEMP_DIR/adt_summary"
  FINISHED_BUILD_COMMAND='adt-sbuild %SBUILD_BUILD_DIR %SBUILD_PKGBUILD_DIR'
fi

# lock against concurrent apt-get update/upgrade operations on the source chroot
LOCKDIR="/var/lock/sbuild-package"
if ! test -d $LOCKDIR; then
  mkdir -p $LOCKDIR
  chgrp sbuild $LOCKDIR
  chmod 3775 $LOCKDIR
fi
umask 002

# build package and run autopkgtests inside the chroot
(
  echo "Building $PACKAGE (DEB_PG_SUPPORTED_VERSIONS=$DEB_PG_SUPPORTED_VERSIONS)"
  set -x
  flock --shared 9 # lock against concurrent source chroot upgrades
  ${wrap-newpid-netns} \
  sbuild --nolog --batch \
    -d $distribution-pgdg --arch $architecture ${ARCH_ALL:-} ${RESOLVE_ALTERNATIVES:-} \
    ${BPOREPO:+--extra-repository="$BPOREPO"} \
    ${binnmu_reason:+--make-binNMU="$binnmu_reason" --binNMU="$binnmu_version" -m "$MAILING_LIST"} \
    ${FINISHED_BUILD_COMMAND:+--finished-build-commands="$FINISHED_BUILD_COMMAND"} \
    --no-run-lintian \
    ${ADD_DEPENDS:-} $DSC
) 9> $LOCKDIR/$distribution-$architecture.lock

# remove arch:all packages that were only built for autopkgtests
if [ "${FILTER_ARCH_ALL:-}" ]; then
  sed -i -e '/_all\.deb$/d' *.changes
fi

# when building for PG devel, remove buildinfo so we don't overwrite the one
# from the main "pgdg" build
if [ "$DEB_PG_SUPPORTED_VERSIONS" != "pgdg" ]; then
  sed -i -e '/\.buildinfo$/d' *.changes
fi

# rename Ubuntu bionic's .ddebs to .deb
if grep -q '\.ddeb$' *.changes; then
  for ddeb in $(grep -ho '[^ ]*\.ddeb$' *.changes | sort -u); do
    mv -v $ddeb ${ddeb%.ddeb}.deb
  done
  sed -i -e 's/\.ddeb$/.deb/' *.changes
fi

# copy autopkgtest results back (already in junit format)
if [ "${DEB_ADT_SUMMARY:-}" ]; then
  cp -v $DEB_ADT_SUMMARY autopkgtest.xml
else
  adtsummary2junit /dev/null > autopkgtest.xml
fi
