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

BASE_URL="${LIMINAL_BASE_URL:-https://liminalshield.com}"
MODE="${LIMINAL_MODE:-human-chrome}"
STORAGE="${LIMINAL_STORAGE:-ephemeral}"
MEDIA="${LIMINAL_MEDIA:-none}"
HOST_RUNTIME="${LIMINAL_HOST_RUNTIME:-zoom-app}"
COHOST_RUNTIME="${LIMINAL_COHOST_RUNTIME:-local}"
PORT="${LIMINAL_SHIELD_PORT:-8080}"
HOST_PORT="${LIMINAL_HOST_PORT:-$PORT}"
BOT_PORT="${LIMINAL_BOT_PORT:-$((PORT + 1))}"
COHOST_SERVER_URL="${LIMINAL_COHOST_SERVER_URL:-https://zoom-host-guard-coordinator-dwfg7dsmya-uc.a.run.app/}"
WORKSPACE="${LIMINAL_WORKSPACE:-default}"
SAVE_DIR="${LIMINAL_SAVE_DIR:-$HOME/Liminal Shield Downloads}"
LAUNCHER_NAME="${LIMINAL_LAUNCHER_NAME:-Liminal Shield}"
CREATE_DESKTOP_SHORTCUT="${LIMINAL_CREATE_DESKTOP_SHORTCUT:-1}"
INSTALL_DIR="${LIMINAL_INSTALL_DIR:-$HOME/.liminal-shield}"
BIN_DIR="$INSTALL_DIR/bin"
LAUNCHER_DIR="$INSTALL_DIR/launchers"
DESKTOP_DIR="${LIMINAL_DESKTOP_DIR:-$HOME/Desktop}"
RUNNER_PATH="$BIN_DIR/run.sh"

normalize_mode() {
  case "$1" in
    desktop|human-desktop|shield-desktop) echo "human-desktop" ;;
    chrome|human-chrome|shield-chrome) echo "human-chrome" ;;
    zoom-chrome|zoom-human-chrome|human-zoom-chrome|shield-zoom-participant|shield-zoom-host-chrome) echo "human-zoom-chrome" ;;
    zoom-desktop|zoom-human-desktop|human-zoom-desktop|shield-zoom-desktop|shield-zoom-host|shield-zoom-host-desktop) echo "human-zoom-desktop" ;;
    mac-zoom|zoom-mac|native-zoom|macos-zoom) echo "mac-zoom" ;;
    zoom-bot|zoom-cohost|bot-zoom|bot-zoom-cohost|shield-zoom-sentinel|shield-zoom-sentinel-local) echo "bot-zoom-cohost" ;;
    complete|complete-zoom|shield-complete-zoom) echo "complete" ;;
    complete-local|complete-desktop|host-complete-desktop|shield-host-complete-desktop|shield-zoom-desktop-with-sentinel|shield-zoom-complete-desktop|shield-zoom-complete-local) echo "complete-local" ;;
    complete-server|shield-zoom-cloud|shield-zoom-complete-server) echo "complete-server" ;;
    *)
      echo "Unsupported LIMINAL_MODE: $1" >&2
      exit 1
      ;;
  esac
}

quote_for_script() {
  printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"
}

safe_file_name() {
  printf '%s' "$1" | tr '/:' '--' | tr -cd '[:alnum:] ._-'
}

download_runner() {
  mkdir -p "$BIN_DIR" "$LAUNCHER_DIR"
  curl -fsSL "$BASE_URL/run.sh" -o "$RUNNER_PATH"
  chmod +x "$RUNNER_PATH"
}

write_launcher() {
  local file_path="$1"
  local mode="$2"
  local port="$3"
  local storage="$4"
  local media="$5"
  local workspace="$6"
  local save_dir="$7"
  local host_port="${8:-}"
  local bot_port="${9:-}"
  local cohost_server_url="${10:-}"

  {
    printf '%s\n' '#!/usr/bin/env bash'
    printf '%s\n' 'set -euo pipefail'
    printf 'export LIMINAL_MODE=%s\n' "$(quote_for_script "$mode")"
    printf 'export LIMINAL_SHIELD_PORT=%s\n' "$(quote_for_script "$port")"
    printf 'export LIMINAL_STORAGE=%s\n' "$(quote_for_script "$storage")"
    printf 'export LIMINAL_MEDIA=%s\n' "$(quote_for_script "$media")"
    printf 'export LIMINAL_HOST_RUNTIME=%s\n' "$(quote_for_script "$HOST_RUNTIME")"
    printf 'export LIMINAL_COHOST_RUNTIME=%s\n' "$(quote_for_script "$COHOST_RUNTIME")"
    printf 'export LIMINAL_WORKSPACE=%s\n' "$(quote_for_script "$workspace")"
    printf 'export LIMINAL_SAVE_DIR=%s\n' "$(quote_for_script "$save_dir")"
    if [[ -n "$host_port" ]]; then
      printf 'export LIMINAL_HOST_PORT=%s\n' "$(quote_for_script "$host_port")"
    fi
    if [[ -n "$bot_port" ]]; then
      printf 'export LIMINAL_BOT_PORT=%s\n' "$(quote_for_script "$bot_port")"
    fi
    if [[ -n "$cohost_server_url" ]]; then
      printf 'export LIMINAL_COHOST_SERVER_URL=%s\n' "$(quote_for_script "$cohost_server_url")"
    fi
    printf '%s\n' 'export LIMINAL_OPEN=1'
    printf '%s\n' "$(quote_for_script "$RUNNER_PATH")"
    printf '%s\n' 'echo'
    printf '%s\n' 'echo "Liminal Shield launcher finished. You can close this window."'
    printf '%s\n' 'read -r -p "Press Return to close..." _ || true'
  } > "$file_path"

  chmod +x "$file_path"
}

copy_to_desktop() {
  local launcher_path="$1"
  if [[ "$CREATE_DESKTOP_SHORTCUT" != "1" ]]; then
    return
  fi
  mkdir -p "$DESKTOP_DIR"
  cp "$launcher_path" "$DESKTOP_DIR/$(basename "$launcher_path")"
  chmod +x "$DESKTOP_DIR/$(basename "$launcher_path")"
}

install_single() {
  local mode="$1"
  local name="$2"
  local port="$3"
  local storage="$4"
  local media="$5"
  local workspace="$6"
  local save_dir="$7"
  local safe_name
  safe_name="$(safe_file_name "$name")"
  local launcher_path="$LAUNCHER_DIR/${safe_name}.command"
  write_launcher "$launcher_path" "$mode" "$port" "$storage" "$media" "$workspace" "$save_dir"
  copy_to_desktop "$launcher_path"
  echo "Installed: $launcher_path"
  if [[ "$CREATE_DESKTOP_SHORTCUT" == "1" ]]; then
    echo "Desktop:   $DESKTOP_DIR/${safe_name}.command"
  fi
}

host_mode_for_runtime() {
  case "$1" in
    chrome|zoom-chrome|human-zoom-chrome) echo "human-zoom-chrome" ;;
    app|zoom-app|desktop|zoom-desktop|human-zoom-desktop) echo "human-zoom-desktop" ;;
    mac|mac-zoom|zoom-mac|native-zoom|macos-zoom) echo "mac-zoom" ;;
    *)
      echo "Unsupported LIMINAL_HOST_RUNTIME: $1" >&2
      echo "Use one of: chrome, zoom-app, mac-zoom" >&2
      exit 1
      ;;
  esac
}

install_complete() {
  local complete_name="${LAUNCHER_NAME:-Shield Zoom Complete}"
  if [[ "$complete_name" == "Liminal Shield" ]]; then
    complete_name="Shield Zoom Complete"
  fi
  local host_mode
  host_mode="$(host_mode_for_runtime "$HOST_RUNTIME")"
  local host_label="Shield Zoom Host"
  if [[ "$host_mode" == "human-zoom-desktop" ]]; then
    host_label="Shield Zoom Desktop"
  elif [[ "$host_mode" == "human-zoom-chrome" ]]; then
    host_label="Shield Zoom Host Chrome"
  elif [[ "$host_mode" == "mac-zoom" ]]; then
    host_label="Shield Zoom Host Mac"
  fi
  if [[ "$host_mode" == "human-zoom-desktop" && "$COHOST_RUNTIME" == "local" && "$complete_name" == "Shield Zoom Complete" ]]; then
    complete_name="Shield Zoom Desktop with Local Sentinel"
  elif [[ "$host_mode" == "human-zoom-desktop" && ( "$COHOST_RUNTIME" == "server" || "$COHOST_RUNTIME" == "cloud" ) && "$complete_name" == "Shield Zoom Complete" ]]; then
    complete_name="Shield Zoom Desktop with Cloud Sentinel"
  fi

  install_single "complete" "$complete_name" "$PORT" "$STORAGE" "$MEDIA" "$WORKSPACE" "$SAVE_DIR"
  install_single "$host_mode" "$host_label" "$HOST_PORT" "$STORAGE" "$MEDIA" "$WORKSPACE" "$SAVE_DIR"

  if [[ "$COHOST_RUNTIME" == "local" ]]; then
    install_single "bot-zoom-cohost" "Shield Zoom Sentinel Local" "$BOT_PORT" "ephemeral" "none" "bot" "$SAVE_DIR"
  fi

  echo
  echo "$complete_name is installed."
  echo "Double-click '$complete_name.command' to start the host and co-host setup."
}

install_complete_local() {
  COHOST_RUNTIME="local"
  install_complete
}

install_complete_server() {
  COHOST_RUNTIME="server"
  install_complete
}

main() {
  local normalized
  normalized="$(normalize_mode "$MODE")"
  download_runner

  if [[ "$normalized" == "complete" ]]; then
    install_complete
  elif [[ "$normalized" == "complete-local" ]]; then
    install_complete_local
  elif [[ "$normalized" == "complete-server" ]]; then
    install_complete_server
  else
    install_single "$normalized" "$LAUNCHER_NAME" "$PORT" "$STORAGE" "$MEDIA" "$WORKSPACE" "$SAVE_DIR"
  fi
}

main "$@"
