#!/usr/bin/env bash

set -eux

PATCH_FILE="other/bootstrap_daemon/docker/release.patch"
trap 'rm -f "$PATCH_FILE"' EXIT

RELEASE=0
while [[ $# -gt 0 ]]; do
  case $1 in
    --release)
      RELEASE=1
      shift
      ;;
    *)
      echo "Unknown option $1"
      exit 1
      ;;
  esac
done

if [[ $RELEASE -eq 1 ]]; then
  git fetch upstream
  RELEASE_BRANCH=$(git branch -r | grep "upstream/release/" | sort -V | tail -n1 | sed 's/^[[:space:]]*//' || true)
  if [[ -n "$RELEASE_BRANCH" ]]; then
    echo "Generating release patch from $RELEASE_BRANCH"
    git diff "HEAD..$RELEASE_BRANCH" -- toxcore/tox.h CMakeLists.txt >"$PATCH_FILE"
  else
    echo "No release branch found in upstream"
  fi
fi

docker_build() {
  DOCKER_BUILDKIT=1 docker build --progress=plain -f other/bootstrap_daemon/docker/Dockerfile -t toxchat/bootstrap-node .
}

# Run Docker build once. If it succeeds, we're good.
if docker_build; then
  exit 0
fi

# We're not good. Run it again, but now capture the output.
OUTPUT=$(docker_build 2>&1 || true)

if echo "$OUTPUT" | grep '/usr/local/bin/tox-bootstrapd: FAILED'; then
  # This is a checksum warning, so we need to update it.
  echo "$OUTPUT" | grep -Eo '[0-9a-f]{64}  /usr/local/bin/tox-bootstrapd' | tail -n1 >other/bootstrap_daemon/docker/tox-bootstrapd.sha256
fi

# Run once last time to complete the build.
docker_build
