#!/bin/bash

# This script will prepare a directory containg a source package for the Android component
# that ships google's binaries
#
# ./debian/scripts/make_zip_package.sh ndk,25.1.8937393 build-tools,33.0.0 platforms,android-33~33 platform-tools
#

set -e

SCRIPT_PATH=$(dirname $0)
CWD="$(realpath "$SCRIPT_PATH")"

LOCAL_ZIP_DIR="$(realpath ../google-android-installers-zips)"

TMPDIR=$(mktemp -d /tmp/gai-zip-XXX) || exit
trap "echo removing $TMPDIR && rm -rf -- '$TMPDIR'" EXIT

PKGDIR="$TMPDIR/$(dpkg-parsechangelog -S Source)"

mkdir -p "$PKGDIR"

cp -a . "$PKGDIR"
rm -fr "$PKGDIR"/.git

pushd "$PKGDIR"
sed -i "/XS-Autobuild/a XS-With-Zips: yes" debian/control.in
debian/scripts/zip_file_add.sh "${LOCAL_ZIP_DIR}" "$@"
if [ "a$SKIP_SIGN" = "a1" ]; then
  dpkg-buildpackage -S --no-sign
else
  dpkg-buildpackage -S
fi
popd

dcmd mv -v "$TMPDIR"/*_source.changes $(realpath $CWD/../../..)

rm -fr "$PKGDIR"
trap - EXIT
