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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
ENV_FILE="${ROOT_DIR}/scripts/env/dev.env"

if [[ ! -f "${ENV_FILE}" ]]; then
  echo "Missing ${ENV_FILE}. Create it from scripts/env/dev.env.example." >&2
  exit 1
fi

set -a

source "${ENV_FILE}"
set +a

if [[ -z "${APP_VERSION:-}" ]]; then
  APP_VERSION="$(git -C "${ROOT_DIR}" describe --tags --abbrev=0 2>/dev/null || printf 'dev')"
  export APP_VERSION
fi

if [[ "${WARPBOX_DATA_DIR:-}" != /* ]]; then
  export WARPBOX_DATA_DIR="${ROOT_DIR}/${WARPBOX_DATA_DIR:-data}"
fi

cd "${ROOT_DIR}/backend"
exec go run ./cmd/warpbox
