#!/bin/bash # 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" # Log the script name when it is called log "DEBUG" "Starting script: $(basename "$0")" # Function to display usage usage() { log "INFO" "Usage: $0 [-d|--delete] [-r|--recursive ] ..." log "INFO" "Options:" log "INFO" " -d, --delete Delete the original zip file after conversion" log "INFO" " -r, --recursive Recursively find and convert zip files in the specified path" exit 1 } # Check if at least one argument is provided if [ "$#" -lt 1 ]; then usage fi # Initialize variables DELETE_ORIGINAL=false RECURSIVE=false RECURSIVE_PATH="" # Parse options while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in -d | --delete ) DELETE_ORIGINAL=true log "DEBUG" "DELETE_ORIGINAL set to true" ;; -r | --recursive ) RECURSIVE=true shift RECURSIVE_PATH=$1 log "DEBUG" "RECURSIVE set to true with path: $RECURSIVE_PATH" ;; * ) usage ;; esac shift done # Check if there are any zip files provided or a recursive path if [ "$RECURSIVE" = false ] && [ "$#" -lt 1 ]; then usage fi # Create a temporary directory TEMP_DIR=$(mktemp -d) log "DEBUG" "Created temporary directory: $TEMP_DIR" # Function to clean up temporary files cleanup() { log "DEBUG" "Cleaning up temporary directory: $TEMP_DIR" rm -rf "$TEMP_DIR" } # Trap to ensure cleanup is done on exit trap cleanup EXIT # Function to process zip files process_zip() { local zipfile=$1 local zipdir=$(dirname "$zipfile") # Get the directory of the zip file log "INFO" "Processing zip file: $zipfile" log "DEBUG" "Unzipping $zipfile to $TEMP_DIR" unzip -q "$zipfile" -d "$TEMP_DIR" if [ $? -ne 0 ]; then log "ERROR" "Failed to unzip $zipfile" exit 1 fi for cuefile in "$TEMP_DIR"/*.cue; do if [ -f "$cuefile" ]; then name=${cuefile%.cue} chd_name=$(basename "$name").chd # Get the base name of the CHD file log "DEBUG" "Converting $cuefile to CHD: $TEMP_DIR/$chd_name" chdman createcd -i "$cuefile" -o "$TEMP_DIR/$chd_name" --force if [ $? -ne 0 ]; then log "ERROR" "Failed to convert $cuefile to CHD" exit 1 fi # Move the CHD file to the original directory log "DEBUG" "Moving $TEMP_DIR/$chd_name to $zipdir/" mv "$TEMP_DIR/$chd_name" "$zipdir/" if [ $? -ne 0 ]; then log "ERROR" "Failed to move $TEMP_DIR/$chd_name to $zipdir/" exit 1 fi fi done # Delete the original zip file if the delete option is set if [ "$DELETE_ORIGINAL" = true ]; then log "DEBUG" "Deleting original zip file: $zipfile" rm -f "$zipfile" fi } # Process zip files recursively if the recursive option is set if [ "$RECURSIVE" = true ]; then log "INFO" "Recursively finding and converting zip files in: $RECURSIVE_PATH" find "$RECURSIVE_PATH" -type f -name "*.zip" | while read -r zipfile; do process_zip "$zipfile" done else # Process each zip file provided as an argument for zipfile in "$@"; do if [ -f "$zipfile" ]; then process_zip "$zipfile" else log "ERROR" "File $zipfile does not exist" exit 1 fi done fi # Clean up temporary files cleanup log "INFO" "Conversion completed successfully" exit 0