#!/bin/sh

# Retrieve last git hash built, and rebuild the source package with it.
# Useful when new distributions have been added and Jenkins doesn't have the
# source packages for them yet.

# for pkg in $(cat packages); do ./trigger-source-rebuild "$pkg"; done

set -eu

while getopts "d:r:" opt ; do
  case $opt in
    d) distribution="$OPTARG" ;;
    r) revision="$OPTARG" ;;
    *) exit 5 ;;
  esac
done
# shift away args
shift $(($OPTIND - 1))

PACKAGE="$1"
XML=$(curl -fs https://pgdgbuild.dus.dg-i.net/view/Sources/job/$PACKAGE-source/lastSuccessfulBuild/api/xml?xpath=//lastBuiltRevision/SHA1)
SHA1=$(echo "$XML" | grep -Eo '[0-9a-f]{40}')
test "$SHA1"

set -x
./trigger-job ${distribution:+-d $distribution} -r "${revision:-1}" "$PACKAGE-source" "branch=$SHA1"
