#!/bin/bash

lsusb | grep -q '040d:340b' && opts='--audio-device=pipewire/alsa_output.usb-FX-AUDIO_FX-AUDIO-D-802-00.analog-stereo'

Stations=("CBS" "MyTV" "CW" "ABC" "Outlaw" "MeTV" "True Crime" "StartTV" "MeToons" "PBS" "PBS World" "PBS Create" "PBS Kids" "NBC" "FOX" "The365" "Cozi" "Court" "Mystery")

printf "\n%s\n%s\n" "   Stations   " "--------------"
for i in ${!Stations[@]}; do
    printf "%3s%11s\n" "$(($i + 1)))" "${Stations[$i]}"
done; echo
(( "$1" % 1 + 1 )) &>/dev/null && STN="$1" || read -p "Select Station: " STN

case "$STN" in
    1) Channel="http://hdhomerun:5004/auto/v3.1" ;; # KDLO (CBS)
    2) Channel="http://hdhomerun:5004/auto/v3.2" ;; # UTV (MyTV)
    3) Channel="http://hdhomerun:5004/auto/v3.4" ;; # CW
    4) Channel="http://hdhomerun:5004/auto/v13.1" ;; # KSFY (ABC)
    5) Channel="http://hdhomerun:5004/auto/v13.2" ;; # Outlaw
    6) Channel="http://hdhomerun:5004/auto/v13.3" ;; # MeTV
    7) Channel="http://hdhomerun:5004/auto/v13.4" ;; # T-Crime 
    8) Channel="http://hdhomerun:5004/auto/v13.5" ;; # StartTV
    9) Channel="http://hdhomerun:5004/auto/v13.6" ;; # MeToons
    10) Channel="http://hdhomerun:5004/auto/v16.1" ;; # PBS
    11) Channel="http://hdhomerun:5004/auto/v16.2" ;; # PBS World
    12) Channel="http://hdhomerun:5004/auto/v16.3" ;; # PBS Create
    13) Channel="http://hdhomerun:5004/auto/v16.4" ;; # PBS Kids
    14) Channel="http://hdhomerun:5004/auto/v46.1" ;; # KDLT (NBC)
    15) Channel="http://hdhomerun:5004/auto/v46.2" ;; # FOX
    16) Channel="http://hdhomerun:5004/auto/v46.3" ;; # The365
    17) Channel="http://hdhomerun:5004/auto/v46.4" ;; # Cozi
    18) Channel="http://hdhomerun:5004/auto/v46.5" ;; # Court
    19) Channel="http://hdhomerun:5004/auto/v46.6" ;; # IONMyst
    *) exit ;;
esac

if [[ $DISPLAY ]]; then
    mpv $opts -fs "$Channel"
else
    trap "rm .xinitrc" EXIT
    echo "mpv $opts -fs $Channel" > ~/.xinitrc
    startx &> /dev/null
fi