#!/bin/bash ################################################################################ # Script: preflight-checks.sh # Description: Comprehensive pre-flight checks for ROM organizer system. # Validates dependencies, tools, directories, and configuration # before running the main processing workflow. # # Usage: source "${SCRIPT_DIR}/preflight-checks.sh" # Then call: run_preflight_checks # # Dependencies: # - config.sh # - logger.sh # - utils.sh ################################################################################ # Global variables for tracking check results PREFLIGHT_ERRORS=0 PREFLIGHT_WARNINGS=0 # Function to check if a command exists check_command() { local cmd="$1" local description="$2" local required="$3" # true/false if command -v "$cmd" >/dev/null 2>&1; then log "INFO" "✓ Found required tool: $cmd ($description)" return 0 else if [ "$required" = "true" ]; then log "ERROR" "✗ Missing required tool: $cmd ($description)" ((PREFLIGHT_ERRORS++)) return 1 else log "WARN" "⚠ Missing optional tool: $cmd ($description)" ((PREFLIGHT_WARNINGS++)) return 1 fi fi } # Function to check if a binary exists in EMUBINDIR check_binary() { local binary="$1" local description="$2" local required="$3" # true/false local binary_path="${EMUBINDIR}/${binary}" if [ -f "$binary_path" ] && [ -x "$binary_path" ]; then log "INFO" "✓ Found binary tool: $binary ($description)" return 0 else if [ "$required" = "true" ]; then log "ERROR" "✗ Missing required binary: $binary ($description)" log "ERROR" " Expected location: $binary_path" ((PREFLIGHT_ERRORS++)) return 1 else log "WARN" "⚠ Missing optional binary: $binary ($description)" log "WARN" " Expected location: $binary_path" ((PREFLIGHT_WARNINGS++)) return 1 fi fi } # Function to check directory exists and is writable check_directory() { local dir="$1" local description="$2" local required="$3" # true/false local create_if_missing="$4" # true/false if [ -d "$dir" ]; then if [ -w "$dir" ]; then log "INFO" "✓ Directory accessible: $dir ($description)" return 0 else log "ERROR" "✗ Directory not writable: $dir ($description)" ((PREFLIGHT_ERRORS++)) return 1 fi else if [ "$create_if_missing" = "true" ]; then log "INFO" "Creating missing directory: $dir ($description)" if mkdir -p "$dir" 2>/dev/null; then log "INFO" "✓ Created directory: $dir" return 0 else log "ERROR" "✗ Failed to create directory: $dir" ((PREFLIGHT_ERRORS++)) return 1 fi else if [ "$required" = "true" ]; then log "ERROR" "✗ Missing required directory: $dir ($description)" ((PREFLIGHT_ERRORS++)) return 1 else log "WARN" "⚠ Missing optional directory: $dir ($description)" ((PREFLIGHT_WARNINGS++)) return 1 fi fi fi } # Function to check configuration variable check_config_var() { local var_name="$1" local description="$2" local required="$3" # true/false local var_value="${!var_name}" if [ -n "$var_value" ]; then log "INFO" "✓ Configuration set: $var_name=$var_value ($description)" return 0 else if [ "$required" = "true" ]; then log "ERROR" "✗ Missing required configuration: $var_name ($description)" ((PREFLIGHT_ERRORS++)) return 1 else log "WARN" "⚠ Missing optional configuration: $var_name ($description)" ((PREFLIGHT_WARNINGS++)) return 1 fi fi } # Function to check system tools and dependencies check_system_dependencies() { log "INFO" "=== Checking System Dependencies ===" # Required system tools check_command "bash" "Bash shell" "true" check_command "find" "Find files utility" "true" check_command "cp" "Copy files utility" "true" check_command "mv" "Move files utility" "true" check_command "rm" "Remove files utility" "true" check_command "ln" "Link files utility" "true" check_command "mkdir" "Make directory utility" "true" check_command "realpath" "Resolve paths utility" "true" # Optional but recommended tools check_command "curl" "Download files from web" "false" check_command "wget" "Download files from web" "false" check_command "7z" "7-Zip archive utility" "false" check_command "unzip" "Extract ZIP archives" "false" check_command "rclone" "Cloud storage sync" "false" } # Function to check binary tools in EMUBINDIR check_binary_tools() { log "INFO" "=== Checking Binary Tools ===" # Check if bin directory exists check_directory "$EMUBINDIR" "Binary tools directory" "true" "true" # PS3 tools check_binary "PS3Dec" "PS3 disc decryption tool" "false" # Future tools will be added here as needed # check_binary "xbox_iso_converter" "Xbox ISO conversion tool" "false" # check_binary "chd_tool" "CHD conversion utility" "false" } # Function to check directory structure check_directory_structure() { log "INFO" "=== Checking Directory Structure ===" # Base directories (create if missing) check_directory "$EMUBASEDIR" "EMU base directory" "true" "true" check_directory "$SOURCEBASEDIR" "Source ROMs directory" "true" "true" check_directory "$FILTERBASEDIR" "Filtered ROMs directory" "true" "true" check_directory "$ORGBASEDIR" "Organized ROMs directory" "true" "true" check_directory "$STAGEBASEDIR" "Staged ROMs directory" "true" "true" check_directory "$SHAREBASEDIR" "Shared ROMs directory" "true" "true" # Configuration directories check_directory "$EMULISTSDIR" "List files directory" "true" "true" check_directory "$EXTRABASEDIR" "Extra ROMs directory" "true" "true" # ROM organizer script directory check_directory "$ROMGROOMERDIR" "ROM groomer scripts" "true" "false" check_directory "$ROMGROOMERDIR/systems" "System-specific scripts" "true" "true" } # Function to check configuration check_configuration() { log "INFO" "=== Checking Configuration ===" # Required configuration variables check_config_var "EMUBASEDIR" "EMU base directory path" "true" check_config_var "EMUBINDIR" "Binary tools directory path" "true" check_config_var "ROMGROOMERDIR" "ROM groomer directory path" "true" check_config_var "SOURCEBASEDIR" "Source directory path" "true" check_config_var "FILTERBASEDIR" "Filter directory path" "true" check_config_var "ORGBASEDIR" "Organization directory path" "true" check_config_var "STAGEBASEDIR" "Stage directory path" "true" check_config_var "SHAREBASEDIR" "Share directory path" "true" check_config_var "EMULISTSDIR" "Lists directory path" "true" check_config_var "EXTRABASEDIR" "Extra ROMs directory path" "true" # Optional configuration variables check_config_var "LOG_LEVEL" "Logging level" "false" check_config_var "EMUROMSDIR" "Current ROM link directory" "false" } # Function to check script dependencies check_script_dependencies() { log "INFO" "=== Checking Script Dependencies ===" # Accept scripts in new refactored layout: core/, bin/, tools/, organizers/ local search_roots=( "$ROMGROOMERDIR" "$ROMGROOMERDIR/core" "$ROMGROOMERDIR/bin" "$ROMGROOMERDIR/tools" "$ROMGROOMERDIR/organizers" ) local core_scripts=( "config.sh:Configuration file" "logger.sh:Logging utilities" "utils.sh:Utility functions" "nointro-processor.sh:Main processing script" "organize-roms.sh:ROM organization script" "delete-listfiles.sh:List-based deletion script" "link-listfiles.sh:List-based linking script" ) for script_info in "${core_scripts[@]}"; do local script="${script_info%%:*}" local description="${script_info##*:}" local found_path="" for root in "${search_roots[@]}"; do if [ -f "$root/$script" ]; then found_path="$root/$script" break fi done if [ -n "$found_path" ]; then log "INFO" "✓ Found script: $script ($description) at $found_path" else log "ERROR" "✗ Missing required script: $script ($description)" log "ERROR" " Searched in: ${search_roots[*]}" ((PREFLIGHT_ERRORS++)) fi done } # Function aliases for test compatibility check_tool() { check_command "$@" } check_directory_access() { check_directory "$@" } check_script_exists() { local script_name="$1" local description="$2" # Check if script exists in current directory or in core/ if [ -f "$script_name" ] || [ -f "core/$script_name" ]; then log "INFO" "✓ Found script: $script_name ($description)" return 0 else log "ERROR" "✗ Missing script: $script_name ($description)" return 1 fi } # Function to run all preflight checks run_preflight_checks() { log "INFO" "Starting pre-flight checks for ROM Organizer..." log "INFO" "" # Reset counters PREFLIGHT_ERRORS=0 PREFLIGHT_WARNINGS=0 # Run all check categories check_system_dependencies log "INFO" "" check_binary_tools log "INFO" "" check_directory_structure log "INFO" "" check_configuration log "INFO" "" check_script_dependencies log "INFO" "" # Summary log "INFO" "=== Pre-flight Check Summary ===" if [ $PREFLIGHT_ERRORS -eq 0 ]; then if [ $PREFLIGHT_WARNINGS -eq 0 ]; then log "INFO" "✅ All checks passed! System is ready for ROM processing." else log "WARN" "⚠️ $PREFLIGHT_WARNINGS warning(s) found, but system can proceed." log "WARN" " Consider addressing warnings for optimal functionality." fi return 0 else log "ERROR" "❌ $PREFLIGHT_ERRORS error(s) and $PREFLIGHT_WARNINGS warning(s) found." log "ERROR" " System is not ready for ROM processing." log "ERROR" " Please fix the errors above before proceeding." return 1 fi }