#!/usr/bin/env bash
# Batch-extract all orphan roots from orphan_roots.txt
# Runs up to JOBS parallel extract_tree.py processes.
# Usage: ./batch_recover.sh [--restore-meta] [--include-deleted]
set -euo pipefail

DEST="/mnt/recovered/apr30"
DB="inodes.db"
INPUT="orphan_roots.txt"
JOBS=4
EXTRA_ARGS=()

for arg in "$@"; do
    EXTRA_ARGS+=("$arg")
done

mkdir -p "$DEST"

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

wait
echo "All done"
