#!/usr/bin/env bash

set -eux

apt-get install -y \
    liblzo2-dev \
    libbrotli-dev \
    libsodium-dev \
    build-essential \
    gcc \
    cmake \
    libc-dev

tmpdir="$(mktemp -d)"
trap 'rm -r "$tmpdir"' EXIT

export GOCACHE="$tmpdir/cache"
export GOPATH="$tmpdir/build"

src_dir="/srv/zulip-wal-g-src-$1"
dst="/srv/zulip-wal-g-$1"

cd "$src_dir"

if [ "$(git rev-parse HEAD)" != "$2" ]; then
    echo "Commit tag has changed; expected $2, got $(git rev-parse HEAD)"
    exit 1
fi

export PATH="$PATH:$GOBIN"
export USE_LZO=1
export USE_LIBSODIUM=1
make deps pg_build

mv "main/pg/wal-g" "$dst"
