#!/bin/sh
set -eu

export HWLAB_SERVICE_ID="hwlab-project-management"
export HWLAB_ARTIFACT_KIND="bun-command"
export HWLAB_SERVICE_ENTRYPOINT="cmd/hwlab-project-management/main.ts"
export HWLAB_RUNTIME_MODE="${HWLAB_RUNTIME_MODE:-env-reuse-git-mirror-checkout}"
export HWLAB_COMMIT_ID="${HWLAB_BOOT_COMMIT:?HWLAB_BOOT_COMMIT is required}"
export HWLAB_REVISION="${HWLAB_BOOT_COMMIT}"
export HWLAB_IMAGE="${HWLAB_ENVIRONMENT_IMAGE:-${HWLAB_IMAGE:-}}"
export HWLAB_IMAGE_DIGEST="${HWLAB_ENVIRONMENT_DIGEST:-${HWLAB_IMAGE_DIGEST:-unknown}}"
export PORT="${PORT:-${HWLAB_PROJECT_MANAGEMENT_PORT:-${HWLAB_PORT:-6672}}}"
export HWLAB_PORT="$PORT"
export HWLAB_PROJECT_MANAGEMENT_PORT="$PORT"
export HWLAB_PROJECT_MANAGEMENT_SOURCE_ROOT="${HWLAB_PROJECT_MANAGEMENT_SOURCE_ROOT:-$PWD}"

bun_bin="${HWLAB_BUN_COMMAND:-}"
if [ -z "$bun_bin" ]; then
  if command -v bun >/dev/null 2>&1; then
    bun_bin="$(command -v bun)"
  elif [ -x /usr/local/bin/bun ]; then
    bun_bin="/usr/local/bin/bun"
  elif [ -x ./node_modules/.bin/bun ]; then
    bun_bin="./node_modules/.bin/bun"
  else
    echo "hwlab-project-management boot failed: bun not found" >&2
    exit 127
  fi
fi

exec "$bun_bin" run cmd/hwlab-project-management/main.ts
