#!/bin/bash
export TEXTDOMAIN=pmusic



######################################################
#                                                    #
#                      F I L E                       #
#                                                    #
######################################################

S='
<menu label="'$(gettext 'File')'">
 <menuitem stock="gtk-cdrom" label="'$(gettext 'Play CD-audio')'">'
  [ "$CD_DEV" = false ] && S=$S'<visible>disabled</visible>'
  S=$S'<action>'$APPDIR'/func_cd -play_cd</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-preferences" label="'$(gettext 'Preferences')'..."><action>'$APPDIR'/func_config -preferences &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-cdrom" label="'$(gettext 'Eject CD')'">'
  [ "$CD_DEV" = false ] && S=$S'<visible>disabled</visible>'
  S=$S'<action>'$APPDIR'/func_player -stop -skip_update</action>
  <action>eject</action>
 </menuitem>
 <menuitem stock="gtk-quit" label="'$(gettext 'Quit')'" accel-key="0x071" accel-mods="4">
  <action>'$APPDIR'/func_config -write_config</action>
  <action>'$APPDIR'/func -quit</action>
  <action>exit:Exit</action>
 </menuitem>
 <menuitem stock="gtk-quit" label="'$(gettext 'Quit and save gui/geometry')'">
  <action>'$APPDIR'/func_config -write_config</action>
  <action>rm '$WORKDIR'/xml_pmusic*</action>
  <action>'$APPDIR'/func -quit</action>
  <action>exit:Exit</action>
 </menuitem>
</menu>'
GUI_MENU_FILE="$S"



######################################################
#                                                    #
#             M U S I C   S O U R C E S              #
#                                                    #
######################################################

#built top 100 menuitems for jamendo
TOP100_MENU=''
for I in ' ' Blues Classical Electro Jazz Hiphop Lounge Metal Pop Rock; do
  TOP100_MENU=$TOP100_MENU'<menuitem label="Top 100 '$I'">
   <action>'$APPDIR'/func_webmusic -jamendo_top100 '$I' &</action>
  </menuitem>'
done

 S='<menu label="'$(gettext 'Music sources')'">
 <menuitem stock="gtk-favorites" label="'$(gettext 'Favorites')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>'$APPDIR'/func_favorites -show_favorites</action>
  <action>refresh:SOURCE</action>
 </menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'RadioHits Grabber')'..."><action>. '$APPDIR'/func_radiohits &</action></menuitem>
  <menuitem label="CD">'
  [ "$CD_DEV" = false ] && S=$S'<visible>disabled</visible>'
  S=$S'<action>echo "|'$(gettext 'Probing disc')'" > '$WORKDIR'/browser</action>
  <action>'$APPDIR'/func_cd -read_cd &</action>
  <action>refresh:SOURCE</action>
 </menuitem>
 <menuitem label="'$(gettext 'Browse')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>export SOURCE="'$BROWSERDIR'"; . '$APPDIR'/func -browse</action>
  <action>refresh:SOURCE</action>
  <action>refresh:SEARCH</action>
 </menuitem>



##Radio stations 
 <menu label="'$(gettext 'Radio stations')'">
  <menuitem label="'$(gettext 'Setup')'..."><action>. '$APPDIR'/func_radio &</action></menuitem>
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Genre')'">
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func -radio -genre</action>
   <action>refresh:SOURCE</action>
  </menuitem>
  <menuitem label="'$(gettext 'Country')'">
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func -radio -country</action>
   <action>refresh:SOURCE</action>
  </menuitem>
 </menu>

##Webmusic
 <menu label="'$(gettext 'Web Music')'">
  <menuitem label="'$(gettext 'Setup')'..."><action>'$APPDIR'/func_webmusic -webmusic_manager &</action></menuitem>
  <menuitemseparator></menuitemseparator>
  '$TOP100_MENU'
  <menuitemseparator></menuitemseparator>
  <menuitem label="'$(gettext 'Browse')'">
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func_webmusic -webmusic</action>
   <action>refresh:SOURCE</action>
  </menuitem>
 </menu>

 <menuitem label="'$(gettext 'Podcast')'..."><action>. '$APPDIR'/func_podcast &</action></menuitem>
 <menuitem label="'$(gettext 'My Music')'..."><action>'$APPDIR'/func_index -update_index use_meta &</action></menuitem>

 <menuitemseparator></menuitemseparator>

 <menuitem label="'$(gettext 'Most played')'...">
  <action>'$APPDIR'/func_rating -custom</action>
 </menuitem>
 <menuitem label="'$(gettext 'Recent played')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>'$APPDIR'/func -index_recent</action>
  <action>refresh:SOURCE</action>
 </menuitem>
 
 <menuitemseparator></menuitemseparator>

 <menuitem label="'$(gettext 'View source history')'">
  <action signal="button-press-event">clear:SOURCE</action>
  <action>'$APPDIR'/func -index_history</action>
  <action>refresh:SOURCE</action>
 </menuitem>
 <menuitem label="'$(gettext 'Advanced file search')'..."><action>'$APPDIR'/func -pfilesearch</action></menuitem>
</menu>'
GUI_MENU_SOURCES="$S"



######################################################
#                                                    #
#                 P L A Y Q U E U E                  #
#                                                    #
######################################################

S='
<menu label="'$(gettext 'PlayQueue')'">
 <menuitem stock="gtk-pmusic_remove" label="'$(gettext 'Clear')'" accel-key="0x06e" accel-mods="4">
  <action>'$APPDIR'/func_store_playlist &</action>
  <action>sleep 0.2; '$APPDIR'/func -new</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>
 <menuitem stock="gtk-save" label="'$(gettext 'Save')'..." accel-key="0x073" accel-mods="4"><action>. '$APPDIR'/func -save</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-save-as" label="'$(gettext 'Export tracks in queue')'..."><action>'$APPDIR'/func_export &</action></menuitem>
 <menuitem stock="gtk-cdrom" label="'$(gettext 'Burn Audio-CD of queue')'...">'
  if [ ! -f /usr/local/pburn/pburn ] || [ "$CD_DEV" = false ]; then S=$S'<visible>disabled</visible>'; fi
  S=$S'<action>'$APPDIR'/func -burn &</action>
 </menuitem>
 <menuitemseparator></menuitemseparator>'
#---
 S=$S'<menuitem stock="gtk-pmusic_undo" label="'$(gettext 'Go back in queue history')'" accel-key="0x07a" accel-mods="4">
  <action>. '$APPDIR'/func -undo</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>
 <menuitem stock="gtk-pmusic_redo" label="'$(gettext 'Go forward in queue history')'" accel-key="0x079" accel-mods="4">
  <action>. '$APPDIR'/func -redo</action>
  <action>refresh:PLAYLIST</action>
 </menuitem>
</menu>'
GUI_MENU_PLAYLIST="$S"




######################################################
#                                                    #
#                  P L A Y B A C K                   #
#                                                    #
######################################################

S='
<menu label="'$(gettext 'Playback')'">
 <menuitem label="'$(gettext 'Track info')'" stock="gtk-info" accel-key="0x069" accel-mods="4"><action>'$APPDIR'/func_trackinfo "" -gui &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-media-play" label="'$(gettext 'Play / Pause')'" accel-key="0x020" accel-mods="8"><action>'$APPDIR'/func_player -play -skip_update; echo true > '$WORKDIR'/UPDATE_GUI</action></menuitem>
 <menuitem stock="gtk-media-stop" label="'$(gettext 'Stop')'" accel-key="0xff54" accel-mods="8"><action>'$APPDIR'/func_player -stop -skip_update; cp '$APPDIR'/themes/'$THEME'/icon_play.svg '$WORKDIR'/icon_playpause.svg; echo true > '$WORKDIR'/UPDATE_GUI</action></menuitem>
 <menuitem stock="gtk-media-next" label="'$(gettext 'Next track')'" accel-key="0xff53" accel-mods="8"><action>cp -f '$WORKDIR'/PLAYLIST '$WORKDIR'/tmpPLAYLIST</action><action>'$APPDIR'/func_player -next -user_action</action></menuitem>
 <menuitem stock="gtk-media-previous" label="'$(gettext 'Previous track')'" accel-key="0xff51" accel-mods="8"><action>'$APPDIR'/func_player -back</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'Jump to time')'..."><action>'$APPDIR'/func_player -jump_to &</action></menuitem>
 <menuitem stock="gtk-add_favorites" label="'$(gettext 'Add timestamp to favorites')'"><action>'$APPDIR'/func_favorites -add_favorites_with_timestamp &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-volume" label="'$(gettext 'Sound processing')'..."><action>'$APPDIR'/func_mixer -gui &</action></menuitem>
</menu>'
GUI_MENU_TRACK="$S"


S='
<menu label="'$(gettext 'Tools')'">
 <menu label="'$(gettext 'Plugins')'">
  <menuitem label="'$(gettext 'Plugin manager')'..."><action>'$APPDIR'/func -plugins &</action></menuitem>
  <menuitem label="'$(gettext 'Download plugins')'..."><action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; $BROWSER "$PLUGIN_URL" &</action></menuitem>
  <menuitemseparator></menuitemseparator>'
	find $APPDIR/plugins/ -name "*.rc" > $WORKDIR/tmp_gui_menu
	while read I; do
		if [ "`grep -F 'PLUGIN_RUN_AT_START=false' "$I"`" ]; then
			TMP="`grep -F 'PLUGIN_EXEC' "$I" | cut -d= -f2 | cut -d'"' -f2 | cut -d'"' -f1`"
			TMP1="`grep -F 'PLUGIN_NAME' "$I" | cut -d= -f2 | cut -d'"' -f2 | cut -d'"' -f1`"
			TMP2="`grep -F 'PLUGIN_IMAGE' "$I" | cut -d= -f2 | cut -d'"' -f2 | cut -d'"' -f1`"
			S=$S'<menuitem image-name="'$TMP2'" label="'$TMP1'..."><height>28</height><action>'$APPDIR'/plugins/'$TMP' &</action></menuitem>'
		fi
	done < $WORKDIR/tmp_gui_menu
  S=$S'
 </menu>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'Mass tagger')'..."><action>'$APPDIR'/func_id3tagger '$WORKDIR'/playlist &</action></menuitem>
</menu>'
GUI_MENU_TOOLS="$S"


S='
<menu label="'$(gettext 'View')'">
 <menuitem stock="gtk-fullscreen" label="'$(gettext 'Now playing')'">
  <action>exec pmusic -j -p .NowPlaying -T .NowPlaying</action>
 </menuitem>
 <menuitemseparator></menuitemseparator>
 <menu label="'$(gettext 'Docks')'">
  <menuitem stock="gtk-find" label="'$(gettext 'Show/hide Search options')'">
   <action type="activate">VIEW_SEARCH_OPTIONS</action>
  </menuitem>
  <menuitem stock="gtk-pmusic_add" label="'$(gettext 'Show/hide Smart Add')'">
   <action type="activate">VIEW_ADD_OPTIONS</action>
  </menuitem>
 </menu>'


 S=$S'</menu>'
GUI_MENU_VIEW="$S"


GUI_MENU_HELP='
<menu label="'$(gettext 'Help')'">
 <menuitem stock="gtk-about" label="'$(gettext 'About')'"><action>'$APPDIR'/func -about &</action></menuitem>
 <menuitem stock="gtk-help" label="'$(gettext 'Frequently asked questions')'" accel-key="0xffbe" accel-mods="0"><action>'$APPDIR'/func -faq &</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem stock="gtk-help" label="'$(gettext 'I want music')'"><action>'$APPDIR'/func -info_IWantMusic</action></menuitem>
 <menuitemseparator></menuitemseparator>
 <menuitem label="'$(gettext 'Dependency check')'"><action>'$APPDIR'/func -dependency_check</action></menuitem>
</menu>'


S='
<hbox spacing="1">
 <hbox space-expand="true" space-fill="true">
 <menubar name="menu_main">
  '$GUI_MENU_FILE'
  '$GUI_MENU_SOURCES'
  '$GUI_MENU_PLAYLIST'
  '$GUI_MENU_TRACK'
  '$GUI_MENU_VIEW'
  '$GUI_MENU_TOOLS'
  '$GUI_MENU_HELP'
  <action>rm -f $WORKDIR/history</action> ##reset undo/redo function
 </menubar>
 </hbox>'
 if [ $FIRST_RUN = true ]; then S=$S'
 <button>
  <input file stock="gtk-help"></input>
  <label>'$(gettext 'I want music')'</label>
  <action>'$APPDIR'/func -info_IWantMusic</action>
 </button>'
 fi
GUI_MENUS=$S'</hbox>'

