%
% anyremote configuration file for Gmusicbrowser (http://squentin.free.fr/gmusicbrowser/gmusicbrowser.html) 
% management (Server-mode)
%
% prerequisites: amixer/pactl utility
%

% STATUS stable
% XTEST no
% SOUND mixer
% ENV no
% DEF_PLAYER   
% ALL_IN_ONE
% MEDIACENTER audio

GuiAppName=Gmusicbrowser
GuiAppBinary=gmusicbrowser
GuiAppRun=echo 'P=`ps -ef|grep gmusicbrowser|grep -v grep|grep -v anyremote|grep -v nedit`; if [ "x$P" = "x" ]; then echo NOK; else echo OK; fi'|sh -s
GuiAppIcon=gmusicbrowser.png
GuiAppType=Application
GuiAppProtocols=Server
GuiAppDesc=An open-source jukebox for large collections of mp3/ogg/flac/mpc files, written in perl.

[Protocol]=Server

(Init)=\
	Include($(CfgDir)/Utils/aliases-server.cfg);\
	Macro(SETUP_DEFAULT_MIXER);\
	Macro(TEST_DBUS_SEND);\
 	Make(var,player_name,by_value,Gmusicbrowser);\
	Include($(CfgDir)/Utils/aliases-server-player.cfg);\
	Make(var,gmusicbrowser_opt,R=`gmusicbrowser -h|grep -- -remotecmd`;if [ "x$R" = "x" ]; then echo "-cmd"; else echo "-remotecmd"; fi);\
	Make(var,gmusicbrowser_cmd,by_value,\
	     dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.gmusicbrowser /org/mpris/MediaPlayer2);\
	Make(var,gmusicbrowser_get,by_value,\
	     dbus-send --type=method_call --print-reply --dest=org.mpris.MediaPlayer2.gmusicbrowser /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get);\
	Make(var,gmusicbrowser_metadata,by_value,\
	         $(gmusicbrowser_get) string:org.mpris.MediaPlayer2.Player string:Metadata);\
	Make(var,cover_size,by_value,);\
	Make(var,gmusicbrowser_layout,\
	     V=`$(CfgDir)/Utils/registry.sh $(TmpDir) get gmusicbrowser_layout`;\
	     if [ "x$V" = "x" ]; then echo "5x1"; else echo $V; fi);

(Exit)=\
	Exec($(CfgDir)/Utils/registry.sh $(TmpDir) set gmusicbrowser_layout $(gmusicbrowser_layout));

(Connect)=\
	Make(mode,gmusicbrowser_$(gmusicbrowser_layout));


[Mode]=gmusicbrowser_general

% Volume control and 0-9,*,# assignments by Utils/aliases-server-player.cfg
%1=Exec(gmusicbrowser $(gmusicbrowser_opt) DecVolume);
%2=Exec(gmusicbrowser $(gmusicbrowser_opt) TogMute); -- this do not works
%3=Exec(gmusicbrowser $(gmusicbrowser_opt) IncVolume);

PLAY_ONLY=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) PlayPause 2> /dev/null);

PLAY=\
	Macro(PLAY_ONLY);\
	Macro(GMUSICBROWSER_START);

RWD=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) "Rewind 10" 2> /dev/null);
	
FF=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) "Forward 10" 2> /dev/null);

PREV=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) PrevSongInPlaylist 2> /dev/null);\
	Macro(GMUSICBROWSER_TITLE);

STOP=\
	Timer(GMUSICBROWSER_TITLE,cancel);\
	Exec(gmusicbrowser $(gmusicbrowser_opt) Stop 2> /dev/null);
	
NEXT=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) NextSongInPlaylist 2> /dev/null);\
	Macro(GMUSICBROWSER_TITLE);

PAUSE=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) PlayPause 2> /dev/null);

RUN_IF_NEED=\
	Exec(P=`ps -ef|grep gmusicbrowser|grep -v grep|grep -v anyremote|grep -v edit`; \
	     if [ "x$P" = "x" ]; then gmusicbrowser 2> /dev/null & fi)

RUN_OR_QUIT=\
	Exec(P=`ps -ef|grep gmusicbrowser|grep -v grep|grep -v anyremote|grep -v edit`; \
	     if [ "x$P" = "x" ]; then gmusicbrowser  2> /dev/null & else gmusicbrowser $(gmusicbrowser_opt) Quit; fi);

APP_QUIT=\
	Exec(gmusicbrowser $(gmusicbrowser_opt) Quit 2> /dev/null)

Repeat=\
	Make(var,repeat_val,\
	     P=`$(gmusicbrowser_get) string:org.mpris.MediaPlayer2.Player string:LoopStatus|tail -1|grep string|cut -f 2 -d '"'`;\
	     if [ "x$P" = "xNone" ]; then echo Playlist; else echo None; fi);\
        Exec($(gmusicbrowser_cmd) org.freedesktop.DBus.Properties.Set string:org.mpris.MediaPlayer2.Player string:LoopStatus variant:string:$(repeat_val));

GMUSICBROWSER_TITLE_GENERAL=\
        Make(var,gmusicbrowser_artist,$(gmusicbrowser_metadata)|awk '/.*xesam.albumArtist.*/{getline;getline;print}'|cut -f 2 -d '"');\
        Make(var,gmusicbrowser_album,$(gmusicbrowser_metadata)|awk '/.*xesam.album".*/{getline;print}'|cut -f 2 -d '"');\
        Make(var,gmusicbrowser_title,$(gmusicbrowser_metadata)|awk '/.*xesam.title.*/{getline;print}'|cut -f 2 -d '"');\
	Set(title,$(gmusicbrowser_artist) - $(gmusicbrowser_album));\
	Set(status,$(gmusicbrowser_title));

GMUSICBROWSER_STATUS=\
	Make(var,gmusicbrowser_status,\
	     $(gmusicbrowser_get) string:org.mpris.MediaPlayer2.Player string:PlaybackStatus|tail -1|grep string|cut -f 2 -d '"');

GMUSICBROWSER_CHECK_TIMER=\
	Macro(GMUSICBROWSER_STATUS);\
	Make(var,timer_action,if [ "x$(gmusicbrowser_status)" = "xPlaying" ]; \
	                      then echo GMUSICBROWSER_START; else echo GMUSICBROWSER_CANCEL; fi);\
	Macro($(timer_action));

GMUSICBROWSER_CANCEL=\
	Timer(GMUSICBROWSER_TITLE,cancel);

GMUSICBROWSER_START=\
	Macro(GMUSICBROWSER_TITLE);\
	Timer(GMUSICBROWSER_TITLE,$(UpdateTimeout),0);

APP_PLAY_STATUS=\
	Macro(GMUSICBROWSER_STATUS);\
	Make(var,app_status,if [ "x$(gmusicbrowser_status)" = "xPlaying" ]; then echo playing; else echo paused; fi);

Layout 3x4=\
	Make(mode,gmusicbrowser_3x4);

Layout 7x1=\
	Make(mode,gmusicbrowser_7x1);

Layout 5x1=\
	Make(mode,gmusicbrowser_5x1);

[ModeEnd]

[Mode]=gmusicbrowser_3x4 : gmusicbrowser_general, default_player

% (EnterMode) by Utils/aliases-server-player.cfg

APP_MENU=\
	Set(menu,replace,Repeat,Layout 7x1,Layout 5x1,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,gmusicbrowser_layout,by_value,3x4);\
	Macro(GMUSICBROWSER_CHECK_TIMER);

% PowerMgmt by Utils/aliases-server.cfg

GMUSICBROWSER_TITLE=\
	Macro(GMUSICBROWSER_TITLE_GENERAL);

[ModeEnd]

[Mode]=gmusicbrowser_cover

GMUSICBROWSER_TITLE=\
	Macro(GMUSICBROWSER_TITLE_GENERAL);\
	Macro(APP_COVER, if [ "x$(cover_size)" = "x" ]; then echo 1; else echo 0; fi);

APP_COVER=\
	Make(var,media_file,$(gmusicbrowser_metadata)|awk '/.*xesam.url.*/{getline;print}'|cut -f 2 -d '"'|cut -f 2 -d ':'|sed 's/"$//;s/^\/\/\//\//;s/%20/ /g');\
	Macro(DEFAULT_COVER);

[ModeEnd]

[Mode]=gmusicbrowser_7x1 : gmusicbrowser_general, default_player_7x1, gmusicbrowser_cover

% (EnterMode) by Utils/aliases-server-player.cfg
% Mute, Next Cover,Previous Cover,Update Cover, PowerMgmt by Utils/aliases-server-player.cfg
APP_MENU=\
	Set(menu,replace,Repeat,Mute,Next Cover,Previous Cover,Update Cover,Layout 3x4,Layout 5x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,gmusicbrowser_layout,by_value,7x1);\
	Macro(GMUSICBROWSER_CHECK_TIMER);

[ModeEnd]

[Mode]=gmusicbrowser_5x1 : gmusicbrowser_general, default_player_5x1, gmusicbrowser_cover

% (EnterMode) by Utils/aliases-server-player.cfg
% Mute, Next Cover,Previous Cover,Update Cover, PowerMgmt by Utils/aliases-server-player.cfg
APP_MENU=\
	Set(menu,replace,Repeat,Mute,Next Cover,Previous Cover,Update Cover,Layout 3x4,Layout 7x1,Help,PowerMgmt);\
	Macro(ALLIN1MENU);\
	Macro(MEDIACENTER_MENU);

APP_POST_SETUP=\
	Make(var,gmusicbrowser_layout,by_value,5x1);

APP_TITLE=\
	Macro(GMUSICBROWSER_TITLE);

PLAY=\
	Macro(PLAY_ONLY);

[ModeEnd]

[End]
