#!/bin/bash
#Simple CD Player using yad and mpv.
#Concept and Testing by Glen MacArthur, coded by Google Gemini 3.

PIPE="$HOME/.mpv-cd-socket"

# 1. Clean start
pkill -f "mpv cdda://"
rm -f "$PIPE"

# 2. Start the Engine (Backgrounded & Invisible)
mpv cdda:// --input-ipc-server="$PIPE" --idle=yes --no-terminal --no-video > /dev/null 2>&1 &

# 3. The Minimalist UI
# We just use a simple form with buttons. 
# No loops, no background watchers.
yad --title="CD Player" --window-icon="/usr/local/share/icons/custom/cdplayer.png" --width=360 --center \
    --text="<b>Audio CD Playing</b>" --text-align=center \
    --form --columns=3 \
    --field="!media-skip-backward!:FBTN" "bash -c 'echo \"add chapter -1\" | socat - \"$PIPE\"'" \
    --field="!media-playback-start!:FBTN" "bash -c 'echo \"cycle pause\" | socat - \"$PIPE\"'" \
    --field="!media-skip-forward!:FBTN" "bash -c 'echo \"add chapter 1\" | socat - \"$PIPE\"'" \
    --button="Eject:2" --button="Close:0"

# 4. Exit Logic
RET=$?
if [ $RET -eq 2 ]; then
    echo "stop" | socat - "$PIPE"
    eject
fi

# Kill the engine when the window is closed
pkill -f "mpv cdda://"
rm -f "$PIPE"