#!/bin/bash
set -ex

cd "$(dirname "$0")"

# requires toml package (apt-get install python3-toml)
CLI_VERSION="$(python -c 'import toml; fh = open("Cargo.toml"); data = toml.load(fh); print(data["package"]["version"])')"
export CLI_VERSION

build_and_upload() {
  echo "Building version $CLI_VERSION ($1)"

  cargo build --release --target $1
  # Upload executable
  gsutil cp ../target/${1}/release/re "gs://reinfer-public/cli/bin/${1}/${CLI_VERSION}/re"
}

case $BUILD_PLATFORM in
  "ubuntu-20.04")
    build_and_upload "x86_64-unknown-linux-musl"
    build_and_upload "x86_64-unknown-linux-gnu"

    # Build and upload deb package
    cargo deb
    gsutil cp "../target/debian/reinfer-cli_${CLI_VERSION}_amd64.deb" gs://reinfer-public/cli/debian/
    ;;
  "macos-10.15")
    build_and_upload "x86_64-apple-darwin"
    ;;
  *)
    >&2 echo "fatal: unknown BUILD_PLATFORM '$BUILD_PLATFORM'"
    exit 1
    ;;
esac
