#!/bin/bash
# Convert DVD structure to Compliant ISO
# Concept and Testing by Glen MacArthur, coded by Google Gemini 3

DIR_PATH="$1"
DIR_NAME=$(basename "$DIR_PATH")

# 1. Validation: Ensure a VIDEO_TS folder exists
if [ ! -d "$DIR_PATH/VIDEO_TS" ]; then
    yad --error --text="<b>Mastering Error</b>\n\nNo VIDEO_TS folder found in:\n<i>$DIR_NAME</i>" \
        --image="error" --width=400 --center --title="ISO Architect"
    exit 1
fi

# 2. DVD Compliance: Ensure AUDIO_TS exists (even if empty)
# Some authoring tools skip this, but strict DVD-Video spec requires it.
mkdir -p "$DIR_PATH/AUDIO_TS"

# 3. Setup Naming (Clean up spaces/chars for the 32-char Volume ID)
VOL_ID=$(echo "$DIR_NAME" | tr '[:lower:]' '[:upper:]' | sed 's/[^A-Z0-9_]/_/g' | cut -c 1-32)
OUTPUT_ISO="${DIR_PATH}_$(date +%H%M).iso"

# 4. Master the ISO
# -dvd-video handles the UDF/ISO9660 hybrid filesystem and file placement
terminology -T="Converting Folder Structure to ISO" -e bash -c "echo 'Mastering DVD-Video ISO...'; \
echo 'Volume Label: $VOL_ID'; \
echo 'Target: $(basename "$OUTPUT_ISO")'; \
echo '------------------------------------------------'; \
genisoimage -dvd-video -V '$VOL_ID' -o '$OUTPUT_ISO' '$DIR_PATH'; \
echo -e '\nCreation Complete!'; \
echo 'Your ISO is ready for archiving or burning.'; \
read -n 1"