#!/usr/bin/env bash

set -euo pipefail

# Directory where outputs will show up.
output_directory=$(mktemp --directory)
# Directory in the container to put outputs.
container_output_directory=/out
diamond_version=4.0.515
image=ubuntu:14.04
deb_name=diamond_${diamond_version}_all.deb

# While not an output, it is convenient to put the build script in the shared
# output directory.
cat <<SCRIPT > "$output_directory"/make.sh
#!/usr/bin/env bash

# Based on: https://github.com/python-diamond/Diamond/blob/f2bece462577a7c557be8a9f90f6b9340c3db571/docs/Getting-Started/Installation.md

set -euo pipefail

cd "\$(mktemp --directory)"
sudo apt-get update
sudo apt-get install --assume-yes make pbuilder python-mock python-configobj python-support cdbs devscripts build-essential git

git clone https://github.com/python-diamond/Diamond
cd Diamond
git checkout "v${diamond_version}"

make builddeb

cp build/"$deb_name" "$container_output_directory"
SCRIPT

# Mount the output directory so the build script is accessible to the container
# and the container can copy the built deb back to the host.
docker run \
  --mount type=bind,source="$output_directory",target="$container_output_directory" \
  "$image" \
  bash "$container_output_directory"/make.sh

cat <<RESULTS
Deb created: $output_directory/$deb_name

Run \`scripts/build/upload-to-third-party.sh $output_directory/$deb_name\`
to upload it for use.
RESULTS
