#!/bin/bash # Script to convert old-style build scripts to new professional format # Usage: ./convert-build-scripts.sh SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Function to extract system info from old script command line extract_system_info() { local script_file="$1" # Extract the command line that calls the processor local cmd_line=$(grep -E "(sudo -u aubrey.*build-nointro-system|sudo -u aubrey.*build-redump)" "$script_file" | head -1) if [[ -z "$cmd_line" ]]; then echo "ERROR: Could not find processor call in $script_file" return 1 fi # Extract parameters using regex local system=$(echo "$cmd_line" | sed -n 's/.*-s \([^ ]*\).*/\1/p') local filter=$(echo "$cmd_line" | sed -n 's/.*-f \([^ ]*\).*/\1/p') local sourcesite=$(echo "$cmd_line" | sed -n 's/.*-ss \([^ ]*\).*/\1/p') local sourcesystem=$(echo "$cmd_line" | sed -n 's/.*-sy "\([^"]*\)".*/\1/p') # Determine processor type local processor="nointro-processor.sh" if [[ "$cmd_line" == *"build-redump"* ]]; then processor="redump-processor.sh" fi # Output the extracted info echo "SYSTEM=\"$system\"" echo "FILTER=\"$filter\"" echo "SOURCESITE=\"$sourcesite\"" echo "SOURCESYSTEM=\"$sourcesystem\"" echo "PROCESSOR=\"$processor\"" } # Function to determine language override get_language_override() { local filter="$1" if [[ "$filter" == *"eng"* ]]; then echo "En" elif [[ "$filter" == *"usa"* ]]; then echo "En" else echo "" fi } # Function to generate system description get_system_description() { local system="$1" local filter="$2" case "$system" in "nes") echo "Nintendo Entertainment System" ;; "snes") echo "Super Nintendo Entertainment System" ;; "gb") echo "Nintendo Game Boy" ;; "gbc") echo "Nintendo Game Boy Color" ;; "gba") echo "Nintendo Game Boy Advance" ;; "nds") echo "Nintendo DS" ;; "3ds") echo "Nintendo 3DS" ;; "n64") echo "Nintendo 64" ;; "mastersystem") echo "Sega Master System" ;; "megadrive") echo "Sega Genesis/Mega Drive" ;; "gamegear") echo "Sega Game Gear" ;; "sega32x") echo "Sega 32X" ;; "segacd") echo "Sega CD" ;; "saturn") echo "Sega Saturn" ;; "dreamcast") echo "Sega Dreamcast" ;; "sg1000") echo "Sega SG-1000" ;; "atari2600") echo "Atari 2600" ;; "atari5200") echo "Atari 5200" ;; "atari7800") echo "Atari 7800" ;; "jaguar") echo "Atari Jaguar" ;; "lynx") echo "Atari Lynx" ;; "psx") echo "Sony PlayStation" ;; "psp") echo "Sony PlayStation Portable" ;; "pspminis") echo "Sony PlayStation Portable Minis" ;; "3do") echo "3DO Interactive Multiplayer" ;; "colecovision") echo "ColecoVision" ;; "ngp") echo "Neo Geo Pocket" ;; "ngpc") echo "Neo Geo Pocket Color" ;; "pcengine") echo "PC Engine/TurboGrafx-16" ;; "pcenginecd") echo "PC Engine CD/TurboGrafx-CD" ;; "vectrex") echo "Vectrex" ;; "virtualboy") echo "Nintendo Virtual Boy" ;; *) echo "Unknown System ($system)" ;; esac } # Function to get filter description get_filter_description() { local filter="$1" if [[ "$filter" == *"usa"* ]]; then echo "USA region ROMs" elif [[ "$filter" == *"eng"* ]]; then echo "English region ROMs" elif [[ "$filter" == *"all"* ]]; then echo "all region ROMs" else echo "region-specific ROMs ($filter)" fi } # Function to convert a single script convert_script() { local script_file="$1" local script_name=$(basename "$script_file") echo "Converting $script_name..." # Extract system information local info=$(extract_system_info "$script_file") if [[ $? -ne 0 ]]; then echo "Skipping $script_name - could not extract info" return 1 fi # Parse extracted info eval "$info" local lang_override=$(get_language_override "$FILTER") local sys_desc=$(get_system_description "$SYSTEM" "$FILTER") local filter_desc=$(get_filter_description "$FILTER") # Generate the new script content cat > "$script_file" << EOF #!/bin/bash ################################################################################ # Script: $script_name # Description: Builds a 1G1R (One Game One ROM) set for the $sys_desc, # focusing on $filter_desc. This script sets up the # environment and calls the main processing script with the appropriate # parameters. # # Usage: ./$script_name # # Dependencies: # - $PROCESSOR # - config.sh # - logger.sh # - config-arrays.sh # # Example: # ./$script_name ################################################################################ # Set file permissions - all generated files readable/writable by all umask 000 # Determine the directory of the current script SCRIPT_DIR="\$(dirname "\$(realpath "\$0")")" # Load configuration and logger from the script's directory source "\${SCRIPT_DIR}/config.sh" source "\${SCRIPT_DIR}/logger.sh" source "\${SCRIPT_DIR}/config-arrays.sh" EOF # Add language override if needed if [[ -n "$lang_override" ]]; then cat >> "$script_file" << EOF # Override languageArray to only include $lang_override LANGUAGE_ARRAY_OVERRIDE="$lang_override" source "\${SCRIPT_DIR}/config-arrays.sh" # Re-source to apply the override EOF fi # Add the rest of the script cat >> "$script_file" << EOF # Log the script name when it is called log "DEBUG" "Starting script: \$(basename "\$0")" # Function to set up the environment set_environment() { SYSTEM="$SYSTEM" FILTER="$FILTER" SOURCESITE="$SOURCESITE" SOURCESYSTEM="$SOURCESYSTEM" log "DEBUG" "Environment set up for \$SYSTEM with filter \$FILTER" } # Function to run the processing script run_processing() { log "INFO" "Starting processing for \$SYSTEM with filter \$FILTER" "\${SCRIPT_DIR}/$PROCESSOR" -i -d -s "\$SYSTEM" -f "\$FILTER" -ss "\$SOURCESITE" -sy "\$SOURCESYSTEM" -l -r -k -u if [ \$? -ne 0 ]; then log "ERROR" "Processing failed for \$SYSTEM" exit 1 fi } # Function to display a summary display_summary() { log "INFO" "Processing completed successfully for \$SYSTEM" log "INFO" "Summary:" log "INFO" " - System: \$SYSTEM" log "INFO" " - Filter: \$FILTER" log "INFO" " - Source: \$SOURCESITE/\$SOURCESYSTEM" } # Main script execution main() { set_environment run_processing display_summary } # Run the main function main EOF echo "Converted $script_name successfully" } # Main execution echo "Starting conversion of build scripts..." # Find all old-style build scripts (those that still have sudo or direct calls) for script in /data/emu/rom-organizer/build-*.sh; do if [[ -f "$script" ]]; then # Skip if already converted (check for function-based structure) if grep -q "set_environment()" "$script" 2>/dev/null; then echo "Skipping $(basename "$script") - already converted" continue fi # Skip if doesn't have the old pattern if ! grep -q "sudo -u aubrey.*build-" "$script" 2>/dev/null; then echo "Skipping $(basename "$script") - no old pattern found" continue fi convert_script "$script" fi done echo "Conversion complete!"