#!/bin/bash
#
# Docker required!

if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: $0 <repo> <repo-revision-id>"
    exit 2
fi

platform=ubuntu1604
dockerfile=SCRIPTS/Dockerfile_$platform

mkdir -p "PACKAGES/$platform"

if [ ! -f "$dockerfile.in" ]; then
    echo "Docker file source $dockerfile.in not found (are we in the right directory?"
    exit 1
fi

set -eu

repo="$1"
repo_revision="$2"

current=$(hg id | awk '{ print $1; }')

case "$current" in
    *+) echo "WARNING: Current working copy has been modified - build will check out the last commit, which must perforce be different";;
    *);;
esac

current=${current%%+}

cat "$dockerfile.in" \
    | perl -p -e 's/\[\[REVISION\]\]/'"$current"'/' \
    | perl -p -e 's/\[\[REPO\]\]/'"$repo"'/' \
    | perl -p -e 's/\[\[REPOREVISION\]\]/'"$repo_revision"'/' \
           > "$dockerfile"

dockertag="cannam/vamp-build-and-test-$current"

sudo docker build -t "$dockertag" -f "$dockerfile" SCRIPTS

container=$(sudo docker create "$dockertag")
sudo docker cp "$container":vamp-build-and-test/PACKAGES/linux64/ "PACKAGES/$platform"/
sudo docker rm "$container"

echo "Done!"

