~vcs-imports/kupfer/master-new

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# kupfer      A convenient command and access tool
#
# Copyright 2007--2010 Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This script invokes kupfer by calling for an already running instance, or
# starting a new if none is found running.

PYTHON="@PYTHON@"
PYTHONDIR="@PYTHONDIR@"
test ${PYTHON:0:1} = "@" && PYTHON=python2.6
test ${PYTHONDIR:0:1} = "@" && PYTHONDIR=$(dirname $(realpath $0))/..

# We allow either ``kupfer QUERY`` to pass text on the command line,
# or reading directly from stdin if we pipe text into Kupfer
if ! tty --quiet
then
	echo "kupfer: Reading from stdin"
	TEXT_INPUT=$(cat)
fi

# If there are any options, like "--help", we run Kupfer directly
test "x${1:0:2}" = "x--"
KUPFER_HAS_OPTIONS=$?

test $KUPFER_HAS_OPTIONS != 0 && dbus-send --type=method_call --print-reply \
	--dest=se.kaizer.kupfer /interface se.kaizer.kupfer.Listener.Present \
	>/dev/null 2>&1
KUPFER_RUNNING=$?

if test \( -n "$TEXT_INPUT" -a $KUPFER_HAS_OPTIONS != 0 \)
then
	dbus-send --type=method_call --dest=se.kaizer.kupfer /interface \
		se.kaizer.kupfer.Listener.PutText string:"$TEXT_INPUT"
fi

_realpaths () {
	# Emit realpaths for arguments, separated by NUL bytes
	while test $# != 0
	do
		echo -ne $(realpath "$1")"\0"
		shift
	done
}

if test \( -n "$1" -a $KUPFER_HAS_OPTIONS != 0 \)
then
	# NOTE: We must escape commas here since it is dbus-send's
	# array item separator. This is then unescaped by kupfer
	ARRAY=$(_realpaths "$@" | sed -e 's/,/%%kupfercomma%%/g' | tr \\0 ,)
	dbus-send --type=method_call --dest=se.kaizer.kupfer /interface  \
		se.kaizer.kupfer.Listener.PutFiles array:string:"$ARRAY"
fi



if test $KUPFER_RUNNING != 0
then
	exec ${PYTHON} "$PYTHONDIR/kupfer.py" $*
fi

if test -n "$DESKTOP_STARTUP_ID"
then
	${PYTHON} -c "import gtk.gdk; gtk.gdk.notify_startup_complete()"
fi