#!/bin/bash ################################################################################ # Script: link-listfiles.sh # Description: Links ROMs listed in a file from a source directory to a target # directory. This is useful for adding extra games or translations # to a ROM set. # # Usage: ./link-listfiles.sh # Arguments: # Path to the file containing the list of ROMs to link. # Directory containing the source ROMs. # Directory where the ROMs will be linked. # # Dependencies: # - logger.sh for consistent and color-coded logging. # # Example: # ./link-listfiles.sh /path/to/listfile.txt /path/to/source /path/to/target ################################################################################ # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load logger from the script's directory source "${PROJECT_ROOT}/core/logger.sh" # Log the script name when it is called log "DEBUG" "Starting script: $(basename "$0")" # Function to display usage display_usage() { log "INFO" "Usage: $0 " log "INFO" "Arguments:" log "INFO" " Path to the file containing the list of ROMs to link." log "INFO" " Directory containing the source ROMs." log "INFO" " Directory where the ROMs will be linked." exit 1 } # Check if the correct number of arguments is provided if [ "$#" -ne 3 ]; then log "ERROR" "Incorrect number of arguments." display_usage fi # Assign arguments to variables listfile="$1" source_dir="$2" target_dir="$3" # Validate arguments if [ ! -f "$listfile" ]; then log "ERROR" "Listfile does not exist: $listfile" exit 1 fi if [ ! -d "$source_dir" ]; then log "ERROR" "Source directory does not exist: $source_dir" exit 1 fi if [ ! -d "$target_dir" ]; then log "INFO" "Target directory does not exist. Creating: $target_dir" mkdir -p "$target_dir" fi # Function to link ROMs from the listfile link_roms() { local listfile="$1" local source_dir="$2" local target_dir="$3" local total_files_in_list=0 local linked_files_count=0 local skipped_files_count=0 local not_found_count=0 log "INFO" "Linking ROMs from $listfile to $target_dir" # Read the listfile line by line while IFS= read -r rom; do # Skip empty lines if [ -z "$rom" ]; then continue fi total_files_in_list=$((total_files_in_list + 1)) # Construct the source and target paths source_path="${source_dir}/${rom}" target_path="${target_dir}/${rom}" # Check if the source ROM exists if [ ! -f "$source_path" ]; then log "WARN" "Source ROM not found: $source_path" not_found_count=$((not_found_count + 1)) continue fi # Create the target directory if it doesn't exist target_parent_dir=$(dirname "$target_path") if [ ! -d "$target_parent_dir" ]; then log "DEBUG" "Creating target directory: $target_parent_dir" mkdir -p "$target_parent_dir" fi # Create a symbolic link if [ -e "$target_path" ]; then log "WARN" "Target already exists: $target_path" skipped_files_count=$((skipped_files_count + 1)) else log "DEBUG" "Linking $source_path to $target_path" ln -s "$source_path" "$target_path" linked_files_count=$((linked_files_count + 1)) fi done < "$listfile" # Display the summary log "INFO" "Linking Summary:" log "INFO" " Files in link list: $total_files_in_list" log "INFO" " Files successfully linked: $linked_files_count" log "INFO" " Files skipped (already exist): $skipped_files_count" log "INFO" " Files not found in source: $not_found_count" } # Main script execution main() { log "INFO" "Starting ROM linking process" link_roms "$listfile" "$source_dir" "$target_dir" log "INFO" "ROM linking process completed" } # Run the main function only if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi