forked from bulletmark/libinput-gestures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibinput-gestures-setup
executable file
·225 lines (199 loc) · 5.48 KB
/
libinput-gestures-setup
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# User setup script.
# (C) Mark Blakeney, Aug 2016.
PROG="$(basename $0)"
NAME=${PROG%-*}
BINDIR="/usr/bin"
APPDIR="/usr/share/applications"
ICOBAS="/usr/share/icons/hicolor"
ICODIR="$ICOBAS/128x128/apps"
OCODIR="/usr/share/pixmaps"
DOCDIR="/usr/share/doc/$NAME"
CNFDIR="/etc"
HCFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
AUTDIR="$HCFDIR/autostart"
usage() {
echo "Usage:"
echo "As root: sudo $PROG install|uninstall"
echo "As user: $PROG start|stop|restart|autostart|autostop|status"
echo
echo "-d <dir> (option sets DESTDIR for install/uninstall)"
echo "-r (force allow root to perform user commands. PLEASE AVOID USING THIS!)"
exit 1
}
# Process command line options
DESTDIR=""
FORCEROOT=0
while getopts d:r c; do
case $c in
d) DESTDIR="$OPTARG";;
r) FORCEROOT=1;;
\?) usage;;
esac
done
shift $((OPTIND - 1))
if [[ $# -ne 1 ]]; then
usage
fi
cmd="$1"
# Launch given desktop app. First work out most suitable launcher.
# Pretty crude at present but should work for at least GNOME and KDE.
launch() {
local app="$1"
local fullpath="$APPDIR/$app.desktop"
local binpath="$APPBIN/$app"
# All the commands we will potentially try ..
local cmds=(
"kde kioclient5 exec $fullpath"
"kde kioclient exec $fullpath"
"all gtk-launch $app"
"all i3-msg exec $binpath"
"all exo-open $fullpath"
"all dex $fullpath"
)
local cmdline
for cmdline in "${cmds[@]}" ; do
IFS=' ' read de cmd args <<< "$cmdline"
# Skip if the command does not exist
if ! hash $cmd &>/dev/null; then
continue
fi
# Only try KDE commands on KDE
if ! echo $XDG_CURRENT_DESKTOP | grep -q KDE; then
if [[ $de == kde ]]; then
continue
fi
fi
# Execute this command
$cmd $args &>/dev/null
return $?
done
echo "Don't know how to invoke $app.desktop" >&2
return 1
}
# Set up desktop entry link for auto start of app, if it doesn't already
# exist
auto_start() {
if [[ ! -f $APPDIR/$NAME.desktop ]]; then
if [[ -e $AUTDIR/$NAME.desktop ]]; then
echo "Removed old $AUTDIR/$NAME.desktop"
rm -f $AUTDIR/$NAME.desktop
fi
return 1
fi
if ! cmp -s $APPDIR/$NAME.desktop $AUTDIR/$NAME.desktop; then
if mkdir -p $AUTDIR && cp $APPDIR/$NAME.desktop $AUTDIR; then
echo "installed or updated $AUTDIR/$NAME.desktop"
fi
fi
return 0
}
# Action given user command
user_action() {
local cmd=$1
if [[ $cmd == start ]]; then
if [[ ! -f $APPDIR/$NAME.desktop ]]; then
echo "$NAME is not installed."
exit 1
fi
if launch "$NAME"; then
echo "$NAME started."
fi
elif [[ $cmd == stop ]]; then
for prog in libinput-debug-events $NAME; do
if pkill -u $USER -f "$prog\$|$prog " &>/dev/null; then
echo "$prog stopped."
fi
done
elif [[ $cmd == autostart ]]; then
if ! auto_start; then
echo "$NAME is not installed."
exit 1
fi
elif [[ $cmd == autostop ]]; then
rm -fv $AUTDIR/$NAME.desktop
elif [[ $cmd == status ]]; then
if [[ -f $APPDIR/$NAME.desktop ]]; then
echo "$NAME is installed."
else
echo "$NAME is not installed."
fi
if [[ -f $AUTDIR/$NAME.desktop ]]; then
echo "$NAME is set to autostart."
else
echo "$NAME is not set to autostart."
fi
if pgrep -u $USER -f "$NAME\$|$NAME " &>/dev/null; then
echo "$NAME is running."
else
echo "$NAME is not running."
fi
if [[ -f $HCFDIR/$NAME.conf ]]; then
echo "$NAME is using custom configuration."
else
echo "$NAME is using default configuration."
fi
else
usage
fi
}
if [[ $cmd == install || $cmd == uninstall ]]; then
DESTDIR="${DESTDIR%%+(/)}"
if [[ -z $DESTDIR && $(id -un) != root ]]; then
echo "Install or uninstall must be run as sudo/root."
exit 1
fi
# Remove any old files from earlier versions of program
rm -f $DESTDIR$OCODIR/$NAME.png
rm -f $DESTDIR$ICODIR/$NAME.png
if [[ $cmd == install ]]; then
install -CDv -m 755 -t $DESTDIR$BINDIR $NAME-setup
install -CDv -m 755 -t $DESTDIR$BINDIR $NAME
install -CDv -m 644 -t $DESTDIR$APPDIR $NAME.desktop
install -CDv -m 644 -t $DESTDIR$ICODIR $NAME.svg
install -CDv -m 644 -t $DESTDIR$CNFDIR $NAME.conf
install -CDv -m 644 -t $DESTDIR$DOCDIR README.md
else
rm -rfv $DESTDIR$BINDIR/$NAME
rm -rfv $DESTDIR$APPDIR/$NAME.desktop
rm -rfv $DESTDIR$ICODIR/$NAME.svg
rm -rfv $DESTDIR$CNFDIR/$NAME.conf
rm -rfv $DESTDIR$DOCDIR
rm -rfv $DESTDIR$BINDIR/$NAME-setup
fi
if [[ -z $DESTDIR ]]; then
if [[ -x /usr/bin/update-desktop-database ]]; then
/usr/bin/update-desktop-database -q
fi
if [[ -x /usr/bin/gtk-update-icon-cache ]]; then
/usr/bin/gtk-update-icon-cache $ICOBAS
fi
fi
else
if [[ $(id -un) == root && $FORCEROOT == 0 ]]; then
echo "Non-installation commands must be run as your own user."
exit 1
fi
# Remove any old configuration from earlier versions of program
rm -fv ~/bin/$NAME 2>/dev/null
rm -fv ~/.local/bin/$NAME 2>/dev/null
rm -fv ~/.local/share/applications/$NAME.desktop 2>/dev/null
rm -fv ~/.local/share/icons/$NAME.png 2>/dev/null
# Look for and update any autostart file if it is a link or not
# pointing to the latest desktop entry. Apparently user autostart
# files should not be symlinks to system dir files.
if [[ -e $AUTDIR/$NAME.desktop ]]; then
if [[ -L $AUTDIR/$NAME.desktop ]]; then
echo "Removed old $AUTDIR/$NAME.desktop link"
rm -f $AUTDIR/$NAME.desktop
fi
auto_start
fi
if [[ $cmd == restart ]]; then
user_action "stop"
user_action "start"
else
user_action $cmd
fi
fi
exit 0