#!/usr/bin/env bash

set -euo pipefail

if [ -z "${BASH_VERSION:-}" ]; then
  echo "getagentflow.sh must be run with bash" >&2
  exit 1
fi

BASE_URL="${AGENTFLOW_INSTALLER_BASE_URL:-http://fw.koolcenter.com/binary/geili/agentflow/installer}"
TMP_DIR=""
KEEP_TMP=0

log() {
  printf '[agentflow] %s\n' "$1"
}

fail() {
  printf '[agentflow] %s\n' "$1" >&2
  exit 1
}

recover_working_directory() {
  if pwd >/dev/null 2>&1; then
    return 0
  fi
  cd "${HOME:-/}" 2>/dev/null || cd /
}

cleanup() {
  local exit_code=$?
  if [ "$exit_code" -ne 0 ]; then
    KEEP_TMP=1
  fi
  if [ "${KEEP_TMP:-0}" -eq 0 ] && [ -n "${TMP_DIR:-}" ]; then
    rm -rf "$TMP_DIR"
  fi
}

trap cleanup EXIT
recover_working_directory

download_to_file() {
  local url=$1
  local destination=$2
  local show_progress=0

  if [ -t 2 ]; then
    show_progress=1
  fi

  if command -v curl >/dev/null 2>&1; then
    if [ "$show_progress" -eq 1 ]; then
      curl \
        --fail \
        --location \
        --retry 3 \
        --retry-delay 2 \
        --connect-timeout 30 \
        --max-time 300 \
        --progress-bar \
        --show-error \
        "$url" \
        -o "$destination"
    else
      curl \
        --fail \
        --location \
        --retry 3 \
        --retry-delay 2 \
        --connect-timeout 30 \
        --max-time 300 \
        --silent \
        --show-error \
        "$url" \
        -o "$destination"
    fi
    return
  fi

  if command -v wget >/dev/null 2>&1; then
    if [ "$show_progress" -eq 1 ]; then
      wget \
        --tries=3 \
        --timeout=30 \
        --show-progress \
        --progress=bar:force:noscroll \
        -O "$destination" \
        "$url"
    else
      wget --tries=3 --timeout=30 -qO "$destination" "$url"
    fi
    return
  fi

  fail 'curl or wget is required'
}

has_prompt_terminal() {
  if [ "${AGENTFLOW_PROMPT_USE_STDIN:-0}" = "1" ]; then
    return 0
  fi
  if [ -t 1 ] || [ -t 2 ]; then
    return 0
  fi
  if { exec 9<>/dev/tty; } 2>/dev/null; then
    exec 9>&-
    exec 9<&-
    return 0
  fi
  return 1
}

detect_platform_script() {
  local os_name
  os_name="${AGENTFLOW_BOOTSTRAP_OS:-$(uname -s)}"
  case "$os_name" in
    Linux) printf 'agentflow-linux.sh\n' ;;
    Darwin) printf 'agentflow-macos.sh\n' ;;
    *) fail "unsupported platform: $os_name" ;;
  esac
}

main() {
  local script_name script_url script_path
  script_name=$(detect_platform_script)
  script_url="${BASE_URL%/}/$script_name"

  TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/agentflow-bootstrap.XXXXXX")
  script_path="$TMP_DIR/$script_name"

  log "Downloading $script_url"
  download_to_file "$script_url" "$script_path"
  chmod +x "$script_path"

  if [ "$#" -eq 0 ]; then
    if has_prompt_terminal; then
      set -- menu
    else
      set -- install
    fi
  fi

  if [ -t 0 ] || [ "${1:-}" = 'menu' ] || [ "${AGENTFLOW_PROMPT_USE_STDIN:-0}" = "1" ]; then
    bash "$script_path" "$@"
    return
  fi

  if has_prompt_terminal; then
    bash "$script_path" "$@" </dev/tty
    return
  fi

  bash "$script_path" "$@" </dev/null
}

main "$@"
