#!/bin/bash

EICON="/usr/share/click-radio/icons/error.svg"
OK="/usr/share/click-radio/icons/ok.svg"
CANCEL="/usr/share/click-radio/icons/cancel.svg"
FIND="/usr/share/click-radio/icons/find.svg"
CONFG="$HOME/.config/clickradio"
ICON='/usr/share/click-radio/icons/click-radio.png'
HELP="/usr/share/click-radio/icons/help.svg"

txt_entry=$(yad --window-icon="$ICON" --form --title=$"Click Radio Audio Converter" \
--width=450 --height=100 --fixed --on-top --center --borders=10 \
--button="Help!$HELP":"/usr/share/click-radio/menu/app/help" \
--button="Search!$FIND":"/usr/share/click-radio/menu/app/find" \
--button="Cancel!$CANCEL":1 \
--button="Convert!$OK":0 \
--text=$"<b>Convert a Standard URL to .pls and .m3u Stream Files</b>" \
--field=$"Radio Station Name::CE" \
--field=$"Radio Station URL::CE")
if [ $? = "1" ]; then
rm -f /tmp/tmp*
rm -f $CONFG/console.* 
rm -f $CONFG/{*.pls,*.m3u}
exit
else
entry1=$(echo "$txt_entry"|awk -F "|" '{print $1}' > /tmp/tmp.txt)
entry2=$(echo "$txt_entry"|awk -F "|" '{print $2}' >> /tmp/tmp.txt) 
sleep 1
pkill yad > /dev/null 2>&1 
USERS=$(who |sed 's/ .*//'|head -n1)
chown -R $USERS:$USERS /tmp/tmp.txt
if [ -z "$(cat /tmp/tmp.txt)" ]; then
$(yad --window-icon="$EICON" --image="$EICON" --title=$"Click Radio Audio Converter" \
--width=400 --height=100 --fixed --center --borders=10 --on-top --button="OK!$OK" \
--text=$"\nNo entries have been added to the audio file!")
rm -f $CONFG/console.* 
exit 0
else
NAME=`cat /tmp/tmp.txt | awk 'NR==1{print $0}'`
URL=`cat /tmp/tmp.txt | awk 'NR==2{print $0}'`
# Create a m3u file
sed -i '2s/"//g' /tmp/tmp.txt
cp -f /tmp/tmp.txt $CONFG/"$NAME".m3u
# Create a pls file
`echo \
"[playlist]
NumberOfEntries=1
File1="$URL"
" | awk '{print $0}' > /tmp/tmp.txt`
mv -f /tmp/tmp.txt $CONFG/"$NAME".pls
 fi
  fi
TEST=`cat $CONFG/"$NAME".m3u` 
if [ $TEST = $TEST 2>/dev/null ]; then  
$(yad --window-icon="$EICON" --image="$EICON" --title=$"Click Radio Audio Converter" \
--width=400 --height=100 --fixed --center --borders=10 --on-top --button="OK!$OK" \
--text=$"\nEntries are missing in the audio stream file!")
rm -f $CONFG/console.* 
exit 0
else
killall yad > /dev/null 2>&1 
ENTRY=$(yad --width=400 --height=125 --window-icon=$ICON --image=$ICON --center \
        --form --title="Click Radio Audio Converter" \
        --field="Save in:DIR" "$HOME/Downloads" \
--text='\nSave converted audio files' \
--button="Cancel"!gtk-cancel:1 --button="Ok"!gtk-ok:0)
ret=$?
[[ $ret -eq 1 ]] &&
exit 0
if [[ $ret -eq 0 ]]; then 
RET=$?
    OIFS=$IFS 
    IFS="|" 
    i=0
    retChoice=()

    for ret in $ENTRY;do 
        retChoice[$i]="$ret"
        i=$(($i+1))
    done

    IFS=$OIFS 
    SAVEDIR=${retChoice[0]} # directory path
if ! test -d $SAVEDIR/{m3u,pls}; then  
mkdir -p $SAVEDIR/{m3u,pls}
fi   
cp -f $CONFG/"$NAME".m3u $SAVEDIR/m3u   
mv -f $CONFG/"$NAME".pls $SAVEDIR/pls
sleep 1 
rm -f $SAVEDIR/m3u/console.*  
rm -f $SAVEDIR/pls/console.*  
rm -f $CONFG/{*.pls,*.m3u} 
rm -f $CONFG/console.* 
rm -f /tmp/tmp*
$(yad --window-icon="$ICON" --image="$ICON" --title=$"Click Radio Audio Converter" \
--text="\nFiles added to $SAVEDIR" \
--width=400 --height=100 --fixed --center --borders=10 --on-top --button="Ok!$OK") 
exit 0
 fi
  fi









