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

# Simple runner: builds binary if missing then runs it.
# Environment variables supported:
#  - BIN (path to binary, default ./safelineApi)
#  - CONFIG (path to config.json, default ./config.json)
#  - SAFELINE_API_TOKEN, DNS_PROVIDER, CONTACT_EMAIL (optional flags passed to binary)

BIN=${BIN:-./safelineApi}
CONFIG=${CONFIG:-./config.json}

if [ ! -f "$BIN" ]; then
  echo "Binary not found, building..."
  GOOS=${GOOS:-linux} GOARCH=${GOARCH:-amd64} go build -o "$BIN" ./cmd/safelineApi
fi

ARGS=()
if [ -n "${SAFELINE_API_TOKEN:-}" ]; then
  ARGS+=("-t" "$SAFELINE_API_TOKEN")
fi
if [ -n "${DNS_PROVIDER:-}" ]; then
  ARGS+=("-D" "$DNS_PROVIDER")
fi
if [ -n "${CONTACT_EMAIL:-}" ]; then
  ARGS+=("-e" "$CONTACT_EMAIL")
fi

echo "Starting SafelineAPI ($BIN) with config: $CONFIG"
"$BIN" "${ARGS[@]}"
