#!/bin/bash # Function to display usage usage() { echo "Usage: $0 [-d|--delete] [-r|--recursive ] ..." echo "Options:" echo " -d, --delete Delete the original zip file after conversion" echo " -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 ;; -r | --recursive ) RECURSIVE=true shift RECURSIVE_PATH=$1 ;; * ) 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 WORK_DIR=$(mktemp -d) # Check if the temp dir was created successfully if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then echo "Could not create temp dir" exit 1 fi # Function to clean up temporary files cleanup() { rm -rf "$WORK_DIR" echo "Deleted temp working directory $WORK_DIR" } # Trap to ensure cleanup is done on exit trap cleanup EXIT # Function to process zip files process_zip() { local zipfile=$1 name=${zipfile%.zip} # Remove '.zip' from file name echo "Extracting: $name" unzip -q "$zipfile" -d "$WORK_DIR" if [ $? -ne 0 ]; then echo "Failed to unzip $zipfile" exit 1 fi /usr/local/bin/extract-xiso -r "$WORK_DIR/$name.iso" -d "$WORK_DIR" if [ $? -ne 0 ]; then echo "Failed to convert $name.iso" exit 1 fi mv "$WORK_DIR/$name.iso" . rm "$WORK_DIR/$name.iso.old" # Delete the original zip file if the delete option is set if [ "$DELETE_ORIGINAL" = true ]; then rm -f "$zipfile" fi } # Process zip files recursively if the recursive option is set if [ "$RECURSIVE" = true ]; then 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 echo "File $zipfile does not exist" exit 1 fi done fi # Clean up temporary files cleanup echo "Conversion completed successfully" exit 0