#!/bin/bash

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

PID=`ps -eaf | grep mpg123 | 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 &

# 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 changes from default to the personal radio genres
MENU=`yad --title="SimpleStream" --width=325 \
--window-icon=$ICON \
--center \
--entry \
--button="Cancel!/usr/share/simplestream/menu/icons/cancel.svg:1" \
--button="Ok!/usr/share/simplestream/menu/icons/ok.svg:0" \
--text="<b>Add Genres</b>" \
"$A1" "$B1" "$C1" "$D1" "$E1" "$F1" "$G1" "$H1"`
if [ $? = "1" ]; then
exec "/usr/bin/simplestream"
fi 
if [ -n "${MENU}" ];then
case $MENU in
"$A1")
A1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$A1" --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 New Genres</b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $A1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "1s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$B1")
B1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$B1" --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 New Genres </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------

$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $B1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "4s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$C1")
C1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$C1" --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 genres </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------

$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $C1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "7s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$D1")
D1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$D1" --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 genres </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------

$D2
---------------------------------------
$E1
$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $D1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "10s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$E1")
E1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$E1" --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 genres </b>"`
ret=$?
[[ $ret -eq 1 ]] && 
exec "/usr/bin/simplestream"
if [[ $ret -eq 0 ]]; then
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------

$E2
---------------------------------------
$F1
$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $E1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "13s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$F1")
F1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$F1" --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 genres </b>"`
if [ $? = "1" ]; then
exec "/usr/bin/simplestream"
fi
`echo \
$A1"
$A2
---------------------------------------
$B1
$B2
---------------------------------------
$C1
$C2
---------------------------------------
$D1
$D2
---------------------------------------
$E1
$E2
---------------------------------------

$F2
---------------------------------------
$G1
$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $F1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "16s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
 ;;
"$G1")
G1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$G1" --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 genres </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
---------------------------------------

$G2
---------------------------------------
$H1
$H2
---------------------------------------" | awk '{print $0}' > $CONF/.tmp1`
TR=`echo $G1 | awk '{print $0}' | sed s'/.$//'`
`sed -i "19s|^|$TR|" $CONF/.tmp1`
mv $CONF/.tmp1 $CONF/stations
exec "/usr/bin/simplestream"
fi
 ;;
"$H1")
H1=`yad --title="SimpleStream" \
--window-icon=$ICON --width=375 \
--center \
--form "$H1" --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 genres </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
---------------------------------------

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




