#!/bin/bash
########################################################
#                   Simple Stream                      # 
#               GUI radio streamer                     #
#        © 2024 WTFPL lxgator@gmail.com 	 	       #
########################################################
NAME="Simple Stream"
VERSION='3.0'
PLAYR='/usr/bin/mpg123'
ICON="/usr/share/simplestream/menu/icons/simplestream16.svg"
EICON="/usr/share/simplestream/menu/icons/error.svg"
CONF="$HOME/.simplestream"
URL="/usr/share/simplestream/data"

# Check if required dependencies are there
ERR(){ echo "ERROR: $1" 1>&2; }
declare -i DEPCOUNT=0
for DEP in /usr/bin/{mpv,yad,streamripper}; do
[ -x "$DEP" ] || {
DEPS=$(echo "$DEP"|sed 's/^.........//')
ERR $LINENO | `yad --width=425 --center \
--text="\n<b>$DEPS</b> is missing from your system!\n\nInstall the <b>$DEPS</b> missing dependency.\n" \
--window-icon=$EICON \
--image=$EICON \
--title='Simple Stream' \
--button="Cancel"!/usr/share/simplestream/menu/icons/cancel.svg:1`
exit 0
DEPCOUNT+=1
}
done
[ $DEPCOUNT -eq 0 ] || exit 1

# Need a network connection
wget -q --tries=3 --timeout=10 --delete-after "https://www.google.com/" 
if [[ $? -eq 0 ]]; then
rm -f /tmp/tmp* 2>&1 > /dev/null 
  exec $@
else
 echo "Network failed"
notify-send -t 10000 -i $EICON "No network connection 
Check for connection failure"
exit 0
fi
# New user, create defaulf folder, default stations file 
if ! test -d $CONF ; then
       mkdir -p $CONF
`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`
 fi
# 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}'`
# Kill if main menu is reopened after program running
exec /usr/share/simplestream/data/kill & 
sleep 1
MENU=$(yad --window-icon=$ICON --width=250 --height=270 \
--title="" --image="/usr/share/simplestream/menu/icons/menu.png" --image-on-top \
--button=Close!/usr/share/simplestream/menu/icons/cancel.svg:1 \
--tabnum=1 --tabnum=2 --columns='2' --form \
--field="$A1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$A2"':FBTN' ''"$URL/A3"'' \
--field="$B1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$B2"':FBTN' ''"$URL/B3"'' \
--field="$C1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$C2"':FBTN' ''"$URL/C3"'' \
--field="$D1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$D2"':FBTN' ''"$URL/D3"'' \
--field='Add Genres!/usr/share/simplestream/menu/icons/edit.svg!Add New Radio Genres:FBTN' '/usr/share/simplestream/data/genres' \
--field="$F1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$F2"':FBTN' ''"$URL/F3"'' \
--field="$G1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$G2"':FBTN' ''"$URL/G3"'' \
--field="$H1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$H2"':FBTN' ''"$URL/H3"'' \
--field="$E1"'!/usr/share/simplestream/menu/icons/resume.svg!'"$E2"':FBTN' ''"$URL/E3"'' \
--field='Add Url!/usr/share/simplestream/menu/icons/edit.svg!Add New Radio Url:FBTN' '/usr/share/simplestream/data/url')

 
