#!/bin/bash
STAMP=20260714
QP=$HOME/_quarantine_seobooster_$STAMP
QC=$HOME/_wpconfig_bak_$STAMP
mkdir -p "$QP" "$QC"; chmod 700 "$QC"
IOC_EA='_ea_wc|_EA_WC_'
IOC_LPK='linksparagon|__LPK_IN__|__lpk_ob_cb__|__is_spy_bot__|__get_backlink_codes__'
for site in "$@"; do
  R="$HOME/$site"; WC="$R/wp-config.php"
  echo "  --- site: $site ---"
  [ -f "$WC" ] || { echo "  (pas de wp-config)"; continue; }
  # 1) wp-config _ea_wc (auto)
  if grep -aqEi "$IOC_EA" "$WC" 2>/dev/null; then
    echo "  injection _ea_wc:"; grep -anEi "$IOC_EA" "$WC" | head -n 4 | sed 's/^/      /'
    cp "$WC" "$QC/${site}_wp-config.php.bak"
    sed -E '\#_ea_wc_s#I,\#_ea_wc_e#Id' "$WC" > "$WC.tmp" 2>/dev/null
    removed=$(( $(wc -l < "$WC") - $(wc -l < "$WC.tmp") ))
    if php -l "$WC.tmp" >/dev/null 2>&1 && grep -qE 'DB_NAME' "$WC.tmp" && ! grep -aqEi "$IOC_EA" "$WC.tmp" && [ "$removed" -ge 1 ]; then
      mv "$WC.tmp" "$WC"; echo "  OK wp-config _ea_wc retire ($removed ligne(s)) — backup: $QC/${site}_wp-config.php.bak"
    else rm -f "$WC.tmp"; echo "  !! strip _ea_wc ECHOUE — wp-config INTACT, backup pris, a inspecter"; fi
  fi
  # 2) wp-config linksparagon (manuel : on AFFICHE seulement)
  if grep -aqEi "$IOC_LPK" "$WC" 2>/dev/null; then
    echo "  !! injection LINKSPARAGON/__LPK_IN__ (traitement manuel, non modifie) :"; grep -anEi "$IOC_LPK" "$WC" | head -n 8 | sed 's/^/      /'
  fi
  # 3) plugins hseo / wp-helper -> quarantaine
  PD="$R/wp-content/plugins"
  if [ -d "$PD" ]; then
    for g in $(ls -1 "$PD" 2>/dev/null | grep -iE '^hseo|^wp-helper'); do
      mkdir -p "$QP/$site"; mv "$PD/$g" "$QP/$site/" && echo "  OK plugin SEO-Booster quarantaine-> $g"
    done
  fi
  # 4) cache regenerable
  [ -d "$R/wp-content/cache" ] && rm -rf "$R/wp-content/cache"/* 2>/dev/null && echo "  cache purge"
  # 5) re-scan
  a=0
  grep -aqEi "$IOC_EA" "$WC" 2>/dev/null && { echo "  !! reste _ea_wc"; a=1; }
  grep -aqEi "$IOC_LPK" "$WC" 2>/dev/null && { echo "  !! reste linksparagon (manuel)"; a=1; }
  ls -1 "$R/wp-content/plugins" 2>/dev/null | grep -iqE '^hseo|^wp-helper' && { echo "  !! reste plugin SEO-Booster"; a=1; }
  [ $a -eq 0 ] && echo "  >>> $site: PROPRE" || echo "  >>> $site: RESTE IOC -- voir !!"
done
