#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────────────
# Add Liara mirror repos to Nexus and create groups (Liara → Runflare fallback)
#
# New repos:
#   npm-liara-proxy        →  https://package-mirror.liara.ir/repository/npm/
#   npm-group              →  npm-liara-proxy  +  npm-proxy (Runflare)
#   pypi-liara-proxy       →  https://package-mirror.liara.ir/repository/pypi/
#   pypi-group             →  pypi-liara-proxy  +  pypi-proxy (Runflare)
#   ubuntu-proxy           →  http://linux-mirror.liara.ir/repository/ubuntu/
#   ubuntu-security-proxy  →  http://linux-mirror.liara.ir/repository/ubuntu-security/
#
# Usage:
#   ./mirrors/nexus/add-liara-mirrors.sh
#   NEXUS_ADMIN_PASS=MyPass ./mirrors/nexus/add-liara-mirrors.sh
#   UBUNTU_DIST=focal ./mirrors/nexus/add-liara-mirrors.sh   # default: jammy
# ─────────────────────────────────────────────────────────────────────────────
set -euo pipefail

NEXUS_URL="http://localhost:8081"
ADMIN_PASS="${NEXUS_ADMIN_PASS:-Mirror@2024!}"
UBUNTU_DIST="${UBUNTU_DIST:-jammy}"   # change to focal/bionic if your Dockerfiles use older Ubuntu
AUTH="-u admin:$ADMIN_PASS"

ok()   { echo "✅  $1"; }
skip() { echo "⚠️   $1 already exists (skipped)"; }
fail() { echo "❌  $1 — HTTP $2"; }

add_repo() {
  local TYPE="$1" JSON="$2"
  local NAME
  NAME=$(echo "$JSON" | grep -o '"name":"[^"]*"' | head -1 | cut -d'"' -f4)
  local HTTP
  HTTP=$(curl -s -o /dev/null -w "%{http_code}" $AUTH \
    -X POST "$NEXUS_URL/service/rest/v1/repositories/$TYPE" \
    -H "Content-Type: application/json" -d "$JSON")
  case "$HTTP" in
    201) ok "$NAME"   ;;
    400) skip "$NAME" ;;
    *)   fail "$NAME" "$HTTP" ;;
  esac
}

# ── npm ───────────────────────────────────────────────────────────────────────
echo ""
echo "📦  npm"

add_repo "npm/proxy" '{
  "name": "npm-liara-proxy",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": false},
  "proxy": {
    "remoteUrl": "https://package-mirror.liara.ir/repository/npm/",
    "contentMaxAge": 1440, "metadataMaxAge": 1440
  },
  "negativeCache": {"enabled": true, "timeToLive": 1440},
  "httpClient": {"blocked": false, "autoBlock": true}
}'

add_repo "npm/group" '{
  "name": "npm-group",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": false},
  "group": {
    "memberNames": ["npm-liara-proxy", "npm-proxy"]
  }
}'

# ── PyPI ──────────────────────────────────────────────────────────────────────
echo ""
echo "🐍  PyPI"

add_repo "pypi/proxy" '{
  "name": "pypi-liara-proxy",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": true},
  "proxy": {
    "remoteUrl": "https://package-mirror.liara.ir/repository/pypi/",
    "contentMaxAge": 1440, "metadataMaxAge": 1440
  },
  "negativeCache": {"enabled": true, "timeToLive": 1440},
  "httpClient": {"blocked": false, "autoBlock": true}
}'

add_repo "pypi/group" '{
  "name": "pypi-group",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": true},
  "group": {
    "memberNames": ["pypi-liara-proxy", "pypi-proxy"]
  }
}'

# ── Ubuntu APT ────────────────────────────────────────────────────────────────
echo ""
echo "🐧  Ubuntu APT (distribution: $UBUNTU_DIST)"
echo "    If your Dockerfiles use a different Ubuntu version, re-run with:"
echo "    UBUNTU_DIST=focal $0"
echo ""

add_repo "apt/proxy" '{
  "name": "ubuntu-proxy",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": false},
  "proxy": {
    "remoteUrl": "http://linux-mirror.liara.ir/repository/ubuntu/",
    "contentMaxAge": 1440, "metadataMaxAge": 1440
  },
  "negativeCache": {"enabled": true, "timeToLive": 1440},
  "httpClient": {
    "blocked": false, "autoBlock": true,
    "connection": {"useTrustStore": false}
  },
  "apt": {"distribution": "'"$UBUNTU_DIST"'", "flat": false}
}'

add_repo "apt/proxy" '{
  "name": "ubuntu-security-proxy",
  "online": true,
  "storage": {"blobStoreName": "default", "strictContentTypeValidation": false},
  "proxy": {
    "remoteUrl": "http://linux-mirror.liara.ir/repository/ubuntu-security/",
    "contentMaxAge": 1440, "metadataMaxAge": 1440
  },
  "negativeCache": {"enabled": true, "timeToLive": 1440},
  "httpClient": {
    "blocked": false, "autoBlock": true,
    "connection": {"useTrustStore": false}
  },
  "apt": {"distribution": "'"$UBUNTU_DIST"'", "flat": false}
}'

# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "🎉  Done!"
echo "═══════════════════════════════════════════════════════════════"
echo ""
echo "  npm-group   →  https://mirror.soroushasadi.com/repository/npm-group/"
echo "                 Liara first, Runflare as fallback"
echo ""
echo "  pypi-group  →  https://mirror.soroushasadi.com/repository/pypi-group/"
echo "                 Liara first, Runflare as fallback"
echo ""
echo "  Ubuntu APT  →  https://mirror.soroushasadi.com/repository/ubuntu-proxy/"
echo "                 distribution: $UBUNTU_DIST"
echo "                 security:     https://mirror.soroushasadi.com/repository/ubuntu-security-proxy/"
echo ""
echo "To use Ubuntu APT in a Dockerfile:"
echo "  RUN echo 'deb https://mirror.soroushasadi.com/repository/ubuntu-proxy/ $UBUNTU_DIST main restricted universe' > /etc/apt/sources.list && \\"
echo "      echo 'deb https://mirror.soroushasadi.com/repository/ubuntu-security-proxy/ $UBUNTU_DIST-security main restricted universe' >> /etc/apt/sources.list && \\"
echo "      apt-get update"
echo ""
