#!/usr/bin/env bash
#
# Prepares bundled resources for distribution.
#
# This script copies resources that should be bundled with Warp into a
# destination directory. It is used by macOS and Linux build scripts.
#
# Usage:
#   prepare_bundled_resources <destination_directory> [channel] [cargo_profile]
#
# Arguments:
#   destination_directory:  The directory where resources should be installed.
#                           Resources will be copied to subdirectories within
#                           this path (e.g., $DEST_DIR/skills).
#   channel:                (Optional) Release channel (local, dev, preview,
#                           stable). Used to include channel-gated skills.
#   cargo_profile:          (Optional) Cargo build profile to use when
#                           generating the settings schema. Reusing the same
#                           profile as the main build avoids recompiling deps.
#
# Environment variables:
#   SKIP_SETTINGS_SCHEMA: Set to "1" to skip generating the JSON settings
#                         schema. By default the schema is always generated
#                         so that production bundles never accidentally omit it.
#   SETTINGS_SCHEMA_CACHE: Path to a cache file for the generated schema.
#                          When set, the first invocation generates the schema
#                          and saves a copy to this path; subsequent invocations
#                          copy from the cache instead of regenerating. This
#                          avoids redundant compilations when bundling multiple
#                          package formats for the same channel.

set -e

if [ $# -lt 1 ] || [ $# -gt 3 ]; then
  echo "Error: Expected 1-3 arguments but received $#" >&2
  echo "Usage: $0 <destination_directory> [channel] [cargo_profile]" >&2
  exit 1
fi

DEST_DIR="$1"
CHANNEL="${2:-}"
CARGO_PROFILE="${3:-}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
RESOURCES_SRC="$REPO_ROOT/resources"

# Validate that the source resources directory exists
if [ ! -d "$RESOURCES_SRC" ]; then
  echo "Error: Resources directory not found at $RESOURCES_SRC" >&2
  exit 1
fi

# Create the destination directory if it doesn't exist
mkdir -p "$DEST_DIR"

# Copy bundled resources
if [ -d "$RESOURCES_SRC/bundled" ]; then
  echo "Copying bundled resources to $DEST_DIR/bundled"
  rm -rf "$DEST_DIR/bundled"
  cp -R "$RESOURCES_SRC/bundled" "$DEST_DIR/bundled"
else
  echo "Warning: No bundled directory found at $RESOURCES_SRC/bundled" >&2
fi

if [ -n "$GIT_RELEASE_TAG" ]; then
  VERSION_METADATA_DIR="$DEST_DIR/bundled/metadata"
  VERSION_METADATA_PATH="$VERSION_METADATA_DIR/version.json"

  echo "Writing bundled Warp version metadata to $VERSION_METADATA_PATH"
  mkdir -p "$VERSION_METADATA_DIR"
  printf '{\n  "warp_version": "%s"\n}\n' "$GIT_RELEASE_TAG" > "$VERSION_METADATA_PATH"
fi

# Copy channel-gated skills matching the current release channel.
GATED_SRC="$REPO_ROOT/resources/channel-gated-skills"
DEST_SKILLS="$DEST_DIR/bundled/skills"

if [ -n "$CHANNEL" ] && [ -d "$GATED_SRC" ]; then
  echo "Copying channel-gated skills for channel '$CHANNEL'..."
  "$SCRIPT_DIR/copy_conditional_skills" \
    "$CHANNEL" \
    "$GATED_SRC" \
    "$DEST_SKILLS"
fi

# Generate third-party license attribution.
#
# Additional (non-Cargo) third-party license files to include in the output.
# Each entry is: "Component Name|License Identifier|path/to/LICENSE/relative/to/repo/root"
# When adding a new third-party component to the bundle, add its license file
# to the repo alongside the component and add an entry here.
ADDITIONAL_LICENSES=(
    "Alacritty (alacritty_terminal)|Apache-2.0|crates/warp_terminal/src/model/LICENSE-ALACRITTY"
    "Hack Font|MIT|app/assets/bundled/fonts/hack/LICENSE.md"
    "Roboto Font|SIL Open Font License|app/assets/bundled/fonts/roboto/LICENSE.txt"
    "bash-preexec|MIT|app/assets/bundled/bootstrap/bash-preexec-LICENSE.md"
    "Claude API Skill|Apache-2.0|resources/bundled/skills/claude-api/LICENSE.txt"
    "rudder-sdk-rust|MIT|app/src/server/telemetry/LICENSE-RUDDER-SDK-RUST.txt"
    "Windows Terminal|MIT|app/assets/windows/LICENSE-WINDOWS-TERMINAL"
    "GitHub Desktop|MIT|app/src/code_review/GITHUB-DESKTOP-LICENSE"
)

# Build the third-party licenses file unless the NO_LICENSES envvar is set.
if [ -z "$NO_LICENSES" ]; then
  LICENSES_OUTPUT="$DEST_DIR/THIRD_PARTY_LICENSES.txt"
  echo "Generating third-party licenses at $LICENSES_OUTPUT"
  cargo about generate \
      --workspace \
      --manifest-path "$REPO_ROOT/Cargo.toml" \
      -c "$REPO_ROOT/about.toml" \
      -o "$LICENSES_OUTPUT" \
      "$REPO_ROOT/about.hbs"

  # Append additional (non-Cargo) third-party licenses.
  for entry in "${ADDITIONAL_LICENSES[@]}"; do
      IFS='|' read -r name license_id license_path <<< "$entry"
      license_file="$REPO_ROOT/$license_path"
      if [ ! -f "$license_file" ]; then
          echo "Error: License file not found: $license_file" >&2
          exit 1
      fi
      printf '\n%s (%s)\n' "$name" "$license_id" >> "$LICENSES_OUTPUT"
      printf '%0.s-' {1..80} >> "$LICENSES_OUTPUT"
      printf '\n' >> "$LICENSES_OUTPUT"
      cat "$license_file" >> "$LICENSES_OUTPUT"
      printf '\n' >> "$LICENSES_OUTPUT"
  done
fi

# Generate settings JSON schema unless explicitly skipped.
if [ "${SKIP_SETTINGS_SCHEMA:-}" != "1" ]; then
  SCHEMA_OUTPUT="$DEST_DIR/settings_schema.json"

  if [ -n "${SETTINGS_SCHEMA_CACHE:-}" ] && [ -f "$SETTINGS_SCHEMA_CACHE" ]; then
    echo "Copying cached settings schema to $SCHEMA_OUTPUT"
    cp "$SETTINGS_SCHEMA_CACHE" "$SCHEMA_OUTPUT"
  else
    echo "Generating settings schema at $SCHEMA_OUTPUT"

    SCHEMA_CMD=(cargo run)
    if [ -n "$CARGO_PROFILE" ]; then
      SCHEMA_CMD+=(--profile "$CARGO_PROFILE")
    fi
    SCHEMA_CMD+=(--manifest-path "$REPO_ROOT/Cargo.toml" --bin generate_settings_schema --)
    if [ -n "$CHANNEL" ]; then
      SCHEMA_CMD+=(--channel "$CHANNEL")
    fi
    SCHEMA_CMD+=("$SCHEMA_OUTPUT")

    "${SCHEMA_CMD[@]}"

    if [ -n "${SETTINGS_SCHEMA_CACHE:-}" ]; then
      echo "Caching settings schema at $SETTINGS_SCHEMA_CACHE"
      cp "$SCHEMA_OUTPUT" "$SETTINGS_SCHEMA_CACHE"
    fi
  fi
fi

echo "Successfully prepared bundled resources in $DEST_DIR"
