110 lines
3.7 KiB
Bash
Executable file
110 lines
3.7 KiB
Bash
Executable file
#!/bin/bash
|
|
# replace_hd_images_v2.sh — source : HD_DECOR-6_47L
|
|
# Usage:
|
|
# dry-run (défaut) : ./replace_hd_images_v2.sh
|
|
# appliquer : ./replace_hd_images_v2.sh apply
|
|
# un chapitre seul : ./replace_hd_images_v2.sh apply "10_enseigner-..."
|
|
|
|
set -e
|
|
|
|
CONTENT_ROOT="/Users/sarah/Sites/revue_decor/content"
|
|
HD_DIR="/Users/sarah/Sites/revue_decor/HD_DECOR-6_47L"
|
|
MODE="${1:-dry-run}"
|
|
TARGET_CHAPTER="${2:-}"
|
|
|
|
do_replace() {
|
|
local chapter="$1"
|
|
local chapter_dir="$CONTENT_ROOT/$chapter"
|
|
local slug="${chapter#*_}"
|
|
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo "Chapitre : $chapter"
|
|
echo ""
|
|
|
|
local found_any=false
|
|
|
|
while IFS= read -r content_file; do
|
|
local content_basename
|
|
content_basename=$(basename "$content_file")
|
|
local content_base="${content_basename%.*}"
|
|
local content_ext="${content_basename##*.}"
|
|
|
|
# Cherche un fichier HD .jpg avec le nom exact
|
|
local hd_file=""
|
|
for f in "$HD_DIR"/*_"${slug}"__"${content_base}".jpg; do
|
|
[ -f "$f" ] && hd_file="$f" && break
|
|
done
|
|
|
|
# Si pas trouvé, cherche avec suffixe -0
|
|
if [ -z "$hd_file" ]; then
|
|
for f in "$HD_DIR"/*_"${slug}"__"${content_base}"-0.jpg; do
|
|
[ -f "$f" ] && hd_file="$f" && break
|
|
done
|
|
fi
|
|
|
|
if [ -z "$hd_file" ]; then
|
|
echo " [—] Pas de correspondance HD : $content_basename"
|
|
continue
|
|
fi
|
|
|
|
found_any=true
|
|
local hd_basename
|
|
hd_basename=$(basename "$hd_file")
|
|
local new_basename="${content_base}.jpg"
|
|
local dest_file="$chapter_dir/$new_basename"
|
|
|
|
# Vérifier si le fichier destination est déjà identique à la source HD
|
|
if [ -f "$dest_file" ] && cmp -s "$hd_file" "$dest_file"; then
|
|
echo " [=] Déjà à jour, ignoré : $new_basename"
|
|
continue
|
|
fi
|
|
|
|
if [ "$content_ext" = "jpg" ]; then
|
|
echo " [↑] $content_basename ← $hd_basename (mise à jour)"
|
|
else
|
|
echo " [✓] $content_basename → $new_basename ← $hd_basename"
|
|
echo " .txt : ${content_basename}.txt → ${new_basename}.txt"
|
|
fi
|
|
|
|
if [ "$MODE" = "apply" ]; then
|
|
cp "$hd_file" "$dest_file"
|
|
# Supprimer l'ancien fichier si extension différente
|
|
if [ "$content_ext" != "jpg" ] && [ -f "$chapter_dir/$content_basename" ]; then
|
|
rm "$chapter_dir/$content_basename"
|
|
fi
|
|
# Renommer le .txt si extension différente
|
|
if [ "$content_ext" != "jpg" ] && [ -f "$chapter_dir/${content_basename}.txt" ]; then
|
|
mv "$chapter_dir/${content_basename}.txt" "$chapter_dir/${new_basename}.txt"
|
|
fi
|
|
fi
|
|
|
|
done < <(find "$chapter_dir" -maxdepth 1 -type f ! -name "*.txt" ! -name "chapitre.txt" | sort)
|
|
|
|
if [ "$found_any" = false ]; then
|
|
echo " (aucune image HD trouvée pour ce chapitre)"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
echo ""
|
|
if [ "$MODE" = "apply" ]; then
|
|
echo "MODE : APPLY — les fichiers vont être remplacés"
|
|
else
|
|
echo "MODE : DRY-RUN — aucune modification"
|
|
fi
|
|
echo ""
|
|
|
|
if [ -n "$TARGET_CHAPTER" ]; then
|
|
do_replace "$TARGET_CHAPTER"
|
|
else
|
|
for chapter_dir in "$CONTENT_ROOT"/*/; do
|
|
chapter=$(basename "$chapter_dir")
|
|
[[ "$chapter" == _* ]] && continue
|
|
[[ "$chapter" == "home" || "$chapter" == "error" || "$chapter" == "print" ]] && continue
|
|
do_replace "$chapter"
|
|
done
|
|
fi
|
|
|
|
if [ "$MODE" = "dry-run" ]; then
|
|
echo "→ Dry-run terminé. Pour appliquer : bash <script> apply"
|
|
fi
|