#!/bin/bash #set -x # Set file permissions - all generated files readable/writable by all 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 shared libraries source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" source "${PROJECT_ROOT}/core/utils.sh" # Ensure utils.sh is sourced source "${PROJECT_ROOT}/core/config-arrays.sh" source "${SCRIPT_DIR}/preflight-checks.sh" # Load pre-flight checks # Log the script name when it is called log "DEBUG" "Starting script: $(basename "$0")" # Function to display usage display_usage() { log "INFO" "Usage: $0 [-i|--interactive] [-d|--delete] [-s|--system ] [-f|--filter ]" log "INFO" " [-ss|--sourcesite ] [-sy|--sourcesystem ]" log "INFO" " [-l|--language] [-r|--region] [-k|--kind] [-u|--usa]" exit 1 } # Function to set up directories setup_directories() { log "DEBUG" "Setting up directories for system: $SYSTEM and filter: $FILTER" SOURCESYSTEMDIR="${SOURCEBASEDIR}/${SOURCESITE}/${SOURCESYSTEM}" FILTERSYSTEMDIR="${FILTERBASEDIR}/${SOURCESYSTEM}.${FILTER}" ORGSYSTEMDIR="${ORGBASEDIR}/${SYSTEM}.${FILTER}" STAGEFILTERDIR="${STAGEBASEDIR}/${FILTER}" STAGESYSTEMDIR="${STAGEFILTERDIR}/${SYSTEM}" SHAREFILTERDIR="${SHAREBASEDIR}/${FILTER}" SHARESYSTEMDIR="${SHAREFILTERDIR}/${SYSTEM}" } # Step 1: Copy Source to Filter Directory copy_source_to_filter() { prompt_user "STEP 1" "Copy Source to Filter Directory" "copy_files \"$SOURCESYSTEMDIR\" \"$FILTERSYSTEMDIR\"" } # Step 2: Filter ROMs to just the ones we want filter_roms() { prompt_user "STEP 2" "Filter ROMs to just the ones we want" "echo 'Manual: Use Rom Manager to filter ROMs in $FILTERSYSTEMDIR based on dat for $FILTER'; read -p 'Press [Enter] key when done or [Ctrl+C] to exit...'" } # Step 3: Delete Problematic ROMs - Second Filtering delete_problematic_roms() { # Handle files with the '-' separator (for deleting problematic ROMs) for file in "${EMULISTSDIR}/${SYSTEM}-"*; do if [ ! -f "$file" ]; then continue; fi filename=$(basename "$file") # Extract the part after $SYSTEM- and replace '-' with ' ' for display list_name="${filename#${SYSTEM}-}" display_name="${list_name//-/ }" # Prompt user to decide whether to run the delete script on filtered directory prompt_user "STEP 3 - Deleting problematic ROMs" "Delete ROMs from list file: $filename ($display_name)" "\"${SCRIPT_DIR}/delete-listfiles.sh\" \"$FILTERSYSTEMDIR\" \"$file\"" done } # Step 4: Copy Filtered ROMs to Organization Directory copy_filtered_to_org() { prompt_user "STEP 4" "Copy Filtered ROMs to Organization Directory" "copy_files \"$FILTERSYSTEMDIR\" \"$ORGSYSTEMDIR\"" } # Step 5: Execute System-Specific Commands execute_system_specific_commands() { # Check if a custom system script exists # Prefer central systems directory in project root local system_script="${PROJECT_ROOT}/systems/custom-${SYSTEM}.sh" if [ -f "$system_script" ] && [ -x "$system_script" ]; then # Found a custom script - prompt user to execute it prompt_user "STEP 5" "Execute system-specific commands for $SYSTEM" "execute_custom_system_script \"$system_script\"" else log "INFO" "STEP 5 - No custom script found for system: $SYSTEM (looked for: $system_script)" fi } # Helper function to execute the custom system script execute_custom_system_script() { local system_script="$1" log "INFO" "Executing system-specific commands for: $SYSTEM" log "INFO" "Found custom script for $SYSTEM: $system_script" # Export environment variables for the system script export SYSTEM export FILTER export ORGSYSTEMDIR export FILTERSYSTEMDIR export SOURCESYSTEMDIR export STAGESYSTEMDIR export SHARESYSTEMDIR export ROMGROOMER_SCRIPT_DIR="${SCRIPT_DIR}" # Execute the system-specific script if "$system_script"; then log "INFO" "Successfully executed custom script for $SYSTEM" else log "ERROR" "Custom script for $SYSTEM failed with exit code $?" fi } # Step 6: Organize ROMs into subdirectories organize_roms() { # Use the refactored location of organize-roms.sh under organizers/ local organize_script="${PROJECT_ROOT}/organizers/organize-roms.sh" if [ ! -f "$organize_script" ]; then log "ERROR" "organize-roms script not found at $organize_script" exit 1 fi # organize-roms.sh now uses environment variables for configuration # The build scripts (e.g., build-nes-usa-1g1r.sh) should set: # - KEEP_IN_ROOT_REGIONS # - KEEP_IN_ROOT_KINDS # - ORGANIZE_LANGUAGES_BY_SYMLINK # - LANGUAGE_EXCLUDE_REGIONS # # Legacy flags from command line are ignored (deprecated) log "DEBUG" "Calling organize-roms.sh with directory: $ORGSYSTEMDIR" prompt_user "STEP 6" "Organize ROMs into subdirectories" "$organize_script" "$ORGSYSTEMDIR" } # Step 7: Add Extra Games add_extra_games() { # Handle files with the '+' separator (for adding games from myrient source) for file in "${EMULISTSDIR}/${SYSTEM}+"*; do if [ ! -f "$file" ]; then continue; fi filename=$(basename "$file") # Extract the part after $SYSTEM+ and replace '-' with ' ' list_name="${filename#${SYSTEM}+}" dir_name="${list_name//-/ }" # For '+' delimiter, use SOURCESYSTEMDIR to get ROMs from myrient source prompt_user "STEP 7 - Adding extra games from myrient" "Add games from list file: $filename to directory: $dir_name" "\"${SCRIPT_DIR}/link-listfiles.sh\" \"$file\" \"$SOURCESYSTEMDIR\" \"${ORGSYSTEMDIR}/${dir_name}\"" done # Handle files with the '.' separator (for adding games from extra directory) for file in "${EMULISTSDIR}/${SYSTEM}."*; do if [ ! -f "$file" ]; then continue; fi filename=$(basename "$file") # Extract the part after $SYSTEM. and replace '-' with ' ' list_name="${filename#${SYSTEM}.}" dir_name="${list_name//-/ }" # For '.' delimiter, use extra directory specific to the system extra_system_dir="${SOURCEBASEDIR}/extra/${SYSTEM}" prompt_user "STEP 7 - Adding extra games from extra directory" "Add games from list file: $filename to directory: $dir_name" "\"${SCRIPT_DIR}/link-listfiles.sh\" \"$file\" \"$extra_system_dir\" \"${ORGSYSTEMDIR}/${dir_name}\"" done } # Step 8: Copy Organized Games to Stage Directory copy_organized_to_stage() { prompt_user "STEP 8" "Copy Organized Games to Stage Directory" "copy_files \"$ORGSYSTEMDIR\" \"$STAGESYSTEMDIR\"" } # Step 9: Set ROM Link set_rom_link() { log "INFO" "STEP 9 - Set ROM link" if [ "$INTERACTIVE" = true ]; then read -p "Press [Enter] to continue, [S] to skip..." choice if [ "$choice" = "S" ] || [ "$choice" = "s" ]; then log "INFO" "Skipping STEP 9" return fi fi if [ -L "$EMUROMSDIR" ]; then rm "$EMUROMSDIR" fi ln -s "$STAGEFILTERDIR" "$EMUROMSDIR" } # Step 10: Run AARM on new data run_aarm() { prompt_user "STEP 10" "Run AARM on new data" "echo 'Manual: Use AARM to generate metadata for $EMUROMSDIR/$SYSTEM'; read -p 'Press [Enter] key when done or [Ctrl+C] to exit...'" } # Step 11: Copy Staged Games to Share Directory copy_staged_to_share() { prompt_user "STEP 11" "Copy Staged Games to Share Directory" "copy_files \"$STAGESYSTEMDIR\" \"$SHARESYSTEMDIR\"" } # Main script execution main() { # Parse options while [[ "$#" -gt 0 ]]; do case $1 in -i | --interactive ) INTERACTIVE=true; log "DEBUG" "INTERACTIVE set to true" ;; -d | --delete ) DELETE=true; log "DEBUG" "DELETE set to true" ;; -s | --system ) SYSTEM="$2"; shift; log "DEBUG" "SYSTEM set to: $SYSTEM" ;; -f | --filter ) FILTER="$2"; shift; log "DEBUG" "FILTER set to: $FILTER" ;; -ss | --sourcesite ) SOURCESITE="$2"; shift; log "DEBUG" "SOURCESITE set to: $SOURCESITE" ;; -sy | --sourcesystem ) SOURCESYSTEM="$2"; shift; log "DEBUG" "SOURCESYSTEM set to: $SOURCESYSTEM" ;; -l | --language ) LANGUAGE=true; log "DEBUG" "LANGUAGE set to true" ;; -r | --region ) REGION=true; log "DEBUG" "REGION set to true" ;; -k | --kind ) KIND=true; log "DEBUG" "KIND set to true" ;; -u | --usa ) USA=true; log "DEBUG" "USA set to true" ;; * ) display_usage ;; esac shift done # Validate required parameters validate_parameters "$SYSTEM" "$FILTER" "$SOURCESITE" "$SOURCESYSTEM" # Run pre-flight checks log "INFO" "" if ! run_preflight_checks; then log "ERROR" "Pre-flight checks failed. Aborting ROM processing." exit 1 fi log "INFO" "" # Set up directories setup_directories # Execute steps copy_source_to_filter filter_roms delete_problematic_roms copy_filtered_to_org execute_system_specific_commands organize_roms add_extra_games copy_organized_to_stage set_rom_link run_aarm copy_staged_to_share log "INFO" "Processing completed successfully for system: $SYSTEM and filter: $FILTER" } # Run the main function main "$@"