#!/bin/bash

CONF="$HOME/.simplestream"
PLAYR="/usr/bin/mpv"
ICON="/usr/share/simplestream/menu/icons/simplestream.png"
A1=`cat $CONF/stations | awk 'NR==1{print $0}'`
A2=`cat $CONF/stations | awk 'NR==2{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 $A2 2>&1 |tee > $CONF/.tmp &
sleep 3
echo $A2 > $CONF/url | awk 'NR==1{print $0}'
while true; do
echo $A1 > $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 \
"$A1
"$TITLE"
" \
| awk '{print $0}' > $CONF/playing`
done
fi &

