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

FASTDL_ROOT="/srv/nvme/Archives/SvenBackup/svencoop-files/svencoop_addon"
cd "$FASTDL_ROOT" || exit 1

# pick compressor: pigz if available, else gzip
if command -v pigz &>/dev/null; then
  COMPRESS="pigz -9 -kf"
else
  COMPRESS="gzip -9 -kf"
fi

# extensions to compress
exts=(bsp wav res wad mdl vtf vmt txt)

for ext in "${exts[@]}"; do
  find -L . -type f -iname "*.${ext}" ! -iname "*.gz" -print0 \
    | while IFS= read -r -d '' file; do
        gz="${file}.gz"
        if [[ -f "$gz" ]]; then
          echo "Skipping (already has .gz): $file"
          continue
        fi
        echo "Compressing: $file"
        $COMPRESS "$file" && chmod 644 "$gz"
      done
done

echo "✅ Done compressing all FastDL assets."