#!/bin/bash ################################################################################ # System-Specific Script: custom-saturn.sh # Description: Custom processing commands for SATURN ROMs # # This script is called during Step 5 of the ROM organization workflow. # It receives the following environment variables: # - SYSTEM: The system name (saturn) # - FILTER: The filter type (e.g., all, usa, english) # - ORGSYSTEMDIR: Path to the organization directory for this system # - FILTERSYSTEMDIR: Path to the filtered ROMs directory # - SOURCESYSTEMDIR: Path to the source ROMs directory # # Usage: This script is automatically called by nointro-processor.sh ################################################################################ # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Resolve project root (parent of tools/ or core/) if [ -n "$ROMGROOMER_SCRIPT_DIR" ]; then PROJECT_ROOT="$(dirname "$ROMGROOMER_SCRIPT_DIR")" else PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" fi # Source core libraries only if not already loaded by the caller if [ -z "$CONFIG_LOADED" ]; then if [ -f "$PROJECT_ROOT/core/config.sh" ]; then # shellcheck source=/dev/null source "$PROJECT_ROOT/core/config.sh" else echo "[custom-saturn] ERROR: core/config.sh not found at $PROJECT_ROOT/core/config.sh" >&2 exit 1 fi fi if [ -z "$LOGGER_LOADED" ]; then if [ -f "$PROJECT_ROOT/core/logger.sh" ]; then # shellcheck source=/dev/null source "$PROJECT_ROOT/core/logger.sh" else echo "[custom-saturn] ERROR: core/logger.sh not found at $PROJECT_ROOT/core/logger.sh" >&2 exit 1 fi fi if ! declare -F log >/dev/null 2>&1; then echo "[custom-saturn] ERROR: log function unavailable after sourcing logger.sh" >&2 exit 1 fi if [ -z "$UTILS_LOADED" ]; then if [ -f "$PROJECT_ROOT/core/utils.sh" ]; then # Mark utils loaded after sourcing # shellcheck source=/dev/null source "$PROJECT_ROOT/core/utils.sh" UTILS_LOADED=true else echo "[custom-saturn] ERROR: core/utils.sh not found at $PROJECT_ROOT/core/utils.sh" >&2 exit 1 fi fi # Log the script execution log "INFO" "Executing custom commands for SATURN system" log "DEBUG" "SYSTEM: $SYSTEM" log "DEBUG" "FILTER: $FILTER" log "DEBUG" "ORGSYSTEMDIR: $ORGSYSTEMDIR" # TODO: Add your SATURN-specific processing here # Examples of what you might want to do: # - Convert ROM formats (e.g., ZIP to CHD for disc-based games) # - Validate multi-disc games are properly grouped # - Apply region-specific patches or fixes # - Handle system-specific file naming conventions # - Process system-specific metadata ############################### # Configuration Flags (env overridable) ############################### SATURN_CONVERT_TO_CHD=${SATURN_CONVERT_TO_CHD:-true} SATURN_DELETE_ZIPS=${SATURN_DELETE_ZIPS:-true} SATURN_SKIP_IF_CHD_PRESENT=${SATURN_SKIP_IF_CHD_PRESENT:-true} # Verbose listing of archive contents (set to true to see unzip -l output) SATURN_VERBOSE=${SATURN_VERBOSE:-false} # Generate multi-disc playlists after conversion (disc markers only) SATURN_GENERATE_M3U=${SATURN_GENERATE_M3U:-true} SATURN_M3U_FORCE=${SATURN_M3U_FORCE:-false} SATURN_M3U_VERBOSE=${SATURN_M3U_VERBOSE:-false} CONVERTER_SCRIPT="${ROMGROOMER_SCRIPT_DIR:-$SCRIPT_DIR}/convertFromZipCueToChd.sh" if [ ! -x "$CONVERTER_SCRIPT" ]; then # Fallback to bin/ if [ -x "${ROMGROOMER_SCRIPT_DIR%/tools}/bin/convertFromZipCueToChd.sh" ]; then CONVERTER_SCRIPT="${ROMGROOMER_SCRIPT_DIR%/tools}/bin/convertFromZipCueToChd.sh" fi fi perform_conversion=true conversion_failed=0 converted_count=0 if [ "$SATURN_CONVERT_TO_CHD" != "true" ]; then perform_conversion=false log "INFO" "SATURN: CHD conversion disabled (SATURN_CONVERT_TO_CHD=$SATURN_CONVERT_TO_CHD)" fi if $perform_conversion; then if ! command -v chdman >/dev/null 2>&1; then log "ERROR" "SATURN: chdman not found; cannot convert to CHD" perform_conversion=false fi fi if $perform_conversion && [ ! -x "$CONVERTER_SCRIPT" ]; then log "ERROR" "SATURN: CHD converter script not found or not executable: $CONVERTER_SCRIPT" perform_conversion=false fi if $perform_conversion && [ "$SATURN_SKIP_IF_CHD_PRESENT" = true ] && find "$ORGSYSTEMDIR" -maxdepth 1 -type f -name '*.chd' | grep -q .; then log "INFO" "SATURN: Existing CHDs detected and SATURN_SKIP_IF_CHD_PRESENT=true; skipping conversion" perform_conversion=false fi if $perform_conversion; then mapfile -t candidate_zips < <(find "$ORGSYSTEMDIR" -maxdepth 1 -type f -name '*.zip') if [ ${#candidate_zips[@]} -eq 0 ]; then log "INFO" "SATURN: No ZIP archives found in $ORGSYSTEMDIR to convert" perform_conversion=false else log "INFO" "SATURN: Evaluating ${#candidate_zips[@]} ZIP files for CHD conversion" to_convert=() for z in "${candidate_zips[@]}"; do if unzip -Z1 "$z" 2>/dev/null | grep -qi '\.cue$'; then [ "$SATURN_VERBOSE" = true ] && log "DEBUG" "SATURN: Found CUE in $(basename "$z")" to_convert+=("$z") else log "DEBUG" "SATURN: Skip (no .cue inside): $(basename "$z")" fi done if [ ${#to_convert[@]} -eq 0 ]; then log "INFO" "SATURN: No ZIP+CUE sets detected requiring conversion" perform_conversion=false else log "INFO" "SATURN: Converting ${#to_convert[@]} archive(s) to CHD" DELETE_FLAG="" [ "$SATURN_DELETE_ZIPS" = true ] && DELETE_FLAG="-d" for z in "${to_convert[@]}"; do log "INFO" "SATURN: Converting $(basename "$z")" if ! "$CONVERTER_SCRIPT" $DELETE_FLAG "$z"; then log "ERROR" "SATURN: Conversion failed for $(basename "$z")" conversion_failed=1 break fi converted_count=$((converted_count+1)) done if [ $conversion_failed -ne 0 ]; then log "ERROR" "SATURN: One or more conversions failed" else log "INFO" "SATURN: CHD conversion complete ($converted_count/${#to_convert[@]} succeeded)" fi fi fi fi ############################### # Generate M3U Playlists (always attempt if enabled) ############################### if [ "$SATURN_GENERATE_M3U" = true ]; then GEN_SCRIPT="$PROJECT_ROOT/tools/generate-disc-playlists.sh" if [ -x "$GEN_SCRIPT" ]; then log "INFO" "SATURN: Generating disc multi-disc .m3u playlists (disc markers only)" PLAYLIST_ROOT="$ORGSYSTEMDIR" \ PLAYLIST_FORCE=$([ "$SATURN_M3U_FORCE" = true ] && echo 1 || echo 0) \ PLAYLIST_VERBOSE=$([ "$SATURN_M3U_VERBOSE" = true ] && echo 1 || echo 0) \ "$GEN_SCRIPT" || log "ERROR" "SATURN: disc playlist generator encountered an error" else log "WARN" "SATURN: disc playlist generator not found at $GEN_SCRIPT (skipping)" fi else log "INFO" "SATURN: M3U generation disabled (SATURN_GENERATE_M3U=$SATURN_GENERATE_M3U)" fi # Exit code: 0 if no conversion failures, else 1 if [ $conversion_failed -ne 0 ]; then exit 1 fi exit 0