#!/bin/bash ################################################################################ # Script: delete-listfiles.sh # Description: Deletes ROMs listed in a file from a specified directory. # # Usage: ./delete-listfiles.sh [--dry-run] # # Dependencies: # - utils.sh for shared utility functions. # - logger.sh for consistent and color-coded logging. ################################################################################ # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load shared libraries 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 DRY_RUN=false # Function to display usage display_usage() { log "INFO" "Usage: $0 [--dry-run]" log "INFO" "Arguments:" log "INFO" " Directory containing the ROMs to delete." log "INFO" " File containing the list of ROMs to delete." log "INFO" "Options:" log "INFO" " --dry-run Preview deletions without actually deleting files." log "INFO" "" log "INFO" "Examples:" log "INFO" " $0 /path/to/roms delete-list.txt # Delete files" log "INFO" " $0 /path/to/roms delete-list.txt --dry-run # Preview deletions" exit 1 } # Main script execution main() { local path="" local listfile="" # Parse arguments while [[ $# -gt 0 ]]; do case $1 in --dry-run) DRY_RUN=true log "DEBUG" "Dry-run mode enabled" shift ;; *) if [[ -z "$path" ]]; then path="$1" elif [[ -z "$listfile" ]]; then listfile="$1" else log "ERROR" "Too many arguments: $1" display_usage fi shift ;; esac done # Validate arguments if [[ -z "$path" || -z "$listfile" ]]; then log "ERROR" "Missing required arguments." display_usage fi # Validate directories and files validate_directory "$path" if [ ! -f "$listfile" ]; then log "ERROR" "Listfile does not exist: $listfile" exit 1 fi # Delete ROMs delete_roms "$path" "$listfile" if [[ "$DRY_RUN" == true ]]; then log "INFO" "ROM deletion preview completed (no files were actually deleted)" else log "INFO" "ROM deletion process completed" fi } # Function to delete ROMs listed in the file delete_roms() { local path=$1 local listfile=$2 local total_files_in_list=0 local total_files_in_path=0 local deleted_files_count=0 # Validate inputs if [ ! -d "$path" ]; then log "ERROR" "Directory does not exist: $path" return 1 fi if [ ! -f "$listfile" ]; then log "ERROR" "List file does not exist: $listfile" return 1 fi log "INFO" "Deleting ROMs listed in $listfile from $path" if [[ "$DRY_RUN" == true ]]; then log "INFO" "DRY-RUN MODE: Previewing file deletions (no files will be deleted)" fi # Read the list of files to delete mapfile -t files_to_delete < "$listfile" total_files_in_list=${#files_to_delete[@]} total_files_in_path=$(find "$path" -type f | wc -l) log "DEBUG" "Total files in listfile: $total_files_in_list" log "DEBUG" "Total files in path: $total_files_in_path" # Loop through the list of files and delete if they exist in the path for file in "${files_to_delete[@]}"; do # Trim whitespace from filename file=$(echo "$file" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') # Skip empty lines [ -z "$file" ] && continue file_path="$path/$file" if [ -f "$file_path" ]; then if [[ "$DRY_RUN" == true ]]; then log "INFO" "Would delete: $file_path" deleted_files_count=$((deleted_files_count + 1)) else log "DEBUG" "Deleting file: $file_path" rm "$file_path" || { log "ERROR" "Failed to delete file: $file_path"; continue; } deleted_files_count=$((deleted_files_count + 1)) fi else log "WARN" "File not found: $file_path" fi done # Display the summary if [[ "$DRY_RUN" == true ]]; then log "INFO" "Deletion Preview Summary:" log "INFO" " Files in delete list: $total_files_in_list" log "INFO" " Files that would be deleted: $deleted_files_count" log "INFO" " Files not found: $((total_files_in_list - deleted_files_count))" log "INFO" " Total files would remain in directory: $((total_files_in_path - deleted_files_count))" else log "INFO" "Deletion Summary:" log "INFO" " Files in delete list: $total_files_in_list" log "INFO" " Files found and deleted: $deleted_files_count" log "INFO" " Files not found: $((total_files_in_list - deleted_files_count))" log "INFO" " Total files remaining in directory: $((total_files_in_path - deleted_files_count))" fi } # Run the main function only if this script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi