#!/bin/bash ################################################################################ # Script: debug-copy-source-with-prompt.sh # Description: Debug script to perform everything up through STEP 1 - Copy Source to Filter Directory. # This script includes the `prompt_user` function and is self-contained. # # Usage: ./debug-copy-source-with-prompt.sh # # Dependencies: None (self-contained) ################################################################################ # Set umask to 000 to ensure files are created with 666 permissions umask 000 # Logging function log() { local level=$1 local message=$2 local timestamp=$(date +"%Y-%m-%d %H:%M:%S") echo "[$timestamp] [$level] $message" } # Function to validate if a path is within EMUBASEDIR validate_path_within_emubasedir() { local path=$1 # Debug: Print the input path log "DEBUG" "Input path: $path" # Resolve the full path local resolved_path=$(realpath "$path" 2>&1) if [ $? -ne 0 ]; then log "ERROR" "Failed to resolve path: $resolved_path" exit 1 fi log "DEBUG" "Resolved path: $resolved_path" # Resolve the EMUBASEDIR path local emubasedir_resolved=$(realpath "$EMUBASEDIR" 2>&1) if [ $? -ne 0 ]; then log "ERROR" "Failed to resolve EMUBASEDIR: $emubasedir_resolved" exit 1 fi log "DEBUG" "Resolved EMUBASEDIR: $emubasedir_resolved" # Check if the resolved path is within EMUBASEDIR if [[ "$resolved_path" != "$emubasedir_resolved"* ]]; then log "ERROR" "Path is outside of EMUBASEDIR: $resolved_path" exit 1 fi log "DEBUG" "Path is within EMUBASEDIR: $resolved_path" } # Function to copy files from source to destination copy_files() { local src=$1 local dest=$2 # Debug: Print the source and destination paths log "DEBUG" "Source path: $src" log "DEBUG" "Destination path: $dest" # Validate paths validate_path_within_emubasedir "$src" validate_path_within_emubasedir "$dest" # Create the destination directory log "DEBUG" "Creating destination directory: $dest" mkdir -p "$dest" || { log "ERROR" "Failed to create directory: $dest"; exit 1; } # Copy files from source to destination log "DEBUG" "Copying files from $src to $dest" find "$src" -mindepth 1 -maxdepth 1 -print0 | while IFS= read -r -d '' file; do log "DEBUG" "Copying file: $file" cp -r "$file" "$dest/" || { log "ERROR" "Failed to copy file: $file"; continue; } done log "INFO" "Files copied successfully from $src to $dest" } # Function to prompt the user in interactive mode prompt_user() { local step=$1 local description=$2 local command=$3 if [ "$INTERACTIVE" = true ]; then log "INFO" "$step - $description" read -p "Press [Enter] to continue, [S] to skip..." choice if [ "$choice" = "S" ] || [ "$choice" = "s" ]; then log "INFO" "Skipping $step" return fi else log "INFO" "$step - $description" fi # Debug: Print the command before executing log "DEBUG" "Executing command: $command" # Execute the command using eval to preserve quotes eval "$command" || { log "ERROR" "Failed to execute command: $command"; exit 1; } } # Main script execution main() { # Configuration variables EMUBASEDIR="/data/emu" SOURCEBASEDIR="${EMUBASEDIR}/source" FILTERBASEDIR="${EMUBASEDIR}/filter" SOURCESITE="myrient.erista.me/files/No-Intro" SOURCESYSTEM="Nintendo - Nintendo Entertainment System (Headered)" SYSTEM="nes" FILTER="en.1g1r" INTERACTIVE=true # Enable interactive mode # Define source and destination paths SOURCESYSTEMDIR="${SOURCEBASEDIR}/${SOURCESITE}/${SOURCESYSTEM}" FILTERSYSTEMDIR="${FILTERBASEDIR}/${SOURCESYSTEM}.${FILTER}" # Debug: Print configuration log "DEBUG" "EMUBASEDIR: $EMUBASEDIR" log "DEBUG" "SOURCEBASEDIR: $SOURCEBASEDIR" log "DEBUG" "FILTERBASEDIR: $FILTERBASEDIR" log "DEBUG" "SOURCESITE: $SOURCESITE" log "DEBUG" "SOURCESYSTEM: $SOURCESYSTEM" log "DEBUG" "SYSTEM: $SYSTEM" log "DEBUG" "FILTER: $FILTER" log "DEBUG" "SOURCESYSTEMDIR: $SOURCESYSTEMDIR" log "DEBUG" "FILTERSYSTEMDIR: $FILTERSYSTEMDIR" # Step 1: Copy Source to Filter Directory prompt_user "STEP 1" "Copy Source to Filter Directory" "copy_files \"$SOURCESYSTEMDIR\" \"$FILTERSYSTEMDIR\"" log "INFO" "Script completed successfully" } # Run the main function main