blob: 6e6c205365ece045bc78dec876fb3f7b2b6429ac [file] [log] [blame]
dkegel@google.comf20da1f2009-06-30 19:16:32 +00001
2#----------------------------------------------------------------------------
3# Common utility functions included in all XDG wrapper scripts
4#----------------------------------------------------------------------------
5
6DEBUG()
7{
8 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
9 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
10 shift
11 echo "$@" >&2
12}
13
14#-------------------------------------------------------------
15# Exit script on successfully completing the desired operation
16
17exit_success()
18{
19 if [ $# -gt 0 ]; then
20 echo "$@"
21 echo
22 fi
23
24 exit 0
25}
26
27
28#-----------------------------------------
29# Exit script on malformed arguments, not enough arguments
30# or missing required option.
31# prints usage information
32
33exit_failure_syntax()
34{
35 if [ $# -gt 0 ]; then
36 echo "@NAME@: $@" >&2
37 echo "Try '@NAME@ --help' for more information." >&2
38 else
39 usage
40 echo "Use 'man @NAME@' or '@NAME@ --manual' for additional info."
41 fi
42
43 exit 1
44}
45
46#-------------------------------------------------------------
47# Exit script on missing file specified on command line
48
49exit_failure_file_missing()
50{
51 if [ $# -gt 0 ]; then
52 echo "@NAME@: $@" >&2
53 fi
54
55 exit 2
56}
57
58#-------------------------------------------------------------
59# Exit script on failure to locate necessary tool applications
60
61exit_failure_operation_impossible()
62{
63 if [ $# -gt 0 ]; then
64 echo "@NAME@: $@" >&2
65 fi
66
67 exit 3
68}
69
70#-------------------------------------------------------------
71# Exit script on failure returned by a tool application
72
73exit_failure_operation_failed()
74{
75 if [ $# -gt 0 ]; then
76 echo "@NAME@: $@" >&2
77 fi
78
79 exit 4
80}
81
82#------------------------------------------------------------
83# Exit script on insufficient permission to read a specified file
84
85exit_failure_file_permission_read()
86{
87 if [ $# -gt 0 ]; then
88 echo "@NAME@: $@" >&2
89 fi
90
91 exit 5
92}
93
94#------------------------------------------------------------
mdm@chromium.org56df6d32010-08-31 18:16:49 +000095# Exit script on insufficient permission to write a specified file
dkegel@google.comf20da1f2009-06-30 19:16:32 +000096
97exit_failure_file_permission_write()
98{
99 if [ $# -gt 0 ]; then
100 echo "@NAME@: $@" >&2
101 fi
102
103 exit 6
104}
105
106check_input_file()
107{
108 if [ ! -e "$1" ]; then
109 exit_failure_file_missing "file '$1' does not exist"
110 fi
111 if [ ! -r "$1" ]; then
112 exit_failure_file_permission_read "no permission to read file '$1'"
113 fi
114}
115
116check_vendor_prefix()
117{
118 file_label="$2"
119 [ -n "$file_label" ] || file_label="filename"
120 file=`basename "$1"`
121 case "$file" in
122 [a-zA-Z]*-*)
123 return
124 ;;
125 esac
126
127 echo "@NAME@: $file_label '$file' does not have a proper vendor prefix" >&2
128 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
129 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
130 echo "Use --novendor to override or '@NAME@ --manual' for additional info." >&2
131 exit 1
132}
133
134check_output_file()
135{
136 # if the file exists, check if it is writeable
137 # if it does not exists, check if we are allowed to write on the directory
138 if [ -e "$1" ]; then
139 if [ ! -w "$1" ]; then
140 exit_failure_file_permission_write "no permission to write to file '$1'"
141 fi
142 else
143 DIR=`dirname "$1"`
144 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
145 exit_failure_file_permission_write "no permission to create file '$1'"
146 fi
147 fi
148}
149
150#----------------------------------------
151# Checks for shared commands, e.g. --help
152
153check_common_commands()
154{
155 while [ $# -gt 0 ] ; do
156 parm="$1"
157 shift
158
159 case "$parm" in
160 --help)
161 usage
162 echo "Use 'man @NAME@' or '@NAME@ --manual' for additional info."
163 exit_success
164 ;;
165
166 --manual)
167 manualpage
168 exit_success
169 ;;
170
171 --version)
172 echo "@NAME@ 1.0.2"
173 exit_success
174 ;;
175 esac
176 done
177}
178
179check_common_commands "$@"
180
181[ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
182if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
183 # Be silent
184 xdg_redirect_output=" > /dev/null 2> /dev/null"
185else
186 # All output to stderr
187 xdg_redirect_output=" >&2"
188fi
189
190#--------------------------------------
191# Checks for known desktop environments
192# set variable DE to the desktop environments name, lowercase
193
194detectDE()
195{
196 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
197 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000198 elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
199 elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000200 fi
201}
202
203#----------------------------------------------------------------------------
204# kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
205# It also always returns 1 in KDE 3.4 and earlier
206# Simply return 0 in such case
207
208kfmclient_fix_exit_code()
209{
210 version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE`
211 major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
212 minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
213 release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
214 test "$major" -gt 3 && return $1
215 test "$minor" -gt 5 && return $1
216 test "$release" -gt 4 && return $1
217 return 0
218}