#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
IMAGE_TAG="${IMAGE_TAG:-sub2api-cn-relay-manager:local}"
BINARY_PATH="${BINARY_PATH:-$ROOT_DIR/bin/sub2api-cn-relay-manager}"

mkdir -p "$(dirname "$BINARY_PATH")"

echo "[1/2] building linux binary -> $BINARY_PATH"
(
  cd "$ROOT_DIR"
  GOTOOLCHAIN=local CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags='-s -w' -o "$BINARY_PATH" ./cmd/server
)

echo "[2/2] building OCI image -> $IMAGE_TAG"
(
  cd "$ROOT_DIR"
  docker build -f Dockerfile.local -t "$IMAGE_TAG" .
)

echo "done: $IMAGE_TAG"
