#!/bin/bash
#Copyright 2012,2013,2014,2015 - GPL
#Sigmund Berglund - Valåmoen, Norway.

export TEXTDOMAIN=pmusic
export PING_SERVER='google.com'
export WORKDIR=$HOME/.pmusic/tmp
export APPDIR=/usr/local/pmusic
. $HOME/.pmusic/pmusicrc #we need to know where STORAGE_DIR is located
echo -n > $WORKDIR/tmp_tophits_archive_log

build (){
	if [ $COUNTRY_UK = true ]; then COUNTRY=uk
	elif [ $COUNTRY_US = true ]; then COUNTRY=us
	elif [ $COUNTRY_NO = true ]; then COUNTRY=no
	fi

	if [ ! "$YEAR_TO" ] || [ ! "$YEAR_FROM" -le "$YEAR_TO" ]; then YEAR_TO=$YEAR_FROM; fi
	if [ ! "$MONTH_TO" ] || [ ! "$MONTH_FROM" -le "$MONTH_TO" ]; then MONTH_TO=$MONTH_FROM; fi

	#start Top Hits playlost builder
	for YEAR in $(seq $YEAR_FROM $YEAR_TO); do
		for MONTH in $(seq $MONTH_FROM $MONTH_TO); do
		
			TMP=""
			echo "$(gettext 'Downloading...')" > $WORKDIR/tmp_tophits_archive_log
			case $COUNTRY in
				no)
					WEEK=`date -d "${YEAR}$(printf "%02d" $MONTH)$(printf "%02d" $DATE)" +%V`
					TMP="`wget "http://lista.vg.no/liste/topp-20-single/1/dato/${YEAR}/uke/${WEEK}" -O- | grep -F 'class="album"' | tr '=' '\n'`"
					echo "$TMP" | grep -F 'byArtist' | cut -d'>' -f2 | cut -d'<' -f1 > $WORKDIR/tmp_tophits_archive_vg_artists
					echo "$TMP" | grep -F '"name"' | cut -d'>' -f2 | cut -d'<' -f1 > $WORKDIR/tmp_tophits_archive_vg_titles
					TMP="`paste -d'/' $WORKDIR/tmp_tophits_archive_vg_artists $WORKDIR/tmp_tophits_archive_vg_titles | sed 's%/% - %g'`"
#					[ ! "$TMP" ] && continue
					PMU_NAME="VG-lista"
					;;
				uk)
					#DB=A #A for UK, D for Billboard
					TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5  "http://umdmusic.com/default.asp?Lang=English&Chart=A&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
#					[ ! "$TMP" ] && continue
					PMU_NAME="UK Hits"
					;;
				us)
					#DB=D #A for UK, D for Billboard
					TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] &&	TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] &&	TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
					[ ! "$TMP" ] && TMP="`wget -t 1 -T 5 "http://umdmusic.com/default.asp?Lang=English&Chart=D&ChDay=${DATE}&ChMonth=${MONTH}&ChYear=${YEAR}" -O- | grep -A2000 -F 'Display Chart Table' | grep -F padding | awk -F'>' {'print $5 " - " $3'} | sed -e 's% </B%%g' -e 's% </TD%%g' | grep -vF '<'`"
#					[ ! "$TMP" ] && continue
					PMU_NAME="Billboard Hits"
					;;
			esac

			#remove spaces
			RUN=true
			while [ "$RUN" != "false" ]; do
				TMP2="`echo "$TMP" | sed 's/  / /g'`"
				TMP="$TMP2"
				[ ! "`echo "$TMP" | grep -F '  '`" ] && RUN=false
			done
			echo "$TMP" > $WORKDIR/tmp_tophits_archive

			#write pmu file
			echo -n > $WORKDIR/tmp_tophits_archive.pmu
			COUNT=0
			TOTAL=`cat $WORKDIR/tmp_tophits_archive | wc -l`
			while read I; do
				COUNT=$(($COUNT+1))
				echo "$PMU_NAME $YEAR $MONTH $DATE: $COUNT $I" > $WORKDIR/tmp_tophits_archive_log
				ARTIST2="`echo "$I" | awk -F' - ' {'print $1'}`"
				ARTIST="`echo "$ARTIST2" | awk -F "(" '{print $1}' | awk -F ' feat' '{print $1}' | awk -F ' & ' '{print $1}' | awk -F ' / ' '{print $1}' | sed "s/ $//"`" #remove info in ()
				TITLE2="`echo "$I" | awk -F' - ' {'print $2'}`"
				TITLE="`echo "$TITLE2" | awk -F "(" '{print $1}' | sed "s/ $//"`" #remove info in ()

				FILE_PATH="`cut -d'|' -f1-3 "$STORAGE_DIR/index_mymusic" | grep -iF "|${TITLE}" | grep -Fi "|${ARTIST}|" | cut -d'|' -f1 | grep -iEm1 "$AUDIO_FORMATS"`"
				[ ! "$FILE_PATH" ] && FILE_PATH="`cut -d'|' -f1-3 "$STORAGE_DIR/index_mymusic" | grep -iwF "${TITLE}" | grep -Fwi "${ARTIST}" | cut -d'|' -f1 | grep -iEm1 "$AUDIO_FORMATS"`"
				LENGTH=''
				[ "$FILE_PATH" ] && LENGTH="`grep -Fm1 "${FILE_PATH}|" "$STORAGE_DIR/index_mymusic" | cut -d'|' -f12`"
				if [ ! "$LENGTH" ] && [ "$FILE_PATH" ]; then
					$APPDIR/func_id3io "$FILE_PATH" -read
					LENGTH="`grep -Fm1 "${FILE_PATH}|" "$STORAGE_DIR/index_mymusic" | cut -d'|' -f12`"
				fi
				TMP=''
				TMP=`date +%s%N` #even if no local file is found we mark it with this stamp to be abla to remove it later within pMusic (mid-click)
				echo "|$LENGTH|$ARTIST2 - $TITLE2|$FILE_PATH|$TMP $FILE_PATH" >> $WORKDIR/tmp_tophits_archive.pmu
			done < $WORKDIR/tmp_tophits_archive

			#store result
			cp $WORKDIR/tmp_tophits_archive.pmu "$OUTDIR/$PMU_NAME $YEAR $(printf "%02d" $MONTH) $(printf "%02d" $DATE).pmu"
			
			#add to favorites
			[ "$ADD_FAVORITES" = "true" ] && $APPDIR/func_favorites -add_favorites "pmusicbrowse_expand:$OUTDIR/$PMU_NAME $YEAR $(printf "%02d" $MONTH) $(printf "%02d" $DATE).pmu"
		done
	done
	echo ' ' > $WORKDIR/tmp_tophits_archive_log
}
export -f build


case "$1" in
	-build)
		#check if connected to internet
		LANG=C wget --spider --tries 1 -T 3  -F --max-redirect 0 $PING_SERVER 2> $WORKDIR/tmp_tophits
		if [ ! "`grep 'connected' $WORKDIR/tmp_tophits`" ]; then
			gettext 'No internet connection' > $WORKDIR/tmp_tophits_archive_log
			echo true > $WORKDIR/tmp_tophits_buildchk #unlock gui
			exit
		fi
		#---
		echo false > $WORKDIR/tmp_tophits_buildchk #lock gui
		build
		$APPDIR/func_index -quick_update "$OUTDIR"
		echo true > $WORKDIR/tmp_tophits_buildchk #unlock gui
		exit
		;;
esac

ITEMS_DAY="<item>$(date +%d)</item>"
ITEMS_MONTH="<item>$(date +%m)</item>"
C=1; while [ $C != 32 ]; do ITEMS_DAY="$ITEMS_DAY<item>$C</item>"; C=$(($C+1)); done
C=1; while [ $C != 13 ]; do ITEMS_MONTH="$ITEMS_MONTH<item>$C</item>"; C=$(($C+1)); done
C=1940; while [ $C != $(($(date +%Y)+1)) ]; do ITEMS_YEAR="$ITEMS_YEAR<item>$C</item>"; C=$(($C+1)); done
echo false > $WORKDIR/tmp_tophits_buildchk #will be true when build is done

export tophits='
<window title="pMusic - '$(gettext 'Top-Hits Archive')'" icon-name="pmusic">
 <vbox>
  <frame>
   <hbox>
    <pixmap>
     <width>100</width>
     <input file>/usr/share/icons/hicolor/scalable/apps/pmusic_tophits_archive.svg</input>
    </pixmap>
    <text><label>"'$(gettext 'Build a Top Hits playlist based on your local files. You choose which date(s) the list should reflect.

Combined with the RadioHits Grabber, you got what is hot.')'"</label></text>
    </hbox>
   <hseparator></hseparator>
   <vbox>
    <hbox>
     <text><label>'$(gettext 'Output directory')'</label></text>
     <entry accept="directory" width-request="300" fs-title="Pmusic - '$(gettext 'Output directory')'">
      <variable>OUTDIR</variable>
      <default>"'$HOME'"</default>
     </entry>
     <button>
      <input file stock="gtk-open"></input>
      <action type="fileselect">OUTDIR</action>
     </button>
    </hbox>
    <text><label>""</label></text>
	<radiobutton>
	 <label>UK  (1952-)</label>
	 <variable>COUNTRY_UK</variable>
	</radiobutton>
	<radiobutton>
	 <label>USA Billboard  (1940-)</label>
	 <variable>COUNTRY_US</variable>
	</radiobutton>
	<radiobutton>
	 <label>Norway VG-lista  (1959-)</label>
	 <variable>COUNTRY_NO</variable>
	</radiobutton>
    <hbox>
	 <vbox>
	  <text height-request="23"><label>""</label></text>
	  <text use-markup="true"><label>"<b>'$(gettext 'From')':</b>"</label></text>
	 </vbox>
	 <vbox>
	  <text><label>'$(gettext 'Day')'</label></text>
	  <comboboxtext><variable>DATE</variable>'$ITEMS_DAY'</comboboxtext>
	 </vbox>
	 <vbox>
	  <text><label>'$(gettext 'Month')'</label></text>
	  <comboboxtext><variable>MONTH_FROM</variable><default>'$(date +%m)'</default>'$ITEMS_MONTH'</comboboxtext>
	 </vbox>
	 <vbox>
	  <text><label>'$(gettext 'Year')'</label></text>
	  <comboboxtext><variable>YEAR_FROM</variable><default>'$(date +%Y)'</default>'$ITEMS_YEAR'</comboboxtext>
	 </vbox>
    </hbox>

    <hbox>
     <checkbox>
      <variable>ADD_FAVORITES</variable>
      <label>'$(gettext 'Add to favorites')'</label>
      <default>true</default>
     </checkbox>
     <text space-expand="true" space-fill="true"><label>""</label></text>
     <text use-markup="true"><label>"<b>'$(gettext 'To')':</b>"</label></text>
     <comboboxtext><sensitive>false</sensitive><item></item>'$ITEMS_DAY'</comboboxtext>
     <comboboxtext><variable>MONTH_TO</variable><item></item>'$ITEMS_MONTH'</comboboxtext>
     <comboboxtext><variable>YEAR_TO</variable><item></item>'$ITEMS_YEAR'</comboboxtext>
    </hbox>
    <variable>VBOX_MAIN</variable>
   </vbox>

  </frame>
  <hbox space-expand="true" space-fill="true">
   <button space-expand="false" space-fill="false">
    <input file stock="gtk-help"></input>
    <action>'$BROWSER' http://murga-linux.com/puppy/viewtopic.php?p=663133#663133 &</action>
   </button>
   <text space-expand="true" space-fill="true"><label>""</label></text>
   <button space-expand="false" space-fill="false">
    <input file stock="gtk-cancel"></input>
    <label>'$(gettext 'Cancel')'</label>
    <action>exit:EXIT</action>
   </button>
   <button space-expand="false" space-fill="false">
    <input file stock="gtk-execute"></input>
    <variable>BUTTON_EXEC</variable>
    <label>'$(gettext 'Build Top Hits playlist')'</label>
    <action>disable:VBOX_MAIN</action>
    <action>disable:BUTTON_EXEC</action>
    <action>. '$0' -build &</action>
   </button>
   <checkbox visible="false" file-monitor="true" auto-refresh="true">
    <input file>'$WORKDIR'/tmp_tophits_buildchk</input>
    <action>if true enable:VBOX_MAIN</action>
    <action>if true enable:BUTTON_EXEC</action>
   </checkbox>
  </hbox>
  <statusbar file-monitor="true" auto-refresh="true">
   <input file>'$WORKDIR'/tmp_tophits_archive_log</input>
  </statusbar>
 </vbox>
</window>'
gtkdialog -p tophits
TMP=`ps`
for I in `echo "$TMP" | grep -Fi "tophits" | grep -F build | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
exit 0
