#!/bin/bash ################################################################################ # Script: verify-and-clean-roms-zips.sh # Description: Validates ROM ZIP files against DAT file specifications and # removes files that don't contain valid ROMs or have hash mismatches. # # Usage: ./verify-and-clean-roms-zips.sh [--dry-run] # # Purpose: ROM-specific validation using DAT files (No-Intro/Redump) # - Validates ROM files by name matching against DAT entries # - Performs MD5/SHA1 hash verification for authenticity # - Optionally deletes invalid or corrupted ROM ZIP files # # Dependencies: unzip, md5sum, sha1sum, xmlstarlet, mktemp # # Related Tools: # - scan-archives.sh: For general archive integrity and corruption testing # # Examples: # ./verify-and-clean-roms-zips.sh nintendo.dat /path/to/nintendo/zips/ # ./verify-and-clean-roms-zips.sh redump-psx.dat /data/emu/psx/ --dry-run ################################################################################ set -euo pipefail SCRIPT_NAME="verify-and-clean-roms-zips.sh" DRY_RUN=false # Set umask to 000 to ensure files are created with 666 permissions umask 000 # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load configuration and logger from the script's directory source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" log "DEBUG" "Starting script: $(basename "$0")" show_usage() { echo "Usage: $0 [--dry-run]" echo "" echo "Validates ROM ZIP files against DAT file specifications and removes files that don't contain valid ROMs or have hash mismatches." echo "" echo "Parameters:" echo " Path to DAT file (No-Intro/Redump XML)" echo " Directory containing ROM ZIP files to validate" echo "" echo "Options:" echo " --dry-run Show what would be deleted without making changes" echo " --help Show this help message" echo "" echo "Examples:" echo " $0 nintendo.dat /data/emu/roms/nes/" echo " $0 redump-psx.dat /data/emu/psx/ --dry-run" echo "" echo "Related Tools: scan-archives.sh (general archive integrity)" } # Parse arguments if [[ $# -lt 2 ]]; then show_usage exit 1 fi DATFILE="$1" ZIPDIR="$2" shift 2 while [[ $# -gt 0 ]]; do case $1 in --dry-run) DRY_RUN=true ;; --help|-h) show_usage exit 0 ;; *) log "ERROR" "Unknown option: $1" show_usage exit 1 ;; esac shift done # Dependency checks for dep in xmlstarlet unzip md5sum sha1sum; do if ! command -v "$dep" >/dev/null; then log "ERROR" "$dep is required. Install it and try again." exit 1 fi done if [[ ! -f "$DATFILE" ]]; then log "ERROR" "DAT file not found: $DATFILE" exit 1 fi if [[ ! -d "$ZIPDIR" ]]; then log "ERROR" "ZIP directory not found: $ZIPDIR" exit 1 fi # Build associative arrays of rom name -> md5 and sha1 log "INFO" "Parsing DAT file: $DATFILE" declare -A ROM_MD5 ROM_SHA1 while IFS= read -r line; do name=$(echo "$line" | cut -d'|' -f1) md5=$(echo "$line" | cut -d'|' -f2) sha1=$(echo "$line" | cut -d'|' -f3) ROM_MD5["$name"]="$md5" ROM_SHA1["$name"]="$sha1" done < <(xmlstarlet sel -t -m "/datafile/game/rom" -v "@name" -o "|" -v "@md5" -o "|" -v "@sha1" -n "$DATFILE") checked=0 kept=0 deleted=0 errors=0 echo "" echo "🔍 Checking ZIPs in $ZIPDIR..." for zip in "$ZIPDIR"/*.zip; do [ -e "$zip" ] || continue ((checked++)) zipname=$(basename "$zip") log "DEBUG" "Processing: $zipname" # List files in zip romfile="" while IFS= read -r f; do f=$(echo "$f" | sed 's/^ *//;s/ *$//') if [[ -n "${ROM_MD5[$f]:-}" ]]; then romfile="$f" break fi done < <(unzip -Z1 "$zip") if [[ -z "$romfile" ]]; then log "WARN" "[DEL] $zipname: No expected ROM file found." echo "[DEL] $zipname: No expected ROM file found." if [[ "$DRY_RUN" == "true" ]]; then echo " [DRY RUN] Would delete: $zipname" else rm -f "$zip" && log "INFO" "Deleted: $zipname" || log "ERROR" "Failed to delete: $zipname" fi ((deleted++)) continue fi # Extract to temp and check hashes tmpdir="$(mktemp -d)" if ! unzip -qq -j "$zip" "$romfile" -d "$tmpdir"; then log "ERROR" "[ERR] $zipname: Failed to extract $romfile" echo "[ERR] $zipname: Failed to extract $romfile" ((errors++)) rm -rf "$tmpdir" continue fi md5_actual=$(md5sum "$tmpdir/$romfile" | awk '{print $1}') sha1_actual=$(sha1sum "$tmpdir/$romfile" | awk '{print $1}') md5_expected="${ROM_MD5[$romfile]}" sha1_expected="${ROM_SHA1[$romfile]}" if [[ "$md5_actual" != "$md5_expected" || "$sha1_actual" != "$sha1_expected" ]]; then log "WARN" "[DEL] $zipname: Hash mismatch for $romfile (MD5: $md5_actual/$md5_expected, SHA1: $sha1_actual/$sha1_expected)" echo "[DEL] $zipname: Hash mismatch for $romfile (MD5: $md5_actual/$md5_expected, SHA1: $sha1_actual/$sha1_expected)" if [[ "$DRY_RUN" == "true" ]]; then echo " [DRY RUN] Would delete: $zipname" else rm -f "$zip" && log "INFO" "Deleted: $zipname" || log "ERROR" "Failed to delete: $zipname" fi ((deleted++)) else log "INFO" "[OK ] $zipname: $romfile matches hashes." echo "[OK ] $zipname: $romfile matches hashes." ((kept++)) fi rm -rf "$tmpdir" done echo "" echo "════════════════════════════════════════════════════════════════" echo "📋 Summary:" echo " ZIPs checked: $checked" echo " ZIPs kept: $kept" echo " ZIPs deleted: $deleted" echo " Errors: $errors" echo "════════════════════════════════════════════════════════════════" log "INFO" "Summary: $checked checked, $kept kept, $deleted deleted, $errors errors" exit 0 # ...end of script...