#!/usr/bin/env bash # disc_sets.sh # Core helpers for identifying and grouping multi-disc game images. # Policy (per requirements): # * ONLY handle explicit numeric / Roman numeral disc markers: (Disc 1), (Disc 2), (Disc I), ... # * Do NOT group or include Bonus / Special / Extras discs (they lack numeric/roman index). # * Never rename underlying files; only derive playlist name by stripping the (Disc N) tag. # * Roman numerals supported up to X (extendable if needed). [[ -n "$DISC_SETS_LOADED" ]] && return 0 DISC_SETS_LOADED=1 roman_to_int_disc() { local r=${1^^} # Fast path for digits if [[ $r =~ ^[0-9]+$ ]]; then echo "$r"; return 0 fi # General Roman numeral parser (basic, up to 3999, we only expect small values) local map=( [I]=1 [V]=5 [X]=10 [L]=50 [C]=100 [D]=500 [M]=1000 ) local len=${#r} i=0 total=0 curr next while (( i < len )); do curr=${map[${r:i:1}]:-0} (( i+1 < len )) && next=${map[${r:i+1:1}]:-0} || next=0 if (( curr == 0 )); then echo "$1"; return 0 # Not a valid Roman numeral; fall back to original token fi if (( curr < next )); then total=$(( total + next - curr )) i=$(( i + 2 )) else total=$(( total + curr )) i=$(( i + 1 )) fi done echo "$total" } # Returns 0 if filename contains a (Disc N) marker with numeric or roman numeral index. disc_is_main() { local fname=$1 # Accept Disc 0 and extended Roman numerals (I,V,X,L,C,D,M combinations) [[ $fname =~ \(Disc[[:space:]]+([0-9IVXLCDM]+)\) ]] || return 1 return 0 } # Extract numeric ordering token (zero padded to 3) from filename. disc_order_token() { local fname=$1 local idx if [[ $fname =~ \(Disc[[:space:]]+([0-9IVXLCDM]+)\) ]]; then idx=$(roman_to_int_disc "${BASH_REMATCH[1]}") if [[ $idx =~ ^[0-9]+$ ]]; then # Optional remap: treat Disc 0 as Disc 1 if DISK_ZERO_BASE_ONE=1 if [[ $idx -eq 0 && ${DISK_ZERO_BASE_ONE:-0} -eq 1 ]]; then idx=1 fi printf '%03d' "$idx" return 0 fi fi # Fallback sentinel to push unknowns last echo "999" } # Derive playlist base name from filename by stripping a single (Disc N) tag. playlist_base_name() { local fname=$1 # Remove extension first local noext=${fname%.*} # Strip the first (Disc N) occurrence while preserving surrounding spacing noext=$(sed -E 's/[[:space:]]*\(Disc[[:space:]]+[0-9IVX]+\)//I' <<<"$noext") # Trim trailing spaces sed -E 's/[[:space:]]+$//' <<<"$noext" } # Collect grouped disc candidates; prints tab-separated lines: KEYDIRORDERFILENAME # KEY format: DIR__PLAYLIST_BASENAME disc_collect_groups() { local root=$1 shift local -a exts=("$@") shopt -s nullglob nocaseglob local -a files=() local e for e in "${exts[@]}"; do while IFS= read -r -d '' f; do files+=("$f") done < <(find "$root" -type f -iname "*.${e}" -print0) done local f fname dir base order key for f in "${files[@]}"; do fname=$(basename "$f") if ! disc_is_main "$fname"; then continue fi order=$(disc_order_token "$fname") dir=$(dirname "$f") base=$(playlist_base_name "$fname") key="${dir}__${base}" printf '%s\t%s\t%s\t%s\n' "$key" "$dir" "$order" "$fname" done } disc_write_playlists() { local min_discs=$1 force=$2 dry_run=$3 verbose=$4 shift 4 local input_lines=$1 local created=0 skipped=0 groups=0 # Build associative arrays declare -A orders_by_key declare -A dir_by_key while IFS=$'\t' read -r key dir order fname; do [[ -z $key ]] && continue dir_by_key[$key]=$dir orders_by_key[$key]+="${order}|${fname}\n" done <<<"$input_lines" for key in "${!orders_by_key[@]}"; do groups=$((groups+1)) local dir=${dir_by_key[$key]} local base=${key#*__} local playlist="${dir}/${base}.m3u" mapfile -t entries < <(printf '%s' "${orders_by_key[$key]}" | grep -v '^$' | sort) local count=${#entries[@]} if (( count < min_discs )); then ((verbose)) && echo "[disc] skip (only ${count} disc): $playlist" skipped=$((skipped+1)) continue fi if [[ -f $playlist && $force -ne 1 ]]; then ((verbose)) && echo "[disc] exists (skip): $playlist" skipped=$((skipped+1)) continue fi local content="" local ent fname for ent in "${entries[@]}"; do fname=${ent#*|} content+="${fname}\n" done if (( dry_run )); then echo "[disc] DRY-RUN would create: $playlist (${count} discs)" else printf '%s' "$content" > "$playlist" echo "[disc] Created: $playlist (${count} discs)" fi created=$((created+1)) done echo "[disc] Summary: created=$created skipped=$skipped groups=$groups" } return 0