-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_script
executable file
·217 lines (178 loc) · 5.27 KB
/
install_script
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
#!/usr/bin/env bash
function usage () {
echo
echo "Usage"
echo " $0 username"
echo
}
function fancy_message() {
# $1 = type , $2 = message
# Message types
# 0 - info
# 1 - warning
# 2 - error
if [ -z "${1}" ] || [ -z "${2}" ]; then
return
fi
local RED="\e[31m"
local GREEN="\e[32m"
local YELLOW="\e[33m"
local RESET="\e[0m"
local MESSAGE_TYPE=""
local MESSAGE=""
MESSAGE_TYPE="${1}"
MESSAGE="${2}"
case ${MESSAGE_TYPE} in
info) echo -e " [${GREEN}+${RESET}] INFO: ${MESSAGE}";; # Previously "0"
warn) echo -e " [${YELLOW}*${RESET}] WARNING: ${MESSAGE}";; # Previously "1"
error) echo -e " [${RED}!${RESET}] ERROR: ${MESSAGE}";; # Previously "2"
*) echo -e " [?] UNKNOWN: ${MESSAGE}";;
esac
}
fancy_message info "Welcome to the yablo installer. The Automatic Battery Optimizer for Linux 🐧."
# Check if the user running the script is root
if [ "$(id -u)" -ne 0 ]; then
fancy_message error "You need to be root."
exit 1
fi
# Take command line arguments
if [ $# -eq 0 ]; then
fancy_message warn "No arguments supplied"
usage
exit 1
fi
if [ -z "$1" ]; then
fancy_message warn "No arguments supplied"
usage
exit 1
fi
USERNAME=$1
# check if already installed
path_to_executable=$(which yablo 2> /dev/null)
if [ -x "$path_to_executable" ] ; then
fancy_message warn "Already installed at ${path_to_executable}"
echo ""
read -p "Do you want to update your installation? [y/N]" -n 1 -r
echo ""
UPDATE=0
if [[ ${REPLY} =~ ^[Yy]$ ]]; then
fancy_message info "Updating installation"
UPDATE=1
else
exit 1
fi
else
fancy_message info "No installations detected."
fi
# check if systemd is on system
if [ ! -x "$(which systemctl 2> /dev/null )" ] ; then
fancy_message error "systemd is not available on your system. You need to manually install it."
exit 1
else
fancy_message info "Found systemd."
fi
fancy_message info "All checks passed."
echo ""
read -p "Are you sure you want to install yablo? [y/N]" -n 1 -r
echo ""
if [[ ${REPLY} =~ ^[Yy]$ ]]; then
fancy_message info "Starting installation."
echo ""
read -p "Do you want to build it locally? [y/N]" -n 1 -r
echo ""
INSTALL_PATH="/usr/local/bin/yablo"
INSTALL_PATH_REMOVE="/usr/local/bin/yablo_remove"
if [[ ${REPLY} =~ ^[Yy]$ ]]; then
# check if rust is available on system
path_to_executable=$(which cargo 2> /dev/null)
if [ -x "$path_to_executable" ] ; then
fancy_message warn "Rust is on system"
else
fancy_message error "Rust is not installed on system. Exit."
exit 1
fi
cargo build --release
if [ $? -eq 0 ]; then
fancy_message info "Successfully build binary"
else
fancy_message error "Build didn't finish successful"
exit 1
fi
if [ ${UPDATE} -eq 1 ]; then
fancy_message info "Stopping systemd service"
systemctl stop yablo.service
fi
cp target/release/yablo ${INSTALL_PATH}
fancy_message info "Installed binary"
else
fancy_message info "Downloading binary"
ARCHIVE_NAME="yablo_0_2_0.tar.gz"
sudo -u ${USERNAME} bash -c '\
curl -L -s -o yablo_0_2_0.tar.gz "https://github.com/sebastian-xyz/yablo/releases/download/v0.2.0/yablo_0_2_0.tar.gz"
'
if [ $? -eq 0 ]; then
fancy_message info "Successfully downloaded binary"
else
fancy_message error "Download failed"
exit 1
fi
SHA_SUM="faba2a70971507e3db76d726f65a4c62b68db9dfab7c56e410d041e3f3234140 yablo_0_2_0.tar.gz"
echo ${SHA_SUM} | sha256sum --quiet -c
if [ $? -eq 0 ]; then
fancy_message info "Binary verified"
else
fancy_message error "Binary checksum is wrong"
exit 1
fi
tar -xvzf yablo_0_2_0.tar.gz 1> /dev/null
if [ $? -eq 0 ]; then
fancy_message info "Extracted binary"
else
fancy_message error "Failed to extract binary"
exit 1
fi
if [ ${UPDATE} -eq 1 ]; then
fancy_message info "Stopping systemd service"
systemctl stop yablo.service
fi
cp yablo ${INSTALL_PATH}
fancy_message info "Installed binary"
fi
chmod +x yablo_remove
cp yablo_remove ${INSTALL_PATH_REMOVE}
LOG_FILE="/var/log/yablo.log"
if [ -f ${LOG_FILE} ]; then
fancy_message warn "Logfile exists already. Backing up the old one."
cp --archive ${LOG_FILE} '/var/log/yablo-COPY-$(date +"%Y%m%d%H%M%S")'
echo "" > ${LOG_FILE}
fancy_message info "Created new empty log file"
else
fancy_message info "Created new empty log file"
touch ${LOG_FILE}
fi
SYSTEMD_SERVICE="/etc/systemd/system/yablo.service"
cp yablo.service ${SYSTEMD_SERVICE}
fancy_message info "Created systemd service"
echo ""
read -p "Do you want to enable the systemd service? [y/N]" -n 1 -r
echo ""
if [[ ${REPLY} =~ ^[Yy]$ ]]; then
if [ ${UPDATE} -eq 1 ]; then
systemctl daemon-reload
else
fancy_message info "Activated systemd service"
systemctl enable yablo.service 1> /dev/null
fi
systemctl start yablo.service
fancy_message info "Started systemd service"
else
if [ ${UPDATE} -eq 1 ]; then
systemctl daemon-reload
fi
fi
fancy_message info "Successfully installed yablo"
fancy_message info "Cleaning up"
rm yablo_0_2_0.tar.gz yablo
else
fancy_message error "Aborting"
fi