#!/bin/bash
#pMusic
#Copyright, Sigmund Berglund - GPL
. $APPDIR/func -reset_gtk
export TEXTDOMAIN=pmusic

echo 0 > $WORKDIR/radio_splash
echo "pMusic" > $WORKDIR/radio_progresstext #text in progressbar
echo -n > $WORKDIR/radio_tmp2

build_db (){
	#check internet connection
	echo "$(gettext 'Connecting')" > $WORKDIR/radio_progresstext
	LANG=C wget --spider "$PING_SERVER" 2> $WORKDIR/tmp
	if [ ! "`grep 'connected' $WORKDIR/tmp`" ]; then
		TXT1="<b>$(gettext 'Not able to connect to internet')</b>"
		. $APPDIR/box_ok
		exit
	fi

	echo -n > "$STORAGE_DIR/index_radio"

	#download updated 
	echo "www.radiosure.com" > $WORKDIR/radio_progresstext
	LANG=C wget --tries=1 -O $WORKDIR/radio_sure.zip http://www.radiosure.com/rsdbms/stations2.zip
	unzip -p $WORKDIR/radio_sure.zip > $WORKDIR/radio_wget
	#pre-formating
	cat $WORKDIR/radio_wget | tr -s "\t" '|' > $WORKDIR/radio_tmp2
	grep -iE "m3u|mp3|pls|ogg|winamp|:....\|" $WORKDIR/radio_tmp2 > $WORKDIR/radio_tmp3
	#---
	echo -n > $WORKDIR/radio_tmp2
	echo -n > $WORKDIR/radio_radiosure_tags
	echo -n > $WORKDIR/radio_radiosure_country
	#for percent
	COUNT=0
	TOTAL="`grep -c ^ $WORKDIR/radio_tmp3`"
	#---
	[ $UTF8 = false ] && export LC_ALL=C
	while read I; do
		RADIO_NAME="`echo "$I" | cut -d'|' -f1`"
		RADIO_TAG="`echo "$I" | cut -d'|' -f3 | sed "s/^ //"`"
		RADIO_COUNTRY="`echo "$I" | cut -d'|' -f4`"
		#URL
		RADIO_PATH="`echo "$I" | cut -d'|' -f6`"
		[ ! "`echo "$RADIO_PATH" | grep -iE "m3u|mp3|pls|ogg|winamp|:....$" | grep -viE "winstream|asx|mms:"`" ] && RADIO_PATH="`echo "$I" | cut -d'|' -f7`"
		[ ! "`echo "$RADIO_PATH" | grep -iE "m3u|mp3|pls|ogg|winamp|:....$" | grep -viE "winstream|asx|mms:"`" ] && RADIO_PATH="`echo "$I" | cut -d'|' -f8`"
		[ ! "`echo "$RADIO_PATH" | grep -iE "m3u|mp3|pls|ogg|winamp|:....$" | grep -viE "winstream|asx|mms:"`" ] && RADIO_PATH="`echo "$I" | cut -d'|' -f9`"
		[ ! "`echo "$RADIO_PATH" | grep -iE "m3u|mp3|pls|ogg|winamp|:....$" | grep -viE "winstream|asx|mms:"`" ] && RADIO_PATH="`echo "$I" | cut -d'|' -f10`"
		[ ! "`echo "$RADIO_PATH" | grep -iE "m3u|mp3|pls|ogg|winamp|:....$" | grep -viE "winstream|asx|mms:"`" ] && RADIO_PATH="`echo "$I" | cut -d'|' -f11`"
		[ "$RADIO_PATH" = "-" ] && RADIO_PATH="`echo "$I" | cut -d'|' -f6`" #no match --> go for the first alternative
		#---
		echo "${RADIO_PATH}|${RADIO_NAME}|${RADIO_TAG}|${RADIO_COUNTRY}" >> $WORKDIR/radio_tmp2 #tmp2 is used by progressbar
		COUNT=$(($COUNT+1))
		echo $(($COUNT*98/$TOTAL)) > $WORKDIR/radio_splash #98 to prevent it to be 100% and close window
	done < $WORKDIR/radio_tmp3
	cat $WORKDIR/radio_tmp2 >> "$STORAGE_DIR"/index_radio
	rm $WORKDIR/radio_*
	$APPDIR/func -radio -country
	echo 100 > $WORKDIR/radio_splash
}

install_index (){
	echo "$(gettext 'Downloading radio index.')" > $WORKDIR/splashtext
	$APPDIR/box_splash &
	#check internet connection
	LANG=C wget --spider "$FTP_SERVER/pmusic_index_radio.gz" 2> $WORKDIR/tmp
	if [ "`grep -Ei "unable to resolve host|file does not exist" $WORKDIR/tmp`" ]; then
		#let's try the alternative ftp server
		LANG=C wget --spider "$FTP_SERVER_2/pmusic_index_radio.gz" 2> $WORKDIR/tmp
		if [ "`grep -Ei "unable to resolve host|file does not exist" $WORKDIR/tmp`" ]; then
			sleep 0.2
			echo 100 > $WORKDIR/splash
			TXT1="<b>$(gettext 'No connection to server')</b>"
			. $APPDIR/box_ok
			exit
		else
			MY_SERVER="$FTP_SERVER_2"
			MY_USER="$FTP_USER_2"
			MY_PASSWORD="$FTP_PASSWORD_2"
		fi
	else
		MY_SERVER="$FTP_SERVER"
		MY_USER="$FTP_USER"
		MY_PASSWORD="$FTP_PASSWORD"
	fi
	#---
	rm "$STORAGE_DIR/index_radio"
	wget --tries=1 --user $MY_USER --password $MY_PASSWORD --output-document="$STORAGE_DIR/index_radio.gz" "$MY_SERVER/pmusic_index_radio.gz"
	gunzip -f "$STORAGE_DIR/index_radio.gz"
	$APPDIR/func -radio -country
	echo 100 > $WORKDIR/splash
}

export -f build_db install_index

export pmusic_radio='
<window title="pMusic - '$(gettext 'Radio stations setup')'" icon-name="pmusic">
<vbox>
 '"$($APPDIR/func_svg -heading "$(gettext 'Radio stations')")"'
 <notebook labels=" '$(gettext 'Install')' | '$(gettext 'Advanced')' ">
 <frame>
  <text height-request="120"><label>"'$(gettext 'This is a one-click download and install of the radio-station index.

The index file will be 1Mb+ when installed, and give you access to 17000+ radio stations.')'"</label></text>
  <hbox homogeneous="true"><hbox>
   <button>
    <variable>BUTTON_INSTALL</variable>
    <input file stock="gtk-apply"></input>
    <label>'$(gettext 'Install')'</label>
    <action>install_index</action>
    <action>EXIT:exit</action>
   </button>
  </hbox></hbox>
 </frame>
 <frame>
  <text><label>"'$(gettext 'The installable index might be too old for you. Do not worry, it is possible to build a new index, based on the very recent info at radiosure.com. - Depending on your system, this will take some time.

The radio-database is static, which means user can not edit it manually. To include new URLs, you should add it to your Favorites or contact www.radiosure.com to include wanted stream-adress. Be aware that pMusic only supports the stream-formats mp3 and ogg.')'"</label></text>
  <progressbar>
   <input>while [ "$I" != "100" ]; do I=`cat '$WORKDIR'/radio_splash`; J=`cat '$WORKDIR'/radio_progresstext`; [ -f '$WORKDIR'/radio_tmp2 ] && NR=`grep -c ^ '$WORKDIR'/radio_tmp2`; echo $I; echo "$J (${I}% - $NR added)"; sleep 0.5; done</input>
   <action type="exit">cancel</action>
  </progressbar>
  <hbox homogeneous="true"><hbox>
   <button>
    <variable>BUTTON_BUILD</variable>
    <label>'$(gettext 'Build index')'</label>
    <action>build_db &</action>
    <action>disable:BUTTON_BUILD</action>
    <action>disable:BUTTON_INSTALL</action>
   </button>
   </hbox></hbox>
  </frame>
 </notebook>
 <hbox>
  <text use-markup="true"><label>"<b>'$(gettext 'Last indexing')':</b>"</label></text>
  <text><input>find "'$STORAGE_DIR'/index_radio" -printf "%Td.%Tb %TY   (%TH:%TM)"</input></text>
  <text use-markup="true"><label>"  <b>'$(gettext 'Radio stations')':</b>"</label></text>
  <text><input>grep -c ^ "'$STORAGE_DIR'/index_radio"</input></text>
  <button cancel></button>
 </hbox>
</vbox>
<action signal="hide">exit:cancel</action>
</window>'
$GTKDIALOG -p pmusic_radio

#kill backends
TMP="`ps -eo pid,command`"
for I in `echo "$TMP" | grep -F 'build_db &' | awk '{print $1}'`; do kill -9 $I 2> $STDERR; done
echo 100 > $WORKDIR/radio_splash #end loop
