-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sh
executable file
·161 lines (133 loc) · 4.48 KB
/
init.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
#!/bin/bash
#
# This is the init script of the rememgram bot written by
# therealpeterpython (github.com/therealpeterpython).
# You can find the bot and my other work at
# github.com/therealpeterpython/remembot.
# Feel free to submit issues, requests and feedback via github.
#
# This program is licensed under CC BY-SA 4.0 by therealpeterpython.
#
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
UNITNAME="remembot.service"
PYTHON="$( which python3 )"
# ================================== #
# ======= Defining functions ======= #
# ================================== #
welcome () {
clear
echo -e "$(<logo.txt)\n"
echo "Welcome to the INIT script of the remembot repository!"
echo "This script will guide you through the initialisation of the bot."
echo -e "You have to initialise the bot just once to activate all features.\n\n"
}
# ================================== #
use_requirements () {
echo -en "\033[s"
read -r -e -p "Do you want me to install the python3 requirements via pip? [Y/n] " inp
inp=$(echo "$inp" | tr '[:upper:]' '[:lower:]') # to lower case
while true
do
case "$inp" in
y|yes)
requirements=true
break
;;
n|no)
requirements=false
break
;;
*)
read -r -p "Please enter 'yes' or 'no' " inp
inp=$(echo "$inp" | tr '[:upper:]' '[:lower:]') # to lower case
;;
esac
done
#echo -en "\r\033[1A\033[K"
}
# ================================== #
install_requirements () {
echo "== python3 -m pip install -r requirements.txt"
python3 -m pip install -r requirements.txt
}
# ================================== #
use_systemd () {
read -r -e -p "Do you want me to setup the systemd service unit to auto-(re)start the bot? [Y/n] " inp
inp=$(echo "$inp" | tr '[:upper:]' '[:lower:]') # to lower case
while true
do
case "$inp" in
y|yes)
systemd=true
break
;;
n|no)
systemd=false
break
;;
*)
read -r -p "Please enter 'yes' or 'no' " inp
inp=$(echo "$inp" | tr '[:upper:]' '[:lower:]') # to lower case
;;
esac
done
#echo -en "\r\033[1A\033[K"
}
# ================================== #
copy_unit () {
# make sure the directory exists
echo "== mkdir -p $HOME/.config/systemd/user"
mkdir -p "$HOME"/.config/systemd/user
# copy the unit
echo "== cp $UNITNAME $HOME/.config/systemd/user"
cp "$UNITNAME" "$HOME"/.config/systemd/user
# replace the placeholder for the python path
PYTHONESC="${PYTHON//\//\\/}"
echo "== sed -i -e 's/{INSERT-ABS-PATH-TO-PYTHON3}/$PYTHONESC/g' $HOME/.config/systemd/user/$UNITNAME"
sed -i -e "s/{INSERT-ABS-PATH-TO-PYTHON3}/$PYTHONESC/g" "$HOME"/.config/systemd/user/"$UNITNAME"
# replace the placeholder for the rememgram path
DIRESC="${DIR//\//\\/}"
echo "== sed -i -e 's/{INSERT-ABS-PATH-TO-REMEMBOT}/$DIRESC\/remembot/g' $HOME/.config/systemd/user/$UNITNAME"
sed -i -e "s/{INSERT-ABS-PATH-TO-REMEMBOT}/$DIRESC\/remembot/g" "$HOME"/.config/systemd/user/"$UNITNAME"
}
# ================================== #
activate_unit () {
echo "== systemctl --user enable $UNITNAME"
systemctl --user enable "$UNITNAME"
}
# ================================== #
activate_lingering () {
echo "== loginctl enable-linger $USER"
loginctl enable-linger "$USER"
}
# ================================== #
start_unit () {
echo "== systemctl --user start remembot.service"
systemctl --user start remembot.service
}
# ================================== #
goodbye () {
echo "The initialisation of the systemd unit is done (if you didn't get any error messages)."
echo "Maybe you want to check the status of the unit with 'systemctl --user status $UNITNAME'"
echo "Have fun!"
}
# ================================== #
# =========== Execution ============ #
# ================================== #
welcome
use_requirements
use_systemd
echo -e "\n********************* Executed commands: *********************"
if [ "$requirements" == true ]
then
install_requirements
fi
if [ "$systemd" == true ]
then
copy_unit
activate_unit
activate_lingering
start_unit
fi
echo -e "****************************************************************\n\n"
goodbye