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

# read pgapt config
for dir in . .. $HOME/apt.postgresql.org; do
  test -f $dir/pgapt.conf || continue
  . $dir/pgapt.conf
  break
done
chroot="chroot:${distribution:=sid}$REPO_DIST_SUFFIX-${architecture:=amd64}-sbuild"
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" ] && grep -Eq '^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" ] && grep -Eq '^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
export DEB_BUILD_PROFILES="pgdg $distribution${DEB_BUILD_PROFILES:+ $DEB_BUILD_PROFILES}"
case $PACKAGE in
  postgresql-?.?|postgresql-[1-9]?|postgresql-[1-9]???) # 9.x, 15, 15ee
    case $distribution in
      # enable cassert on sid
      sid) DEB_BUILD_PROFILES="$DEB_BUILD_PROFILES pkg.postgresql.cassert" ;;
      # disable zstd on stretch, buster and bionic (PG15+)
      stretch|buster|bionic) DEB_BUILD_PROFILES="$DEB_BUILD_PROFILES pkg.postgresql.nozstd" ;;
    esac
    ;;
esac

case $PACKAGE in
  postgresql-9.[1-6])
    case $distribution in
      # Python 3.12 removed distutils
      noble) DEB_BUILD_PROFILES="$DEB_BUILD_PROFILES nopython" ;;
    esac
    ;;

  vip-manager)
    case $distribution in
      buster|jammy|focal)
        echo "Not building vip-manager on $distribution, the bullseye-pgdg package will be copied to $distribution-pgdg"
        adtsummary2junit /dev/null > autopkgtest.xml
        exit 0
        ;;
    esac
    ;;
esac

# 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

# set build options
export DEB_BUILD_OPTIONS="$PG_BUILD_OPTIONS${DEB_BUILD_OPTIONS:+ $DEB_BUILD_OPTIONS}"

# set PG_SUPPORTED_VERSIONS
: ${PG_SUPPORTED_VERSIONS:=pgdg}
export PG_SUPPORTED_VERSIONS
export PG_UPDATECONTROL=yes

for version in $PG_SUPPORTED_VERSIONS; do
  if [ "$version" != "pgdg" ]; then
    # extra build dependencies
    ADD_DEPENDS="${ADD_DEPENDS:-} --add-depends=postgresql-server-dev-$version"
    if grep -q 'Build-Depends: .*postgresql-all' $DSC; then
      ADD_DEPENDS="$ADD_DEPENDS --add-depends=postgresql-$version"
    fi
  fi
done

# prepare temp file for adt-run results
if [ "${autopkgtest:-}" != "skip" ]; then
  ADT_TEMP_DIR=$(mktemp -d /var/tmp/$PACKAGE.XXXXXX)
  chgrp sbuild "$ADT_TEMP_DIR"
  chmod 3775 "$ADT_TEMP_DIR" # allow jenkins to create files
  export DEB_ADT_SUMMARY="$ADT_TEMP_DIR/adt_summary"
  trap "rm -rf $ADT_TEMP_DIR" EXIT
  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 (PG_SUPPORTED_VERSIONS=$PG_SUPPORTED_VERSIONS)"
  set -x
  flock --shared 9 # lock against concurrent source chroot upgrades
  ${wrap-newpid-netns} \
  sbuild --nolog --batch \
    -d $distribution$REPO_DIST_SUFFIX --arch $architecture ${ARCH_ALL:-} ${RESOLVE_ALTERNATIVES:-} \
    ${BPOREPO:+--extra-repository="$BPOREPO"} \
    ${EXTRA_REPOSITORY:+--extra-repository="$EXTRA_REPOSITORY"} \
    ${EXTRA_REPOSITORY_KEY:+--extra-repository-key="$EXTRA_REPOSITORY_KEY"} \
    ${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:-} \
    ${ADD_DEPENDS:+--chroot-setup-commands="/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -h$PGDG_MIRROR -st -v'$PG_SUPPORTED_VERSIONS' $distribution"} \
    $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 [ "$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

# convert autopkgtest results to junit xml format
if [ "${DEB_ADT_SUMMARY:-}" ]; then
  if [ -s "$DEB_ADT_SUMMARY" ]; then
    adtsummary2junit $DEB_ADT_SUMMARY > autopkgtest.xml
  else
    echo "$DEB_ADT_SUMMARY was not created, something went wrong with the autopkgtest run"
    exit 1
  fi
else
  adtsummary2junit /dev/null > autopkgtest.xml
fi
