1#!/usr/local/bin/bash
2
3#
4# cantata-remote
5#
6#  Copyright (c) 2011-2014 Craig Drummond <craig.p.drummond@gmail.com>
7#
8#  ----
9#
10#  This program is free software; you can redistribute it and/or modify
11#  it under the terms of the GNU General Public License as published by
12#  the Free Software Foundation; either version 2 of the License, or
13#  (at your option) any later version.
14#
15#  This program is distributed in the hope that it will be useful,
16#  but WITHOUT ANY WARRANTY; without even the implied warranty of
17#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18#  General Public License for more details.
19#
20#  You should have received a copy of the GNU General Public License
21#  along with this program; see the file COPYING.  If not, write to
22#  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23#  Boston, MA 02110-1301, USA.
24#
25#  ---------------------------------------------------------------------
26#
27#  This shell scrpt is intended to be invoked from the Cantata badge on
28#  the unity task bar.
29#
30
31function usage() {
32    echo "$0 cantata <cantata action>"
33    echo "$0 <mpris action>"
34}
35
36if ([ $# -eq 2 ] && [ "cantata" != "$1" ])  || [ $# == 0 ] || [ $# -gt 2 ] ; then
37    usage
38fi
39
40if [ $1 = "cantata" ] ; then
41    path="/cantata"
42    methodPrefix=mpd.cantata
43    method="triggerAction"
44    methodArgument=$2
45else
46    path=/org/mpris/MediaPlayer2
47    methodPrefix=org.mpris.MediaPlayer2.Player
48    method=$1
49fi
50
51service=@PROJECT_REV_URL@
52
53# If we have qdbus use that...
54qt=`which qdbus`
55if [ "$qt" != "" ] ; then
56    $qt $service > /dev/null
57    if [ $? -ne 0 ] ; then
58        # Cantata not started? Try to start...
59        cantata &
60        sleep 1s
61    fi
62    $qt $service $path $method $methodArgument > /dev/null
63    exit
64fi
65
66# No qdbus so try dbus-send...
67dbus=`which dbus-send`
68if [ "$dbus" != "" ] ; then
69    status=`$dbus --print-reply --reply-timeout=250 --type=method_call --session --dest=$service $path org.freedesktop.DBus.Peer.Ping 2>&1 | grep "org.freedesktop.DBus.Error.ServiceUnknown" | wc -l`
70    if [ "$status" != "0" ] ; then
71        # Cantata not started? Try to start...
72        cantata &
73        sleep 1s
74    fi
75    if [ "$methodArgument" != "" ] ; then
76        methodArgument="string:$methodArgument"
77    fi
78    echo "dbus-send --type=method_call --session --dest=$service $path $methodPrefix.$method $methodArgument"
79    dbus-send --type=method_call --session --dest=$service $path $methodPrefix.$method $methodArgument
80fi
81