#!/bin/bash

ICON='/usr/share/simplestream/menu/icons/simplestream.png'
CONF="$HOME/.simplestream"
FILE="$HOME/.simplestream/stations"

PID=`ps -eaf | grep mpv | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1 | sleep 0.50 
fi &
PID=`ps -eaf | grep simplestream | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
kill -9 $PID > /dev/null 2>&1 &

# Get all the info from stations file, print it.
A1=`cat $CONF/stations | awk 'NR==1{print $0}'`
A2=`cat $CONF/stations | awk 'NR==2{print $0}'`
B1=`cat $CONF/stations | awk 'NR==4{print $0}'`
B2=`cat $CONF/stations | awk 'NR==5{print $0}'`
C1=`cat $CONF/stations | awk 'NR==7{print $0}'`
C2=`cat $CONF/stations | awk 'NR==8{print $0}'`
D1=`cat $CONF/stations | awk 'NR==10{print $0}'`
D2=`cat $CONF/stations | awk 'NR==11{print $0}'`
E1=`cat $CONF/stations | awk 'NR==13{print $0}'`
E2=`cat $CONF/stations | awk 'NR==14{print $0}'`
F1=`cat $CONF/stations | awk 'NR==16{print $0}'`
F2=`cat $CONF/stations | awk 'NR==17{print $0}'`
G1=`cat $CONF/stations | awk 'NR==19{print $0}'`
G2=`cat $CONF/stations | awk 'NR==20{print $0}'`
H1=`cat $CONF/stations | awk 'NR==22{print $0}'`
H2=`cat $CONF/stations | awk 'NR==23{print $0}'`

# Make personal URL changes from default ones.
MENU=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=450 \
--center \
--entry \
--button="Reset!/usr/share/simplestream/menu/icons/add.svg:6" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Ok!/usr/share/simplestream/menu/icons/ok.svg:0" \
--text="<b>Add Url</b>" \
"$A1 $A2" "$B1 $B2" "$C1 $C2" "$D1 $D2" "$E1 $E2" "$F1 $F2" "$G1 $G2" "$H1 $H2"` 
if [ $? = "6" ]; then
A1=`yad --title="SimpleStream" \
--window-icon=$ICON \
--image=$ICON \
--center --width=375 \
--button="No!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Yes!/usr/share/simplestream/menu/icons/ok.svg:0" \
--text="\nReset to original default streams?"`
if [ $? = "1" ]; then
exec "/usr/bin/simplestream"
fi
`echo \
"Top 40
http://relay.181.fm:8070
---------------------------------------
Easy
http://airspectrum.cdnstream1.com:8018/1606_192
---------------------------------------
Rock
http://51.161.115.200:8028/stream/1/
---------------------------------------
Talk
http://198.27.120.235:8450/stream/1/
---------------------------------------
Country
http://173.249.21.17:8036
---------------------------------------
Motown
http://airspectrum.cdnstream1.com:8024/1302_192
---------------------------------------
Blues
http://us3.internet-radio.com:8342
---------------------------------------
Oldies
https://us2.internet-radio.com/proxy/megatoncafe?mp=/stream
---------------------------------------" \
| awk '{print $0}' > $CONF/stations`
exec "/usr/bin/simplestream"
fi
if [ -n "${MENU}" ];then
case $MENU in
"$A1 $A2")
A2=`yad --title="Simple Stream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$A2" --field="$A1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url</b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"

---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $A2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "2s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$B1 $B2")
B2=`yad --title="Simple Stream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$B2" --field="$B1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1

---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $B2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "5s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$C1 $C2")
C2=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$C2" --field="$C1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1

---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $C2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "8s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$D1 $D2")
D2=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$D2" --field="$D1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1

---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $D2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "11s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$E1 $E2")
E2=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$E2" --field="$E1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1

---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $E2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "14s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$F1 $F2")
F2=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$F2" --field="$F1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1

---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $F2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "17s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$G1 $G2")
G2=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$G2" --field="$G1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1

---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $G2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "20s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$H1 $H2")
H2=`yad --title="SimpleStream" \
--window-icon=$ICON \
--width=400 \
--center \
--form "$H2" --field="$H1" \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Save!/usr/share/simplestream/menu/icons/save.svg:0" \
--text="<b>Add a new url </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1

---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $H2 | awk '{print $0}' | sed s'/.$//'`
`sed -i "23s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
esac 
fi
exec "/usr/bin/simplestream"
exit 0
 fi &


