#!/bin/bash
LOG=~/.maintenance-logs/rmlpk-20260713.txt
exec > >(tee "$LOG") 2>&1
echo "########## RETRAIT __LPK_IN__/linksparagon $(date) ##########"
SITES="abtan assayag azogui lahmi lasvergnas medicalesthetique medina miquel nassif s-hombrouck smileart-ads zeitoun-saal"
BAKDIR=~/.maintenance-logs/wpconfig-bak-20260713
mkdir -p "$BAKDIR"
for s in $SITES; do
  f=~/$s/wp-config.php
  [ -f "$f" ] || { echo "  [$s] pas de wp-config -> SKIP"; continue; }
  if ! grep -q "__LPK_IN__" "$f"; then echo "  [$s] deja propre (pas de __LPK_IN__)"; continue; fi
  cp -p "$f" "$BAKDIR/$s-wp-config.php"
  before=$(wc -l < "$f")
  awk -f ~/_rmlpk.awk "$f" > "$f.tmp"
  after=$(wc -l < "$f.tmp")
  if ! php -l "$f.tmp" >/dev/null 2>&1; then
    echo "  [$s] ECHEC php -l apres retrait -> ORIGINAL INCHANGE (tmp supprime)"; rm -f "$f.tmp"; continue
  fi
  if grep -qE "__LPK_IN__|linksparagon" "$f.tmp"; then
    echo "  [$s] ECHEC: marqueur encore present dans le resultat -> ORIGINAL INCHANGE (tmp supprime)"; rm -f "$f.tmp"; continue
  fi
  mv "$f.tmp" "$f"
  echo "  [$s] OK: bloc retire ($before -> $after lignes) | backup: $BAKDIR/$s-wp-config.php"
done
echo "==== Re-hunt IOC dans les wp-config apres retrait ===="
H=0
for s in $SITES; do
  r=$(grep -nE "elysianlink|KAPTAN2025|__LPK_IN__|linksparagon|isGoogleUserAgent|sinanyildiz5747" ~/$s/wp-config.php 2>/dev/null)
  [ -n "$r" ] && { echo "  [$s] ENCORE-> $r"; H=1; }
done
[ $H -eq 0 ] && echo "  aucun IOC restant dans les wp-config -> OK"
echo "==== Controle php -l final des 12 wp-config ===="
for s in $SITES; do php -l ~/$s/wp-config.php >/dev/null 2>&1 && echo "  [$s] php -l OK" || echo "  [$s] php -l ERREUR !!"; done
echo "########## FIN $(date) ##########"
