#!/usr/bin/env bash
#
# clean_old_subfolders.sh
#
# Deletes all SUBFOLDERS inside every date-named folder (./YYYY-MM-DD)
# in the current directory, EXCEPT the two most recent date folders.
# The date folders themselves are left in place, along with any files
# directly inside them — only their subdirectories are removed.
#
# Usage:
#   ./clean_old_subfolders.sh            # dry run, shows what would be deleted
#   ./clean_old_subfolders.sh --apply    # actually deletes
set -euo pipefail
export DATE=`date +%F`
cd /gpfs/hpc/scratch/peterwillendrup/nightlies

mkdir -p $DATE

APPLY=false
if [[ "${1:-}" == "--apply" ]]; then
    APPLY=true
fi

# Find date-named folders, sort chronologically
mapfile -t date_folders < <(find . -maxdepth 1 -type d -name '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' -printf '%f\n' | sort)

total=${#date_folders[@]}
if (( total <= 2 )); then
    echo "Only $total date folder(s) found — nothing to clean (need more than 2 to skip the last two)."
    exit 0
fi

# All but the last two
keep_count=$(( total - 2 ))
targets=("${date_folders[@]:0:keep_count}")

echo "Date folders found: $total"
echo "Folders to KEEP untouched (most recent 2): ${date_folders[@]: -2}"
echo "Folders whose SUBFOLDERS will be removed: ${targets[*]}"
echo

if [[ "$APPLY" == false ]]; then
    echo "[DRY RUN] No changes made. Subdirectories that WOULD be deleted:"
    for d in "${targets[@]}"; do
        find "./$d" -mindepth 1 -maxdepth 1 -type d
    done
    echo
    echo "Re-run with --apply to actually delete these."
else
    for d in "${targets[@]}"; do
        while IFS= read -r sub; do
            echo "Removing: $sub"
            rm -rf -- "$sub"
        done < <(find "./$d" -mindepth 1 -maxdepth 1 -type d)
    done
    echo "Done."
fi

rsync -avz --delete /gpfs/hpc/scratch/peterwillendrup/nightlies/ /nfs/www/html/users/willend/nightlies --exclude "*.out" --exclude "*_list.*" --exclude "*.h5"
