-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathinstall.sh
executable file
·234 lines (206 loc) · 8.91 KB
/
install.sh
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
226
227
228
229
230
231
232
233
234
#!/bin/bash
set -e
# Argument parsing
SHORT=r,d:,p:,s:,h
LONG=remove,dest-dir:,prefix-dir:,sysconf-dir:,no-ectool,no-pre-uninstall,no-post-install,no-battery-sensors,no-sudo,help
VALID_ARGS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
fi
TEMP_FOLDER='./.temp'
trap 'rm -rf $TEMP_FOLDER' EXIT
PREFIX_DIR="/usr"
DEST_DIR=""
SYSCONF_DIR="/etc"
SHOULD_INSTALL_ECTOOL=true
SHOULD_PRE_UNINSTALL=true
SHOULD_POST_INSTALL=true
SHOULD_REMOVE=false
NO_BATTERY_SENSOR=false
NO_SUDO=false
eval set -- "$VALID_ARGS"
while true; do
case "$1" in
'--remove' | '-r')
SHOULD_REMOVE=true
;;
'--prefix-dir' | '-p')
PREFIX_DIR=$2
shift
;;
'--dest-dir' | '-d')
DEST_DIR=$2
shift
;;
'--sysconf-dir' | '-s')
SYSCONF_DIR=$2
shift
;;
'--no-ectool')
SHOULD_INSTALL_ECTOOL=false
;;
'--no-pre-uninstall')
SHOULD_PRE_UNINSTALL=false
;;
'--no-post-install')
SHOULD_POST_INSTALL=false
;;
'--no-battery-sensors')
NO_BATTERY_SENSOR=true
;;
'--no-sudo')
NO_SUDO=true
;;
'--help' | '-h')
echo "Usage: $0 [--remove,-r] [--dest-dir,-d <installation destination directory (defaults to $DEST_DIR)>] [--prefix-dir,-p <installation prefix directory (defaults to $PREFIX_DIR)>] [--sysconf-dir,-s system configuration destination directory (defaults to $SYSCONF_DIR)] [--no-ectool] [--no-post-install] [--no-pre-uninstall] [--no-sudo]" 1>&2
exit 0
;;
--)
break
;;
esac
shift
done
# Root check
if [ "$EUID" -ne 0 ] && [ "$NO_SUDO" = false ]
then echo "This program requires root permissions or use the '--no-sudo' option"
exit 1
fi
SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"
SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"
SERVICES_SUBCONFIGS="$(cd "$SERVICES_DIR" && find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)"
function sanitizePath() {
local SANITIZED_PATH="$1"
local SANITIZED_PATH=${SANITIZED_PATH//..\//}
local SANITIZED_PATH=${SANITIZED_PATH#./}
local SANITIZED_PATH=${SANITIZED_PATH#/}
echo "$SANITIZED_PATH"
}
# remove remaining legacy files
function uninstall_legacy() {
echo "removing legacy files"
rm "/usr/local/bin/fw-fanctrl" 2> "/dev/null" || true
rm "/usr/local/bin/ectool" 2> "/dev/null" || true
rm "/usr/local/bin/fanctrl.py" 2> "/dev/null" || true
rm "/etc/systemd/system/fw-fanctrl.service" 2> "/dev/null" || true
}
function uninstall() {
if [ "$SHOULD_PRE_UNINSTALL" = true ]; then
./pre-uninstall.sh "$([ "$NO_SUDO" = true ] && echo "--no-sudo")"
fi
# remove program services based on the services present in the './services' folder
echo "removing services"
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
# be EXTRA CAREFUL about the validity of the paths (dont wanna delete something important, right?... O_O)
rm -rf "$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION"
done
# remove program services sub-configurations based on the sub-configurations present in the './services' folder
echo "removing services sub-configurations"
for SERVICE in $SERVICES_SUBCONFIGS ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "removing sub-configurations for [$SERVICE]"
SUBCONFIGS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -type f)"
for SUBCONFIG in $SUBCONFIGS ; do
SUBCONFIG=$(sanitizePath "$SUBCONFIG")
echo "removing '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG'"
rm -rf "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG" 2> "/dev/null" || true
done
done
rm "$DEST_DIR$PREFIX_DIR/bin/fw-fanctrl" 2> "/dev/null" || true
ectool autofanctrl 2> "/dev/null" || true # restore default fan manager
if [ "$SHOULD_INSTALL_ECTOOL" = true ]; then
rm "$DEST_DIR$PREFIX_DIR/bin/ectool" 2> "/dev/null" || true
fi
rm -rf "$DEST_DIR$SYSCONF_DIR/fw-fanctrl" 2> "/dev/null" || true
rm -rf "/run/fw-fanctrl" 2> "/dev/null" || true
uninstall_legacy
}
function install() {
uninstall_legacy
rm -rf "$TEMP_FOLDER"
mkdir -p "$DEST_DIR$PREFIX_DIR/bin"
if [ "$SHOULD_INSTALL_ECTOOL" = true ]; then
mkdir "$TEMP_FOLDER"
installEctool "$TEMP_FOLDER" || (echo "an error occurred when installing ectool." && echo "please check your internet connection or consider installing it manually and using --no-ectool on the installation script." && exit 1)
rm -rf "$TEMP_FOLDER"
fi
mkdir -p "$DEST_DIR$SYSCONF_DIR/fw-fanctrl"
cp "./fanctrl.py" "$DEST_DIR$PREFIX_DIR/bin/fw-fanctrl"
chmod +x "$DEST_DIR$PREFIX_DIR/bin/fw-fanctrl"
cp -n "./config.json" "$DEST_DIR$SYSCONF_DIR/fw-fanctrl" 2> "/dev/null" || true
# add --no-battery-sensors flag to the fanctrl service if specified
if [ "$NO_BATTERY_SENSOR" = true ]; then
NO_BATTERY_SENSOR_OPTION="--no-battery-sensors"
fi
# create program services based on the services present in the './services' folder
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/system'"
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/system"
echo "creating services"
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
if [ "$SHOULD_PRE_UNINSTALL" = true ] && [ "$(systemctl is-active "$SERVICE")" == "active" ]; then
echo "stopping [$SERVICE]"
systemctl stop "$SERVICE"
fi
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION'"
cat "$SERVICES_DIR/$SERVICE$SERVICE_EXTENSION" | sed -e "s/%PREFIX_DIRECTORY%/${PREFIX_DIR//\//\\/}/" | sed -e "s/%SYSCONF_DIRECTORY%/${SYSCONF_DIR//\//\\/}/" | sed -e "s/%NO_BATTERY_SENSOR_OPTION%/${NO_BATTERY_SENSOR_OPTION}/" | tee "$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION" > "/dev/null"
done
# add program services sub-configurations based on the sub-configurations present in the './services' folder
echo "adding services sub-configurations"
for SERVICE in $SERVICES_SUBCONFIGS ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "adding sub-configurations for [$SERVICE]"
SUBCONFIG_FOLDERS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)"
# ensure folders exists
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE"
for SUBCONFIG_FOLDER in $SUBCONFIG_FOLDERS ; do
SUBCONFIG_FOLDER=$(sanitizePath "$SUBCONFIG_FOLDER")
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG_FOLDER'"
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG_FOLDER"
done
SUBCONFIGS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -type f)"
# add sub-configurations
for SUBCONFIG in $SUBCONFIGS ; do
SUBCONFIG=$(sanitizePath "$SUBCONFIG")
echo "adding '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG'"
cat "$SERVICES_DIR/$SERVICE/$SUBCONFIG" | sed -e "s/%PREFIX_DIRECTORY%/${PREFIX_DIR//\//\\/}/" | tee "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG" > "/dev/null"
chmod +x "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG"
done
done
if [ "$SHOULD_POST_INSTALL" = true ]; then
./post-install.sh --dest-dir "$DEST_DIR" --sysconf-dir "$SYSCONF_DIR" "$([ "$NO_SUDO" = true ] && echo "--no-sudo")"
fi
}
function installEctool() {
workingDirectory=$1
echo "installing ectool"
ectoolDestPath="$DEST_DIR$PREFIX_DIR/bin/ectool"
ectoolJobId="$(cat './fetch/ectool/linux/gitlab_job_id')"
ectoolSha256Hash="$(cat './fetch/ectool/linux/hash.sha256')"
artifactsZipFile="$workingDirectory/artifact.zip"
echo "downloading artifact from gitlab"
curl -s -S -o "$artifactsZipFile" -L "https://gitlab.howett.net/DHowett/ectool/-/jobs/${ectoolJobId}/artifacts/download?file_type=archive" || (echo "failed to download the artifact." && return 1)
if [[ $? -ne 0 ]]; then return 1; fi
echo "checking artifact sha256 sum"
actualEctoolSha256Hash=$(sha256sum "$artifactsZipFile" | cut -d ' ' -f 1)
if [[ "$actualEctoolSha256Hash" != "$ectoolSha256Hash" ]]; then
echo "Incorrect sha256 sum for ectool gitlab artifact '$ectoolJobId' : '$ectoolSha256Hash' != '$actualEctoolSha256Hash'"
return 1
fi
echo "extracting artifact"
{
unzip -q -j "$artifactsZipFile" '_build/src/ectool' -d "$workingDirectory" &&
cp "$workingDirectory/ectool" "$ectoolDestPath" &&
chmod +x "$ectoolDestPath"
} || (echo "failed to extract the artifact to its designated location." && return 1)
if [[ $? -ne 0 ]]; then return 1; fi
echo "ectool installed"
}
if [ "$SHOULD_REMOVE" = true ]; then
uninstall
else
install
fi
exit 0