-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.sh
executable file
·187 lines (153 loc) · 4.97 KB
/
setup.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
#!/bin/bash
# abort if anything goes sideways
set -eu -o pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# are we running with elevated permissions?
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
get_help(){
cat << EOF
Usage: setup.sh [OPTIONS]
Easily install/update/remove Klipper-MoTD.
Written by Tomasz Paluszkiewicz (GitHub: tomaski)
OPTIONS
-i, --install
The MoTD files will be placed at their inended locations.
Additionaly, certain system settings will be changed.
-r, --remove
The MoTD files will be removed from the system,
and all changes reverted.
-u, --update
Check for updates. If newer version is found,
you'll be asked whether to update or not.
-h, --help
Display this help text and exit. No changes are made.
EOF
}
backup_sshd_config(){
if [ -f /etc/ssh/sshd_config ]; then
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.1
echo "> Created backup of /etc/ssh/sshd_config"
else
echo "> File /etc/ssh/sshd_config not found."
exit 1
fi
}
install_motd(){
sed -i '/PrintLastLog/cPrintLastLog no' /etc/ssh/sshd_config
echo "> Disabled standard LastLog info."
if [ -f /etc/motd ]; then
mv /etc/motd /etc/motd.1
echo "> Disabled default static MoTD file."
fi
if [ -f /etc/update-motd.d/10-uname ]; then
chmod -x /etc/update-motd.d/10-uname
echo "> Disabled default dynamic MoTD file."
fi
mkdir -p /etc/update-motd.d
cp -r $SCRIPT_DIR/files/* /etc/update-motd.d/
chmod +x /etc/update-motd.d/10-klipper-motd
echo "> Copied the klipper MoTD files."
if ! [ -x "$(command -v figlet)" ]; then
apt install figlet -y > /dev/null 2>&1
echo "> Installed necessary 'figlet' package."
fi
chmod +x $SCRIPT_DIR/motd-config
cp $SCRIPT_DIR/motd-config /usr/bin/motd-config
echo "> Installed the MoTD configurator."
}
uninstall_motd(){
sed -i 's/^PrintLastLog\(.*\)$/#PrintLastLog yes/' /etc/ssh/sshd_config
echo "> Enabled standard LastLog info."
if [ -f /etc/motd.1 ]; then
mv /etc/motd.1 /etc/motd
echo "> Enabled default static MoTD file."
fi
if [ -f /etc/update-motd.d/10-uname ]; then
chmod +x /etc/update-motd.d/10-uname
echo "> Enabled default dynamic MoTD file."
fi
if [ -f /etc/update-motd.d/10-klipper-motd ]; then
rm /etc/update-motd.d/10-klipper-motd
fi
if [ -d /etc/update-motd.d/logos ]; then
rm -rf /etc/update-motd.d/logos
fi
echo "> Removed the klipper MoTD files."
if [ -x "$(command -v figlet)" ]; then
apt remove figlet -y > /dev/null 2>&1
echo "> Removed the 'figlet' package."
fi
if [ -f /usr/bin/motd-config ]; then
rm /usr/bin/motd-config
fi
echo "> Deleted the MoTD configurator."
}
check_update_motd(){
git remote update
CURRENT_VERSION=`git describe --tags --abbrev=0 --match "*.*.*" main`
LAST_VERSION=`git describe --tags --abbrev=0 --match "*.*.*" origin/main`
if [ $CURRENT_VERSION != $LAST_VERSION ]; then
echo "New version $LAST_VERSION available. You are on $CURRENT_VERSION"
while true; do
read -r -n 1 -p "Do you wish to update [y/n]? " run_update
case $run_update in
[Yy] ) run_update_motd; break;;
[Nn] ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
echo "You are already on the latest version ($CURRENT_VERSION)."
fi
}
run_update_motd(){
if [ -f /etc/update-motd.d/10-klipper-motd ]; then
git pull --no-edit
cp -r $SCRIPT_DIR/files/* /etc/update-motd.d/
chmod +x /etc/update-motd.d/10-klipper-motd
echo -e "\nKlipper MoTD has been succesfully updated.\n\nRun 'sudo motd-config' to set it up."
else
echo "Klipper MoTD does not seem to be installed!"
fi
}
reload_sshd(){
systemctl reload sshd.service
echo "> Reloaded sshd.service"
}
if [ "$#" -eq 0 ]; then
get_help
exit 1
fi
while [ $# -gt 0 ]; do
case "$1" in
-i | --install)
backup_sshd_config
install_motd
reload_sshd
echo -e "\nKlipper MoTD has been succesfully installed.\n\nRun 'sudo motd-config' to set it up."
shift
;;
-r | --remove)
backup_sshd_config
uninstall_motd
reload_sshd
echo -e "\nKlipper MoTD has been succesfully removed."
shift
;;
-u | --update)
check_update_motd
shift
;;
-h | --help)
get_help
exit 0
;;
*)
echo -e "\nUnknown option '${1}'. Type 'sudo ./"$(basename "$0")" --help' for usage info.\n"
exit 1
;;
esac
done