-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
221 lines (201 loc) · 6.56 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
#!/usr/bin/env bash
# Destination directory for the script and systemd files
otun_dir="/opt/otun"
script_name="otun.sh"
config_json="telegram_config.json"
config_yaml="telegram_config.yaml"
service_file="otun.service"
timer_file="otun.timer"
systemd_dir="/etc/systemd/system"
# Print messages with different styles
print_message() {
local message=$1
local style=$2
case $style in
"info")
echo -e "\e[32m${message}\e[0m" # green text
;;
"warning")
echo -e "\e[33m${message}\e[0m" # yellow text
;;
"error")
echo -e "\e[31m${message}\e[0m" # red text
;;
esac
}
# Check if script is run with sudo
check_sudo() {
# check if user has sudo privileges by checking the output of sudo -l
# output = privileges, no output = no privileges
sudo_permissions=$(sudo -n -v 2>&1)
if [ -n "$sudo_permissions" ]; then
print_message "This script requires sudo privileges. Please enter your password to continue." "info"
sudo -v || {
print_message "Authentication failed. Exiting." "error"
exit 1
}
fi
}
# Create directory if it's not yet created
create_directory() {
local dir=$1
sudo mkdir -p "$dir"
}
# OTUN's file existence check
file_existence_check() {
if [ -e "$otun_dir/$script_name" ] && [ -e "$otun_dir/$config_json" ] && [ -e "$otun_dir/$config_yaml" ]; then
echo ""
while true; do
read -r -p "Files in $otun_dir already exist. Do you want to overwrite them? (y/N): " overwrite_files
case $overwrite_files in
[yY])
copy_files
break
;;
[nN] | "")
echo ""
print_message "Exiting without overwriting files." "info"
exit 1
;;
*)
print_message "Invalid input. Please enter 'y/Y' to overwrite or 'n/N/ENTER' to exit." "warning"
echo ""
;;
esac
done
else
copy_files
fi
}
# Copy files to directory
copy_files() {
sudo cp "$script_name" "$config_json" "$config_yaml" "$otun_dir/"
sudo chmod +x "$otun_dir/$script_name"
}
# Prompt for bot_token and chat_id
prompt_bot_info() {
echo ""
while true; do
read -r -p "Enter your Telegram bot token: " bot_token
# validate Telegram bot token format
if [[ $bot_token =~ ^[0-9]+:[a-zA-Z0-9_-]+$ ]]; then
break
else
print_message "Invalid bot token format." "warning"
echo ""
fi
done
while true; do
read -r -p "Enter your Telegram chat ID: " chat_id
# validate Telegram bot token format
if [[ $chat_id =~ ^(-?[0-9]+)$ ]]; then
break
else
print_message "Invalid chat ID format." "warning"
echo ""
fi
done
}
# Replace placeholders in configuration files
set_telegram_config() {
sudo sed -i "s/BOT_TOKEN_PLACEHOLDER/$bot_token/" "$otun_dir/$config_json"
sudo sed -i "s/CHAT_ID_PLACEHOLDER/$chat_id/" "$otun_dir/$config_json"
sudo sed -i "s/BOT_TOKEN_PLACEHOLDER/$bot_token/" "$otun_dir/$config_yaml"
sudo sed -i "s/CHAT_ID_PLACEHOLDER/$chat_id/" "$otun_dir/$config_yaml"
}
# systemd unit files existence check
systemd_existence_check() {
if [ -e "$systemd_dir/$timer_file" ] && [ -e "$systemd_dir/$service_file" ]; then
echo ""
while true; do
read -r -p "OTUN's systemd service and timer unit files already exist. Do you want to overwrite them? (y/N): " overwrite_systemd
case $overwrite_systemd in
[yY])
break
;;
[nN] | "")
echo ""
print_message "Exiting without overwriting OTUN's systemd service and timer unit files." "info"
exit 1
;;
*)
print_message "Invalid input. Please enter 'y/Y' to overwrite or 'n/N/ENTER' to exit." "warning"
echo ""
;;
esac
done
fi
}
# Inform the user to manually modify the OnCalendar field if desired
inform_user() {
echo ""
read -r -d '' message <<EOF
This install script allows you to specify the time (HH:mm format) you want to
run OTUN once a day. If you wish to schedule OTUN differently, please manually
modify the OnCalendar field in the $systemd_dir/$timer_file file based on your
desired schedule. See the following link for allowed formats:
https://www.freedesktop.org/software/systemd/man/latest/systemd.time.html#Calendar%20Events
After modifying $systemd_dir/$timer_file, do not forget to reload the systemd
manager configuration:
sudo systemctl daemon-reload
EOF
print_message "$message" "info"
}
# Prompt for and validate desired time
prompt_time() {
echo ""
while true; do
read -r -p "Enter the desired time for the script to run in HH:mm format: " run_time
# Validate the input format (HH:mm)
if [[ $run_time =~ ^([0-1][0-9]|2[0-3]):[0-5][0-9]$ ]]; then
break
else
print_message "Invalid time format. Please use HH:mm format (24-hour clock)." "warning"
echo ""
fi
done
}
# Copy systemd timer and service files
copy_systemd_files() {
sudo cp "$service_file" "$systemd_dir/"
sudo cp "$timer_file" "$systemd_dir/"
}
# Replace placeholders in the timer file
replace_timer() {
sudo sed -i "s/HOUR_MINUTE/$run_time/" "$systemd_dir/$timer_file"
}
# Perform systemctl commands
manage_systemd() {
sudo systemctl daemon-reload
sudo systemctl enable "$timer_file"
sudo systemctl start "$timer_file"
}
# Main script starts here
main() {
# check sudo privileges
check_sudo
# create /opt/otun directory if not yet created
create_directory "$otun_dir"
# check if files (all of them) in /opt/otun already exist
file_existence_check
# prompt for bot_token and chat_id
prompt_bot_info
# replace placeholders in configuration files
set_telegram_config
# check if systemd service and timer files (both) already exist
systemd_existence_check
# inform the user to manually modify the OnCalendar field if desired
inform_user
# prompt for and validate desired time
prompt_time
# copy systemd timer and service files
copy_systemd_files
# replace placeholders in the timer file
replace_timer
# perform systemctl commands
manage_systemd
# successfull installation print
print_message "\nOTUN's installation completed successfully." "info"
}
# Run the script
main "$@"