#!/bin/bash

ICON='/usr/share/icons/hicolor/48x48/apps/click-radio.png'
PLAYR='/usr/bin/mpv'
ECON='/usr/share/click-radio/icons/error.svg'
NCON='/usr/share/click-radio/icons/radio-notify.svg'
CONF="$HOME/.config/clickradio"

PID=`ps -eaf | grep "Click Radio" | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID &
fi &
PID=`ps -eaf | grep "mpv" | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID &
fi
PDIR=$(yad \
--title="Click Radio" \
--window-icon=$ICON \
--text="<b>Select music folder to play</b>" \
--file --directory \
--center \
--width=700 \
--height=450 \
--button="Cancel"!/usr/share/click-radio/icons/cancel.svg:3 \
--button="Ok"!/usr/share/click-radio/icons/ok.svg:0)
if [ $? = "3" ]; then
/usr/share/click-radio/menu/wayland/"Click Radio"
fi
cd "$PDIR"
FDIR="`find "$PDIR" -type f \( -name "*.mp3" -o -name "*.aac" -o -name "*.ogg" -o -name "*.pls" -o -name "*.m3u" -o -name "*.wav" -o -name "*.m4a" -o -name "*.flac" -o -name "*.opus" \)`"
if [[ $FDIR == "" ]]
then
exit 0
else
exec $@
fi
/usr/share/click-radio/menu/wayland/"Music Folder"  &
while [[ 1 ]]; do
b=( $PDIR );
n=("$PDIR"/*);
s=${n[$(($RANDOM%${#n[@]}))]};
$PLAYR --audio-display=no "$s" 2>&1 |less;
done
fi
exit 0


