#!/bin/bash

BASE_DIR="/data/download/playwright"

# ===== 版本号维护区 =====
CHROME_VERSIONS=(
  "145.0.7632.6"
)
FIREFOX_VERSIONS=(
  "1509"
)
WEBKIT_VERSIONS=(
  "2248"
)

# ===== 下载函数 =====
download_if_not_exists() {
  local url="$1"
  local outfile="$2"
  if [ -f "$outfile" ]; then
    echo "已存在: $outfile，跳过下载"
  else
    echo "下载: $outfile"
    mkdir -p "$(dirname "$outfile")"
    wget -O "$outfile" "$url"
  fi
}

# ===== Chrome 下载 =====
for ver in "${CHROME_VERSIONS[@]}"; do
  # Linux
  download_if_not_exists \
    "https://cdn.playwright.dev/chrome-for-testing-public/${ver}/linux64/chrome-linux64.zip" \
    "$BASE_DIR/${ver}/linux64/chrome-linux64.zip"
  download_if_not_exists \
    "https://cdn.playwright.dev/chrome-for-testing-public/${ver}/linux64/chrome-headless-shell-linux64.zip" \
    "$BASE_DIR/${ver}/linux64/chrome-headless-shell-linux64.zip"
  # Mac arm64
  download_if_not_exists \
    "https://cdn.playwright.dev/chrome-for-testing-public/${ver}/mac-arm64/chrome-mac-arm64.zip" \
    "$BASE_DIR/${ver}/mac-arm64/chrome-mac-arm64.zip"
  download_if_not_exists \
    "https://cdn.playwright.dev/chrome-for-testing-public/${ver}/mac-arm64/chrome-headless-shell-mac-arm64.zip" \
    "$BASE_DIR/${ver}/mac-arm64/chrome-headless-shell-mac-arm64.zip"
  # Win64
  download_if_not_exists \
    "https://cdn.playwright.dev/chrome-for-testing-public/${ver}/win64/chrome-win64.zip" \
    "$BASE_DIR/${ver}/win64/chrome-win64.zip"
  download_if_not_exists \
    "https://cdn.playwright.dev/chrome-for-testing-public/${ver}/win64/chrome-headless-shell-win64.zip" \
    "$BASE_DIR/${ver}/win64/chrome-headless-shell-win64.zip"
done

# ===== Firefox 下载 =====
for ver in "${FIREFOX_VERSIONS[@]}"; do
  # win64
  download_if_not_exists \
    "https://cdn.playwright.dev/dbazure/download/playwright/builds/firefox/${ver}/firefox-win64.zip" \
    "$BASE_DIR/builds/firefox/${ver}/firefox-win64.zip"
  # ubuntu-24.04
  download_if_not_exists \
    "https://cdn.playwright.dev/dbazure/download/playwright/builds/firefox/${ver}/firefox-ubuntu-24.04.zip" \
    "$BASE_DIR/builds/firefox/${ver}/firefox-ubuntu-24.04.zip"
  # mac
  download_if_not_exists \
    "https://cdn.playwright.dev/dbazure/download/playwright/builds/firefox/${ver}/firefox-osx.zip" \
    "$BASE_DIR/builds/firefox/${ver}/firefox-osx.zip"
done

# ===== Webkit 下载 =====
for ver in "${WEBKIT_VERSIONS[@]}"; do
  # win64
  download_if_not_exists \
    "https://cdn.playwright.dev/dbazure/download/playwright/builds/webkit/${ver}/webkit-win64.zip" \
    "$BASE_DIR/builds/webkit/${ver}/webkit-win64.zip"
  # ubuntu-24.04
  download_if_not_exists \
    "https://cdn.playwright.dev/dbazure/download/playwright/builds/webkit/${ver}/webkit-ubuntu-24.04.zip" \
    "$BASE_DIR/builds/webkit/${ver}/webkit-ubuntu-24.04.zip"
  # mac
  download_if_not_exists \
    "https://cdn.playwright.dev/dbazure/download/playwright/builds/webkit/${ver}/webkit-mac-13.zip" \
    "$BASE_DIR/builds/webkit/${ver}/webkit-mac-13.zip"
done

echo "所有文件已处理，存放在 $BASE_DIR"

