#!/bin/bash ################################################################################ # Script: setup-wizard.sh # Description: Interactive setup wizard for ROM Groomer first-time configuration. # Guides users through setting up directories, preferences, and # validating their system for optimal ROM collection management. # # Usage: ./setup-wizard.sh [--reset] # # Dependencies: # - config.sh # - logger.sh # - utils.sh # # Features: # - Interactive directory setup # - Preference configuration (regions, organization methods) # - System dependency validation # - Remote source configuration (optional) # - Configuration file generation # - Post-setup validation and testing # # Examples: # ./setup-wizard.sh # Run first-time setup # ./setup-wizard.sh --reset # Reset and reconfigure ################################################################################ # Script identification SCRIPT_NAME="setup-wizard.sh" # 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 core directory source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" source "${PROJECT_ROOT}/core/utils.sh" # Log the script name when it is called log "DEBUG" "Starting script: $(basename "$0")" # Global variables for wizard state WIZARD_CONFIG_FILE="${PROJECT_ROOT}/config-wizard.sh" RESET_MODE=false USER_EMUBASEDIR="" USER_PREFERRED_REGIONS=() USER_ORGANIZATION_METHOD="" USER_ENABLE_RCLONE="" USER_RCLONE_REMOTE="" # Colors for better user experience RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' BOLD='\033[1m' NC='\033[0m' # No Color # Function to display colored output print_color() { local color=$1 local message=$2 echo -e "${color}${message}${NC}" } # Function to display the welcome banner show_welcome() { clear print_color "$CYAN" "════════════════════════════════════════════════════════════════════════════════" print_color "$BOLD$CYAN" " ROM Groomer Setup Wizard" print_color "$CYAN" "════════════════════════════════════════════════════════════════════════════════" echo "" print_color "$GREEN" "Welcome to ROM Groomer! This wizard will help you set up your system for" print_color "$GREEN" "optimal ROM collection management." echo "" print_color "$YELLOW" "What this wizard will do:" echo " ✓ Configure your base directories" echo " ✓ Set up your preferred regions and organization methods" echo " ✓ Validate system dependencies" echo " ✓ Optionally configure remote ROM sources" echo " ✓ Test your configuration" echo "" print_color "$BLUE" "This will take about 5-10 minutes. Let's get started!" echo "" read -p "Press Enter to continue or Ctrl+C to exit..." echo "" } # Function to validate and prompt for directory input prompt_directory() { local prompt_text="$1" local default_value="$2" local directory="" while true; do echo -e "${YELLOW}${prompt_text}${NC}" if [[ -n "$default_value" ]]; then echo -e "Default: ${CYAN}${default_value}${NC}" fi read -p "Enter path: " directory # Use default if no input provided if [[ -z "$directory" && -n "$default_value" ]]; then directory="$default_value" fi # Validate directory if [[ -z "$directory" ]]; then print_color "$RED" "❌ Directory path cannot be empty." continue fi # Expand ~ to home directory directory="${directory/#\~/$HOME}" # Check if directory exists if [[ ! -d "$directory" ]]; then print_color "$YELLOW" "⚠️ Directory doesn't exist: $directory" read -p "Create it? (Y/n): " create_dir if [[ "$create_dir" =~ ^[Nn]$ ]]; then continue fi if mkdir -p "$directory" 2>/dev/null; then print_color "$GREEN" "✓ Created directory: $directory" else print_color "$RED" "❌ Failed to create directory: $directory" continue fi fi # Check if directory is writable if [[ ! -w "$directory" ]]; then print_color "$RED" "❌ Directory is not writable: $directory" continue fi print_color "$GREEN" "✓ Directory validated: $directory" echo "$directory" return 0 done } # Function to configure base directories configure_directories() { print_color "$BOLD$BLUE" "═══ Step 1: Directory Configuration ═══" echo "" print_color "$YELLOW" "ROM Groomer needs a base directory where all ROM processing will occur." print_color "$YELLOW" "This will contain subdirectories for source, filter, org, stage, and share." echo "" USER_EMUBASEDIR=$(prompt_directory "Enter your ROM Groomer base directory:" "${EMUBASEDIR:-/data/emu}") echo "" print_color "$GREEN" "✓ Base directory configured: $USER_EMUBASEDIR" echo "" print_color "$BLUE" "The following subdirectories will be created as needed:" echo " 📁 $USER_EMUBASEDIR/source/ - Raw ROM files and archives" echo " 📁 $USER_EMUBASEDIR/filter/ - Filtered ROM collections" echo " 📁 $USER_EMUBASEDIR/org/ - Organized ROM collections" echo " 📁 $USER_EMUBASEDIR/stage/ - Staged collections for deployment" echo " 📁 $USER_EMUBASEDIR/share/ - Final collections for distribution" echo " 📁 $USER_EMUBASEDIR/lists/ - ROM list files" echo " 📁 $USER_EMUBASEDIR/temp/ - Temporary processing files" echo "" read -p "Press Enter to continue..." } # Function to configure region preferences configure_regions() { print_color "$BOLD$BLUE" "═══ Step 2: Region Preferences ═══" echo "" print_color "$YELLOW" "ROM Groomer supports different regional preferences for ROM selection." print_color "$YELLOW" "Choose your preferred regions in order of priority:" echo "" local regions=("USA" "English" "All Regions") local region_codes=("usa" "eng" "all") echo "Available regions:" for i in "${!regions[@]}"; do echo " $((i+1)). ${regions[i]} (${region_codes[i]})" done echo "" while true; do read -p "Select your primary region preference (1-3): " choice case $choice in 1) USER_PREFERRED_REGIONS=("usa" "eng" "all") print_color "$GREEN" "✓ Primary: USA → English → All Regions" break ;; 2) USER_PREFERRED_REGIONS=("eng" "usa" "all") print_color "$GREEN" "✓ Primary: English → USA → All Regions" break ;; 3) USER_PREFERRED_REGIONS=("all" "eng" "usa") print_color "$GREEN" "✓ Primary: All Regions → English → USA" break ;; *) print_color "$RED" "❌ Invalid choice. Please select 1, 2, or 3." ;; esac done echo "" read -p "Press Enter to continue..." } # Function to configure organization method configure_organization() { print_color "$BOLD$BLUE" "═══ Step 3: Organization Method ═══" echo "" print_color "$YELLOW" "Choose how you want ROM collections to be organized:" echo "" echo "Organization methods:" echo " 1. By Region - Organize ROMs into regional subdirectories (USA/, Europe/, etc.)" echo " 2. By Language - Organize ROMs by language (English/, Japanese/, etc.)" echo " 3. Flat - Keep all ROMs in a single directory" echo " 4. Mixed - Let me choose per system" echo "" while true; do read -p "Select organization method (1-4): " choice case $choice in 1) USER_ORGANIZATION_METHOD="region" print_color "$GREEN" "✓ Organization: By Region" break ;; 2) USER_ORGANIZATION_METHOD="language" print_color "$GREEN" "✓ Organization: By Language" break ;; 3) USER_ORGANIZATION_METHOD="flat" print_color "$GREEN" "✓ Organization: Flat (single directory)" break ;; 4) USER_ORGANIZATION_METHOD="mixed" print_color "$GREEN" "✓ Organization: Mixed (choose per system)" break ;; *) print_color "$RED" "❌ Invalid choice. Please select 1, 2, 3, or 4." ;; esac done echo "" read -p "Press Enter to continue..." } # Function to check system dependencies check_dependencies() { print_color "$BOLD$BLUE" "═══ Step 4: System Dependencies ═══" echo "" print_color "$YELLOW" "Checking required and optional tools..." echo "" local all_good=true # Check required tools print_color "$BOLD" "Required tools:" local required_tools=("bash" "find" "sort" "uniq" "mkdir" "cp" "mv" "rm") for tool in "${required_tools[@]}"; do if command -v "$tool" >/dev/null 2>&1; then print_color "$GREEN" " ✓ $tool" else print_color "$RED" " ❌ $tool (REQUIRED)" all_good=false fi done echo "" print_color "$BOLD" "Optional tools:" # Check optional tools local optional_tools=("7z" "unzip" "rclone" "chdman") local tool_descriptions=( "7z - Archive handling (7-Zip)" "unzip - ZIP archive extraction" "rclone - Remote file management" "chdman - CHD format conversion" ) for i in "${!optional_tools[@]}"; do local tool="${optional_tools[i]}" local desc="${tool_descriptions[i]}" if command -v "$tool" >/dev/null 2>&1; then print_color "$GREEN" " ✓ $desc" else print_color "$YELLOW" " ⚠️ $desc (optional)" fi done echo "" if [[ "$all_good" == true ]]; then print_color "$GREEN" "✓ All required dependencies are available!" else print_color "$RED" "❌ Some required tools are missing. Please install them before continuing." echo "" print_color "$YELLOW" "On Ubuntu/Debian: sudo apt-get install bash findutils coreutils" echo "" read -p "Continue anyway? (y/N): " continue_anyway if [[ ! "$continue_anyway" =~ ^[Yy]$ ]]; then exit 1 fi fi echo "" read -p "Press Enter to continue..." } # Function to configure remote sources (optional) configure_remote_sources() { print_color "$BOLD$BLUE" "═══ Step 5: Remote Sources (Optional) ═══" echo "" print_color "$YELLOW" "ROM Groomer can work with remote ROM sources using rclone." print_color "$YELLOW" "This is useful for accessing ROM archives stored on cloud services." echo "" if ! command -v rclone >/dev/null 2>&1; then print_color "$YELLOW" "⚠️ rclone is not installed. Skipping remote source configuration." USER_ENABLE_RCLONE="no" echo "" read -p "Press Enter to continue..." return fi read -p "Do you want to configure remote ROM sources? (y/N): " configure_rclone if [[ "$configure_rclone" =~ ^[Yy]$ ]]; then USER_ENABLE_RCLONE="yes" echo "" print_color "$BLUE" "Available rclone remotes:" local remotes mapfile -t remotes < <(rclone listremotes 2>/dev/null | sed 's/:$//') if [[ ${#remotes[@]} -eq 0 ]]; then print_color "$YELLOW" "No rclone remotes configured yet." echo "" print_color "$BLUE" "You can configure rclone remotes later using: rclone config" USER_RCLONE_REMOTE="" else echo "Configured remotes:" for i in "${!remotes[@]}"; do echo " $((i+1)). ${remotes[i]}" done echo "" read -p "Select a remote to use as default (or press Enter to skip): " remote_choice if [[ -n "$remote_choice" && "$remote_choice" =~ ^[0-9]+$ && "$remote_choice" -ge 1 && "$remote_choice" -le ${#remotes[@]} ]]; then USER_RCLONE_REMOTE="${remotes[$((remote_choice-1))]}" print_color "$GREEN" "✓ Default remote: $USER_RCLONE_REMOTE" else USER_RCLONE_REMOTE="" print_color "$BLUE" "No default remote selected." fi fi else USER_ENABLE_RCLONE="no" print_color "$BLUE" "Remote sources disabled." fi echo "" read -p "Press Enter to continue..." } # Function to generate configuration generate_configuration() { print_color "$BOLD$BLUE" "═══ Step 6: Generate Configuration ═══" echo "" print_color "$YELLOW" "Generating your custom configuration..." echo "" # Create the wizard configuration file cat > "$WIZARD_CONFIG_FILE" << EOF #!/bin/bash ################################################################################ # ROM Groomer Configuration - Generated by Setup Wizard # Generated on: $(date) ################################################################################ # Base directory for all ROM operations export EMUBASEDIR="$USER_EMUBASEDIR" # Preferred regions in order of priority export PREFERRED_REGIONS=($(printf '"%s" ' "${USER_PREFERRED_REGIONS[@]}")) # Default organization method export DEFAULT_ORGANIZATION_METHOD="$USER_ORGANIZATION_METHOD" # Remote source configuration export ENABLE_RCLONE="$USER_ENABLE_RCLONE" export DEFAULT_RCLONE_REMOTE="$USER_RCLONE_REMOTE" # Directory structure export SOURCEDIR="\${EMUBASEDIR}/source" export FILTERDIR="\${EMUBASEDIR}/filter" export ORGDIR="\${EMUBASEDIR}/org" export STAGEDIR="\${EMUBASEDIR}/stage" export SHAREDIR="\${EMUBASEDIR}/share" export LISTSDIR="\${EMUBASEDIR}/lists" export TEMPDIR="\${EMUBASEDIR}/temp" # Create directories if they don't exist mkdir -p "\$SOURCEDIR" "\$FILTERDIR" "\$ORGDIR" "\$STAGEDIR" "\$SHAREDIR" "\$LISTSDIR" "\$TEMPDIR" # Logging configuration export LOG_LEVEL="INFO" export LOG_COLOR="true" # Default processing options export DELETE=false # Safety first - don't delete by default EOF chmod 644 "$WIZARD_CONFIG_FILE" print_color "$GREEN" "✓ Configuration generated: $WIZARD_CONFIG_FILE" echo "" # Create required directories print_color "$YELLOW" "Creating directory structure..." local dirs=("$USER_EMUBASEDIR/source" "$USER_EMUBASEDIR/filter" "$USER_EMUBASEDIR/org" "$USER_EMUBASEDIR/stage" "$USER_EMUBASEDIR/share" "$USER_EMUBASEDIR/lists" "$USER_EMUBASEDIR/temp") for dir in "${dirs[@]}"; do if mkdir -p "$dir" 2>/dev/null; then print_color "$GREEN" " ✓ Created: $dir" else print_color "$RED" " ❌ Failed to create: $dir" fi done echo "" read -p "Press Enter to continue..." } # Function to test configuration test_configuration() { print_color "$BOLD$BLUE" "═══ Step 7: Test Configuration ═══" echo "" print_color "$YELLOW" "Testing your configuration..." echo "" # Source the generated configuration if source "$WIZARD_CONFIG_FILE" 2>/dev/null; then print_color "$GREEN" "✓ Configuration file loads successfully" else print_color "$RED" "❌ Configuration file has errors" return 1 fi # Test directory access local test_dirs=("$SOURCEDIR" "$FILTERDIR" "$ORGDIR" "$STAGEDIR" "$SHAREDIR" "$LISTSDIR" "$TEMPDIR") local all_dirs_ok=true for dir in "${test_dirs[@]}"; do if [[ -d "$dir" && -w "$dir" ]]; then print_color "$GREEN" " ✓ Directory accessible: $(basename "$dir")" else print_color "$RED" " ❌ Directory issue: $dir" all_dirs_ok=false fi done # Test creating a temporary file local test_file="$TEMPDIR/wizard-test-$$" if echo "test" > "$test_file" 2>/dev/null && [[ -f "$test_file" ]]; then print_color "$GREEN" "✓ File operations working" rm -f "$test_file" else print_color "$RED" "❌ File operations failed" all_dirs_ok=false fi echo "" if [[ "$all_dirs_ok" == true ]]; then print_color "$GREEN" "✅ All tests passed! Your configuration is ready." else print_color "$RED" "❌ Some tests failed. Please check directory permissions." return 1 fi echo "" read -p "Press Enter to continue..." } # Function to show completion summary show_completion() { clear print_color "$CYAN" "════════════════════════════════════════════════════════════════════════════════" print_color "$BOLD$GREEN" " 🎉 Setup Complete! 🎉" print_color "$CYAN" "════════════════════════════════════════════════════════════════════════════════" echo "" print_color "$GREEN" "ROM Groomer has been successfully configured!" echo "" print_color "$BOLD" "Configuration Summary:" echo " 📁 Base Directory: $USER_EMUBASEDIR" echo " 🌍 Preferred Regions: ${USER_PREFERRED_REGIONS[*]}" echo " 📋 Organization: $USER_ORGANIZATION_METHOD" echo " ☁️ Remote Sources: $USER_ENABLE_RCLONE" if [[ "$USER_ENABLE_RCLONE" == "yes" && -n "$USER_RCLONE_REMOTE" ]]; then echo " 📡 Default Remote: $USER_RCLONE_REMOTE" fi echo "" print_color "$YELLOW" "Configuration saved to: $WIZARD_CONFIG_FILE" echo "" print_color "$BOLD$BLUE" "Next Steps:" echo " 1. To use this configuration, source it in your scripts:" echo " source $WIZARD_CONFIG_FILE" echo "" echo " 2. Or copy settings to your main config.sh file" echo "" echo " 3. Try organizing some ROMs:" echo " ./build-nes-usa-1g1r.sh # Build a NES collection" echo " ./sort2folders.sh [dir] # Organize ROMs into folders" echo "" echo " 4. Check the documentation:" echo " cat README.md # Project overview" echo " cat SCRIPTS.md # Available scripts" echo "" print_color "$GREEN" "Welcome to ROM Groomer! Happy ROM organizing! 🎮" echo "" } # Function to handle wizard reset handle_reset() { if [[ "$RESET_MODE" == true ]]; then print_color "$YELLOW" "⚠️ Reset mode: This will overwrite existing configuration." echo "" read -p "Continue with reset? (y/N): " confirm_reset if [[ ! "$confirm_reset" =~ ^[Yy]$ ]]; then print_color "$BLUE" "Reset cancelled." exit 0 fi echo "" fi } # Function to display usage show_usage() { echo "Usage: $0 [--reset]" echo "" echo "ROM Groomer Interactive Setup Wizard" echo "" echo "Options:" echo " --reset Reset and reconfigure existing setup" echo " --help Show this help message" echo "" echo "This wizard will guide you through setting up ROM Groomer for first-time use." } # Main wizard function main() { # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --reset) RESET_MODE=true shift ;; --help|-h) show_usage exit 0 ;; *) print_color "$RED" "Unknown option: $1" show_usage exit 1 ;; esac done # Handle reset mode handle_reset # Run the wizard steps show_welcome configure_directories configure_regions configure_organization check_dependencies configure_remote_sources generate_configuration if test_configuration; then show_completion else print_color "$RED" "❌ Setup incomplete due to configuration errors." print_color "$YELLOW" "Please fix the issues and run the wizard again." exit 1 fi } # Show usage if called with --help even in non-interactive mode if [[ "$1" == "--help" || "$1" == "-h" ]]; then show_usage exit 0 fi # Show usage if called without proper terminal (except for help) if [[ ! -t 0 ]]; then show_usage exit 1 fi # Run the main function with all arguments main "$@"