#!/bin/bash

ICON="/usr/share/icons/hicolor/48x48/apps/click-radio.png"
CONF="$HOME/.config/clickradio"
STATION=$(ps aux | grep "mpv\|USER" |awk NR==2 | awk -F'/' '{print $NF}' | sed 's/....$//')

while true; do
sleep 3;
rm -f $CONF/sfplay;
echo "$STATION" >> $CONF/sfplay;
 second_line=$(sed -n '2p' $CONF/playing)
if [[ -z ${second_line//[[:space:]]/} ]]; then
echo "No Stream Information" > $CONF/sfplay;
fi
grep "Playing" $HOME/.config/clickradio/.tmp| sed 's/^.........//'| sed 's/(.*//'| sed 's/[[].*//'|tail -n1 > $CONF/url
grep "icy-title" $CONF/.tmp| sed 's/^............//'| sed 's/[[].*//'| sed -E 's/ \[[^]]*\]$//'| tail -n1 >> $CONF/sfplay;
cp $CONF/sfplay $CONF/playing;
done& 
sleep 3;
No_Stream=$(grep -e AO: $CONF/.tmp)
if [ $No_Stream = $No_Stream 2>/dev/null ]; then
python3 /usr/share/click-radio/menu/notify/not_playing.py
else
echo "Playing stream"
fi
echo "$(date +%s)" > "$CONF/started_at"
    while true; do
        start=$(cat "$CONF/started_at")
        now=$(date +%s)
        if (( now - start > 28800 )); then  # 8 hours
python3 /usr/share/click-radio/menu/notify/notify_shutdown.py &
/usr/share/click-radio/menu/app/kill  
            break
        fi
        sleep 300  # Check every 5 minutes
    done &



