#!/bin/bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
MOTRIX_DIR="${1:-/Users/A/Work/Motrix}"
SRC_BASE="$MOTRIX_DIR/extra"
DST_BASE="$ROOT_DIR/src-tauri/resources/engine"

if [ ! -d "$SRC_BASE" ]; then
  echo "[sync-aria2] Motrix extra directory not found: $SRC_BASE"
  exit 1
fi

rm -rf "$DST_BASE"
mkdir -p "$DST_BASE"

copy_engine_dir() {
  local src="$1"
  local dst="$2"
  if [ -d "$src" ]; then
    mkdir -p "$dst"
    cp "$src"/aria2c* "$dst"/ 2>/dev/null || true
    cp "$src"/aria2.conf "$dst"/ 2>/dev/null || true
    echo "[sync-aria2] copied $src -> $dst"
  fi
}

# macOS
copy_engine_dir "$SRC_BASE/darwin/arm64/engine" "$DST_BASE/darwin/arm64"
copy_engine_dir "$SRC_BASE/darwin/x64/engine" "$DST_BASE/darwin/x64"

# Linux aliases
copy_engine_dir "$SRC_BASE/linux/x64/engine" "$DST_BASE/linux/x64"
copy_engine_dir "$SRC_BASE/linux/arm64/engine" "$DST_BASE/linux/arm64"
copy_engine_dir "$SRC_BASE/linux/armv7l/engine" "$DST_BASE/linux/armv7l"

# Windows
copy_engine_dir "$SRC_BASE/win32/x64/engine" "$DST_BASE/win32/x64"
copy_engine_dir "$SRC_BASE/win32/ia32/engine" "$DST_BASE/win32/ia32"

echo "[sync-aria2] done"
