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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
VENDOR_DIR="$ROOT_DIR/tools/vendor"

choose_python() {
  if [ "${HERMES_PYTHON:-}" != "" ]; then
    command -v "$HERMES_PYTHON" 2>/dev/null || printf '%s\n' "$HERMES_PYTHON"
    return
  fi

  for candidate in python3.10 python3.9 python3.12 python3.11 python3; do
    if command -v "$candidate" >/dev/null 2>&1; then
      command -v "$candidate"
      return
    fi
  done

  echo "python3"
}

choose_osint_python() {
  if [ "${HERMES_OSINT_HOST_PYTHON:-}" != "" ]; then
    command -v "$HERMES_OSINT_HOST_PYTHON" 2>/dev/null || printf '%s\n' "$HERMES_OSINT_HOST_PYTHON"
    return
  fi

  for candidate in python3.12 python3.13 python3.14 python3.11 python3; do
    if command -v "$candidate" >/dev/null 2>&1; then
      command -v "$candidate"
      return
    fi
  done

  choose_python
}

HOST_PYTHON="$(choose_python)"
OSINT_HOST_PYTHON="$(choose_osint_python)"
MAPS_PYTHON="$ROOT_DIR/.venv-maps/bin/python"
SCRAPLING_PYTHON="$ROOT_DIR/.venv-scrapling/bin/python"
LINKEDIN_PYTHON="$ROOT_DIR/.venv-linkedin/bin/python"
OSINT_PYTHON="$ROOT_DIR/.venv-osint/bin/python"

mkdir -p "$VENDOR_DIR"

clone_or_update() {
  local repo_url="$1"
  local target_dir="$2"
  if [ ! -d "$target_dir/.git" ]; then
    if [ -d "$target_dir" ]; then
      echo "Using packaged tool source at $target_dir"
    else
      git clone "$repo_url" "$target_dir"
    fi
  else
    git -C "$target_dir" pull --ff-only
  fi
}

clone_or_update https://github.com/zohaibbashir/Google-Maps-Scrapper.git "$VENDOR_DIR/Google-Maps-Scrapper"
clone_or_update https://github.com/joeyism/linkedin_scraper.git "$VENDOR_DIR/linkedin_scraper"
clone_or_update https://github.com/speedyapply/JobSpy.git "$VENDOR_DIR/JobSpy"
clone_or_update https://github.com/josephlimtech/linkedin-profile-scraper-api.git "$VENDOR_DIR/linkedin-profile-scraper-api"
clone_or_update https://github.com/spinlud/py-linkedin-jobs-scraper.git "$VENDOR_DIR/py-linkedin-jobs-scraper"
clone_or_update https://github.com/D4Vinci/Scrapling.git "$VENDOR_DIR/Scrapling"
clone_or_update https://github.com/laramies/theHarvester.git "$VENDOR_DIR/theHarvester"
clone_or_update https://github.com/s0md3v/Photon.git "$VENDOR_DIR/Photon"
clone_or_update https://github.com/soxoj/maigret.git "$VENDOR_DIR/maigret"
clone_or_update https://github.com/sherlock-project/sherlock.git "$VENDOR_DIR/sherlock"
clone_or_update https://github.com/qeeqbox/social-analyzer.git "$VENDOR_DIR/social-analyzer"
clone_or_update https://github.com/reacherhq/backend.git "$VENDOR_DIR/reacher"

if [ ! -x "$MAPS_PYTHON" ]; then
  "$HOST_PYTHON" -m venv "$ROOT_DIR/.venv-maps"
fi

if [ ! -x "$SCRAPLING_PYTHON" ]; then
  "$HOST_PYTHON" -m venv "$ROOT_DIR/.venv-scrapling"
fi

if [ ! -x "$LINKEDIN_PYTHON" ]; then
  "$HOST_PYTHON" -m venv "$ROOT_DIR/.venv-linkedin"
fi

if [ -x "$OSINT_PYTHON" ] && ! "$OSINT_PYTHON" - <<'PY'
import sys
raise SystemExit(0 if sys.version_info >= (3, 12) else 1)
PY
then
  rm -rf "$ROOT_DIR/.venv-osint"
fi

if [ ! -x "$OSINT_PYTHON" ]; then
  "$OSINT_HOST_PYTHON" -m venv "$ROOT_DIR/.venv-osint"
fi

"$MAPS_PYTHON" -m pip install --upgrade pip
"$MAPS_PYTHON" -m pip install -r "$VENDOR_DIR/Google-Maps-Scrapper/requirements.txt"
"$MAPS_PYTHON" -m playwright install chromium || true

"$SCRAPLING_PYTHON" -m pip install --upgrade pip
"$SCRAPLING_PYTHON" -m pip install "scrapling[all]"
"$ROOT_DIR/.venv-scrapling/bin/scrapling" install || true

"$LINKEDIN_PYTHON" -m pip install --upgrade pip
"$LINKEDIN_PYTHON" -m pip install -e "$VENDOR_DIR/linkedin_scraper" || true
"$LINKEDIN_PYTHON" -m pip install -e "$VENDOR_DIR/JobSpy" || true
"$LINKEDIN_PYTHON" -m pip install -e "$VENDOR_DIR/py-linkedin-jobs-scraper" || true

if [ -f "$VENDOR_DIR/linkedin-profile-scraper-api/package.json" ] && command -v npm >/dev/null 2>&1; then
  PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm --prefix "$VENDOR_DIR/linkedin-profile-scraper-api" install || true
fi

"$OSINT_PYTHON" -m pip install --upgrade pip
"$OSINT_PYTHON" -m pip install -e "$VENDOR_DIR/maigret" || "$OSINT_PYTHON" -m pip install maigret || true
if [ -f "$VENDOR_DIR/sherlock/requirements.txt" ]; then
  "$OSINT_PYTHON" -m pip install -r "$VENDOR_DIR/sherlock/requirements.txt" || true
fi
"$OSINT_PYTHON" -m pip install -e "$VENDOR_DIR/sherlock" || "$OSINT_PYTHON" -m pip install sherlock-project || true
if [ -f "$VENDOR_DIR/Photon/requirements.txt" ]; then
  "$OSINT_PYTHON" -m pip install -r "$VENDOR_DIR/Photon/requirements.txt" || true
fi
if [ -f "$VENDOR_DIR/theHarvester/requirements/base.txt" ]; then
  "$OSINT_PYTHON" -m pip install -r "$VENDOR_DIR/theHarvester/requirements/base.txt" || true
elif [ -f "$VENDOR_DIR/theHarvester/requirements.txt" ]; then
  "$OSINT_PYTHON" -m pip install -r "$VENDOR_DIR/theHarvester/requirements.txt" || true
fi
"$OSINT_PYTHON" -m pip install -e "$VENDOR_DIR/theHarvester" || "$OSINT_PYTHON" -m pip install theHarvester || true
if [ -f "$VENDOR_DIR/social-analyzer/package.json" ] && command -v npm >/dev/null 2>&1; then
  PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true npm --prefix "$VENDOR_DIR/social-analyzer" install || true
fi

echo "Hermes tools installed under $VENDOR_DIR"
echo "Maps Python: $MAPS_PYTHON"
echo "Scrapling Python: $SCRAPLING_PYTHON"
echo "LinkedIn/profile tools Python: $LINKEDIN_PYTHON"
echo "OSINT/profile tools Python: $OSINT_PYTHON"
