-
Notifications
You must be signed in to change notification settings - Fork 46
/
install
executable file
·201 lines (163 loc) · 5.75 KB
/
install
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
#!/bin/bash
set -e
if [[ "$USER" == root ]]; then
echo "Please run 'bash install' as non-root user" 2>&1
exit 1
fi
DIR="$(dirname "$0")"
VERSION="$(cat $DIR/VERSION | tr -d '[:space:]')"
# target executable
SOURCE="$DIR/comfortable-swipe"
TARGET="/usr/local/bin/comfortable-swipe"
# compile targets
COMPILE="$DIR/compile"
COMPILE_SOURCE="$DIR/comfortable-swipe-main.cpp"
COMPILE_TARGET="/usr/local/bin/comfortable-swipe-buffer"
# configurations
CONF_SOURCE="$DIR/config/comfortable-swipe.conf"
CONF_TARGET="$HOME/.config/comfortable-swipe.conf"
OLD_CONF_TARGET="/usr/local/share/comfortable-swipe/comfortable-swipe.conf"
# autostart
AUTOSTART_SOURCE="$DIR/config/comfortable-swipe.desktop"
AUTOSTART_TARGET="$HOME/.config/autostart/comfortable-swipe.desktop"
# stop any running comfortable-swipe if it exists
comfortable-swipe stop > /dev/null 2>&1 || true
# shorthand to abort the installation
function abort {
echo "Installation aborted" >&2
exit 1
}
# shorthand to try a command with sudo if it fails
function trysudo {
$@ 2> /dev/null || sudo $@
}
# uninstall existing program
function uninstall_existing {
local PROGRAM="${1:?missing program name}"
# remove existing comfortable-swipe
if [[ -x "$(command -v "$PROGRAM")" ]]; then
echo -n "Removing previous $(which "$PROGRAM") ... "
trysudo rm -f "$(which "$PROGRAM")"
echo "ok"
fi
}
## CONFIGURATION FILE LOADING
# This section handles installation of configuration file.
# We determine first if any old configuration files exists.
# make sure config directory exists
function install_configuration_file {
mkdir -p "$(dirname "$CONF_TARGET")"
# check if configuration already exists
if [[ -f "$CONF_TARGET" ]]; then
# ask user if we overwrite configuration
echo "Old conf file found in $CONF_TARGET" >&2
read -r -p "Keep the old conf file? (default: yes) [Y/n] " response >&2
# If response is empty, consider it as 'yes' (default)
if [[ -z "$response" ]]; then
response="y"
fi
if [[ "${response,,}" =~ ^(no|n)$ ]]; then
# MAKE SURE they really want to overwrite
read -r -p "Conf file will be overwritten! Are you sure? [Y/n] " response >&2
if [[ -z "$response" ]]; then
response="y"
fi
if [[ "${response,,}" =~ ^(no|n)$ ]]; then
abort
else
# They agreed... replace configuration
cat "$CONF_SOURCE" > "$CONF_TARGET"
fi
fi
else
# target does not exist yet
# try copying from older configurations
if [[ -f "$OLD_CONF_TARGET" ]]; then
# move configuration from an older version (< v1.2)
echo "Moving configuration from $OLD_CONF_TARGET to $CONF_TARGET ..."
mv "$OLD_CONF_TARGET" "$CONF_TARGET"
# clean up old directory if possible
rmdir "$(dirname "$OLD_CONF_TARGET")" > /dev/null 2>&1 || true
else
# fresh installation! just copy default configuration
cat "$CONF_SOURCE" > "$CONF_TARGET"
fi
fi
echo "Installed: $CONF_TARGET"
}
## Installation of main program
#
# /usr/local/bin/comfortable-swipe
#
function install_main_program {
# copy source to target with executable permissions
# install to target, with hardcoded version
if [[ -f "$TARGET" ]]; then
trysudo rm "$TARGET"
fi
trysudo cp "$SOURCE" "$TARGET"
trysudo sed -E "s/^VERSION=.*/VERSION=$VERSION/" -i "$TARGET"
# allow execute permissions with group
trysudo chmod +x "$TARGET"
# make sure non-root user is owner
trysudo chown "$USER" "$TARGET"
echo "Installed: $TARGET"
}
## Compilation and installation of C++ buffer program:
#
# /usr/local/bin/comfortable-swipe-buffer
#
function install_cpp_program {
# compile program to temporary file first
TMP_TARGET="$(mktemp)"
$COMPILE "$COMPILE_SOURCE" -o "$TMP_TARGET" -DCOMFORTABLE_SWIPE_VERSION="\"$VERSION\"" -DCOMFORTABLE_SWIPE_CONFIG="\"$CONF_TARGET\"" -DCOMFORTABLE_SWIPE_AUTOSTART="\"$AUTOSTART_TARGET\""
# compilation ok, now try to install with sudo
trysudo mkdir -p "$(dirname "$COMPILE_TARGET")"
# remove existing file for permissions to work
if [[ -f "$COMPILE_TARGET" ]]; then
sudo rm "$COMPILE_TARGET"
fi
trysudo mv "$TMP_TARGET" "$COMPILE_TARGET"
# bugfix: add with group permissions
trysudo chmod go+x "$COMPILE_TARGET"
# make sure non-root user is owner
trysudo chown "$USER" "$COMPILE_TARGET"
echo "Installed: $COMPILE_TARGET"
}
## Installation of Autostart Desktop file
#
# /home/$USER/.config/autostart/comfortable-swipe.desktop
#
function install_autostart {
mkdir -p "$(dirname "$AUTOSTART_TARGET")"
cat "$AUTOSTART_SOURCE" > "$AUTOSTART_TARGET"
echo "Installed: $AUTOSTART_TARGET"
}
# install configuration files first but defer output later
# this is to ensure that the prompt runs first in the script
INSTALL_CONF_OUTPUT="$(install_configuration_file)"
# uninstall existing commands
uninstall_existing comfortable-swipe
uninstall_existing comfortable-swipe-buffer
# install new binaries
echo "Installing binaries ..."
install_cpp_program
install_main_program
echo "$INSTALL_CONF_OUTPUT"
install_autostart
# add permissions to input group (defer)
# GROUP=$(ls -l /dev/input/event* | awk '{print $4}' | head --line=1) || abort
# make sure comfortable-swipe is in path
if ! command -v comfortable-swipe > /dev/null 2>&1; then
cat <<EOF >&2
WARNING: $(dirname "$TARGET") is not in PATH!
Make sure to add the following in your ~/.profile:
export PATH="$PATH:$(dirname "$TARGET")"
EOF
fi
cat <<EOF
Successfully installed comfortable-swipe $VERSION
Autostart switched $("$TARGET" autostart status)
EOF
# start the program
"$TARGET" start