#!/usr/bin/env bash
# Batch-restore metadata for already-extracted trees.
# Usage: ./batch_restore.sh
set -euo pipefail

DEST="/mnt/recovered/apr30"
INPUT="orphan_roots.txt"
JOBS=10

while read -r inum rest; do
    [[ -z "$inum" || "$inum" == \#* ]] && continue
    dest="${DEST}/${inum}"
    [[ -d "$dest" ]] || continue
    python3 restore_meta.py "$inum" "$dest" &
    while (( $(jobs -r | wc -l) >= JOBS )); do
        wait -n 2>/dev/null || sleep 0.2
    done
done < "$INPUT"

wait
echo "All done"
