#!/bin/bash

CONF="$HOME/.simplestream"
PLAYR="/usr/bin/mpv"
ICON="/usr/share/simplestream/menu/icons/simplestream.png"
B1=`cat $CONF/stations | awk 'NR==4{print $0}'`
B2=`cat $CONF/stations | awk 'NR==5{print $0}'`
PID=`ps -eaf | grep mpv | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1 
fi &
PID=`ps -eaf | grep simplestream | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1 
rm -f $CONF/.tmp > /dev/null 2>&1 
exec /usr/share/simplestream/menu/notify/"SimpleStream" 2>&1 |less &
$PLAYR --no-cache $B2 2>&1 |tee > $CONF/.tmp &
sleep 3
echo $B2 > $CONF/url | awk 'NR==1{print $0}'
while true; do
echo $B1 > $CONF/playing | awk 'NR==1{print $0}'
grep -E "icy-title" $CONF/.tmp| sed 's/^............//'| sed 's/(.*//'| sed 's/[[].*//' | tail -n1 >> $CONF/playing
TITLE=`cat $CONF/playing | awk 'NR==1{print $0}'`
sleep 2
`echo \
"$B1
"$TITLE"
" \
| awk '{print $0}' > $CONF/playing`
done
fi & 

