#!/usr/bin/env bash
#
# Cross-platform helper (macOS, Linux, Git Bash on Windows): checks Docker Compose v2,
# resolves the flowos-solution checkout, then runs install-local-from-registry.sh.
#
# Canonical copy: flowos-app/public/install/setup.sh (sync via scripts/registry/sync-setup-wrappers-to-public.sh)
#
# Environment:
#   FLOWOS_SOLUTION_ROOT   Path to flowos-solution (contains scripts/registry/...).
#   FLOWOS_SOLUTION_GIT_URL If set and ROOT not found, clone into FLOWOS_CLONE_PARENT/FLOWOS_CLONE_DIR_NAME.
#   FLOWOS_CLONE_PARENT     Default: $HOME
#   FLOWOS_CLONE_DIR_NAME   Default: flowos-solution
#
# Pass-through: any args go to install-local-from-registry.sh (e.g. --core-only, --with-modules).
#
set -euo pipefail

die() {
  printf '%s\n' "ERROR: $*" >&2
  exit 1
}

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

require_docker() {
  command -v docker >/dev/null 2>&1 || die "Docker is not installed. Install Docker Desktop (Mac/Windows) or Docker Engine (Linux)."
  docker compose version >/dev/null 2>&1 || die "Docker Compose v2 is required. Check: docker compose version"
}

auto_detect_root() {
  local candidate
  candidate="$(cd "$HERE/../../.." && pwd 2>/dev/null)" || return 0
  if [[ -f "$candidate/scripts/registry/install-local-from-registry.sh" ]]; then
    printf '%s' "$candidate"
  fi
}

resolve_root() {
  if [[ -n "${FLOWOS_SOLUTION_ROOT:-}" ]]; then
    [[ -f "$FLOWOS_SOLUTION_ROOT/scripts/registry/install-local-from-registry.sh" ]] ||
      die "FLOWOS_SOLUTION_ROOT does not look like flowos-solution (missing scripts/registry/install-local-from-registry.sh): $FLOWOS_SOLUTION_ROOT"
    printf '%s' "$FLOWOS_SOLUTION_ROOT"
    return
  fi

  local detected
  detected="$(auto_detect_root)"
  if [[ -n "$detected" ]]; then
    printf '%s' "$detected"
    return
  fi

  if [[ -n "${FLOWOS_SOLUTION_GIT_URL:-}" ]]; then
    command -v git >/dev/null 2>&1 || die "git is required when FLOWOS_SOLUTION_GIT_URL is set."
    local parent name target
    parent="${FLOWOS_CLONE_PARENT:-$HOME}"
    name="${FLOWOS_CLONE_DIR_NAME:-flowos-solution}"
    target="$parent/$name"
    if [[ ! -f "$target/scripts/registry/install-local-from-registry.sh" ]]; then
      if [[ -d "$target/.git" ]]; then
        die "Directory exists but is not a valid flowos-solution checkout: $target"
      fi
      printf '%s\n' "Cloning FlowOS into $target ..."
      mkdir -p "$parent"
      git clone "$FLOWOS_SOLUTION_GIT_URL" "$target"
    fi
    [[ -f "$target/scripts/registry/install-local-from-registry.sh" ]] ||
      die "Clone finished but installer script is missing under: $target"
    printf '%s' "$target"
    return
  fi

  die "Set FLOWOS_SOLUTION_ROOT to your flowos-solution directory, clone this repo first, or set FLOWOS_SOLUTION_GIT_URL to clone automatically. Docs: flowos-docs/deployment/INSTALL-REGISTRY-AND-LOCAL-DEVELOPMENT.md"
}

require_docker
ROOT_RESOLVED="$(resolve_root)"
exec bash "$ROOT_RESOLVED/scripts/registry/install-local-from-registry.sh" "$@"
