#!/bin/bash
# AVL-MXe Minimal ISO Burner
# Logic: growisofs (dvd+rw-tools)
# Concept and Testing by Glen MacArthur, coded by Google Gemini 3

# 1. Input Check
ISO_IN="$1"
if [[ -z "$ISO_IN" || ! -f "$ISO_IN" ]]; then
    yad --error --text="Please select a valid .iso file." --width=300 --center
    exit 1
fi

# 2. BURN DIALOG
BURN_OPTS=$(yad --form \
    --title="Burn ISO to Disc Media" \
    --image="/usr/local/share/icons/custom/burner-app.png" \
    --window-icon="/usr/local/share/icons/custom/burner.png" \
    --width=450 --center \
    --text="<b>Burn ISO to Optical Media</b>\nFile: $(basename "$ISO_IN")" \
    --field="Device Path:" "/dev/sr0" \
    --field="DVD-Video Compatibility (Finalize):CHK" "TRUE" \
    --field="Slow Speed (4x) for Reliability:CHK" "TRUE" \
    --field="Eject when finished:CHK" "TRUE" \
    --field="Dummy Burn (Test Mode):CHK" "FALSE" \
    --button="Burn Disc":10 \
    --button="Cancel":1)

[[ $? -ne 10 ]] && exit 0

# Parse Fields
DEV=$(echo "$BURN_OPTS" | cut -d'|' -f1)
COMPAT=$(echo "$BURN_OPTS" | cut -d'|' -f2)
SLOW=$(echo "$BURN_OPTS" | cut -d'|' -f3)
EJECT=$(echo "$BURN_OPTS" | cut -d'|' -f4)
DUMMY=$(echo "$BURN_OPTS" | cut -d'|' -f5)

# 3. CONSTRUCT COMMAND
BURN_CMD="growisofs"

[[ "$COMPAT" == "TRUE" ]] && BURN_CMD+=" -dvd-compat"
[[ "$SLOW" == "TRUE" ]]   && BURN_CMD+=" -speed=4"
[[ "$DUMMY" == "TRUE" ]]  && BURN_CMD+=" -dry-run"

BURN_CMD+=" -Z $DEV='$ISO_IN'"

# 4. EXECUTION
terminology -T="Feelin' the Burn?" -e bash -c "echo 'Starting Burn Process...'; \
echo 'Command: $BURN_CMD'; \
$BURN_CMD; \
if [ \$? -eq 0 ]; then \
    echo -e '\nSUCCESS: Burn Complete!'; \
    [[ '$EJECT' == 'TRUE' ]] && eject $DEV; \
    sleep 3; \
else \
    echo -e '\nERROR: Burn Failed. Check media or drive.'; \
    read -p 'Press Enter to exit...'; \
fi"