#!/bin/bash

set -e

cd $(dirname $0)
ROOT=$(pwd)/..
OUTPUT_ROOT=$ROOT/.bazel/out/build
VENV=$OUTPUT_ROOT/venv-$(uname -m)
BAZEL_EXTERNAL=$(bazel info output_base --ui_event_filters=-INFO)/external

echo "--- Building wheels for $(uname -m)"
(cd $ROOT && ./tools/build)

echo "--- Setup venv"
test -d $VENV || (
   mkdir -p $OUTPUT_ROOT
   (cd $ROOT && ./tools/python -m venv $VENV)
)

echo "--- Fetch extra deps"
bazel query @pyoxidizer//:*
if [[ "$OSTYPE" == "darwin"* ]]; then
  if [ $(uname -m) != "arm64" ]; then
    bazel query @audio_mac_amd64//:* > /dev/null
    bazel query @pyqt6.3_mac_bundle_amd64//:* > /dev/null
    bazel query @pyqt5.14_mac_bundle_amd64//:* > /dev/null
  else
    bazel query @audio_mac_arm64//:* > /dev/null
    bazel query @pyqt6.3_mac_bundle_arm64//:* > /dev/null
    bazel query @protobuf_wheel_mac_arm64//:* > /dev/null

  fi
else
  bazel query @pyqt515//:* > /dev/null
  bazel query @bundle_extras_linux_amd64//:* > /dev/null
fi

. $ROOT/tools/cargo-env
(cd $ROOT/qt/bundle && $VENV/bin/python build.py $ROOT $BAZEL_EXTERNAL)
