-
Notifications
You must be signed in to change notification settings - Fork 11
/
install.sh
executable file
·296 lines (278 loc) · 8.63 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash
# install script for ClonePi
# last updated @ v1.7.4
#
# - gets latest available version from github
# - updates source repo if possible
# - checks dependencies: system, rsync and fsck.vfat
# - copies clonepi to /usr/local/sbin & sets owner/permisions
# - copies configuration files to /etc/clonepi/ & sets owner/perms
#
# ClonePi version format: "major.minor.revision"
#
# helper functions
#
doMsg()
{
# msg, type
case "$2" in
warn)
printf "WARNING: ${1}\n"
echo
;;
user-abort)
printf "User aborted: ${1}\n"
echo
exit 0
;;
error)
printf "ERROR: ${1}\n"
echo "Aborting!"
echo
exit 1
;;
info-abort)
printf "INFO: ${1}\n"
echo "Nothing to do!"
echo
exit 0
;;
esac
}
#
# config + setup
#
INSTALL_DIR="/usr/local/sbin"
CONF_DIR="/etc/clonepi"
BAK_DIR="/tmp/clonepi-conf-bak"
GITHUB_VERSION_URL="https://raw.githubusercontent.com/SpoddyCoder/clonepi/master/version.txt"
CUR_INSTALLER_VER=`head -10 install.sh | grep "last updated" | cut -f2 -d'@' | xargs | cut -f2 -d'v'`
echo
echo "Welcome to the ClonePi installer"
echo
if [ `id -u` != 0 ]; then
doMsg "The ClonePi installer needs to be run as root" "error"
fi
# get current state
# ...version for this installer
NEW_VERSION=`cat version.txt | xargs`
# ...version currently installed
CUR_VERSION=0
if [ -f ${INSTALL_DIR}/clonepi ]; then
CUR_VERSION=`${INSTALL_DIR}/clonepi -v | grep "ClonePi v" | sed 's/^ClonePi v//'`
fi
# ...version on github
echo "Checking for latest version number at GitHub..."
REMOTE_VERSION=$(wget -q -O - $GITHUB_VERSION_URL)
if [ $? = 0 ]; then
echo "...latest available version is ${REMOTE_VERSION}"
else
echo "...error trying to get latest version - assuming source repo/dir is upto date."
REMOTE_VERSION=$NEW_VERSION
fi
# ...current conf & excludes versions, if installed
CUR_CONF_VER=""
CUR_EXCLUDES_VER=""
if [ -d $CONF_DIR ]; then
if [ -f ${CONF_DIR}/clonepi.conf ]; then
CUR_CONF_VER=`head -5 ${CONF_DIR}/clonepi.conf | grep "last updated" | cut -f2 -d'@' | xargs | cut -f2 -d'v'`
fi
if [ -f ${CONF_DIR}/raspbian.excludes ]; then
CUR_EXCLUDES_VER=`head -5 ${CONF_DIR}/raspbian.excludes | grep "last updated" | cut -f2 -d'@' | xargs | cut -f2 -d'v'`
fi
fi
#
# update repo to latest, if possible
#
if [ -d .git ]; then
# running inside git repo
if [ "$REMOTE_VERSION" = "$NEW_VERSION" ]; then
echo "Source repo is at latest version."
echo
else
doMsg "Source repo is not upto date." "warn"
read -p "Perform a 'git pull origin master' now (yes|no)? " UI < /dev/tty
if [ ! "$UI" = "y" -a ! "$UI" = "yes" ]; then
echo "Continuing without updating the repo"
echo
else
echo "Updating repo..."
su - `logname` -c "cd `pwd` && git pull origin master"
if [ $? = 0 ]; then
echo "Repo updated sucessfully"
# check if installer has been updated
NEW_INSTALLER_VER=`head -5 install.sh | grep "last updated" | cut -f2 -d'@' | xargs | cut -f2 -d'v'`
if [ "$CUR_INSTALLER_VER" != "$NEW_INSTALLER_VER" ]; then
doMsg "the installer has been updated, please re-run" "error"
fi
else
doMsg "problem updating repo." "error"
fi
NEW_VERSION=`cat version.txt | xargs`
fi
fi
else
# not a git repo, assume download zip
if [ "$REMOTE_VERSION" != "$NEW_VERSION" ]; then
doMsg "Source install dir is not upto date.\nDownload the latest zip to get latest version." "error"
fi
fi
#
# check dependencies
#
# system check
IS_RASPBIAN=`lsb_release -d | grep -i Raspbian`
if [ -z "$IS_RASPBIAN" -o ! $? = 0 ]; then
doMsg "this doesn't look like a Rapsbian system." "warn"
echo "Your OS is reported as:"
lsb_release -d
echo
echo "ClonePi is designed to work on Raspberry Pi systems. Please proceed, if you know what you're doing."
echo
fi
# rsync check
if ! rsync --version > /dev/null; then
doMsg "ClonePi requires rsync. Run the following to install: sudo apt-get update && sudo apt-get install rsync" "error"
fi
# fsck.vfat check
if ! test -e /sbin/fsck.vfat; then
doMsg "ClonePi requires dosfstools. Run the following to install: sudo apt-get update && sudo apt-get install dosfstools" "error"
fi
#
# pre-install setup
#
NEW_CONF_VER=`head -5 conf/clonepi.conf | grep "last updated" | cut -f2 -d'@' | xargs | cut -f2 -d'v'`
NEW_EXCLUDES_VER=`head -5 conf/raspbian.excludes | grep "last updated" | cut -f2 -d'@' | xargs | cut -f2 -d'v'`
UPGRADE_CONF=false
UPGRADE_EXCLUDES=false
INSTALL_CLONEPI=true
INSTALL_CONF_DIR=true
INSTALL_CONF_FILE=true
INSTALL_EXCLUDES_FILE=true
if [ "$CUR_VERSION" = "$NEW_VERSION" ]; then
INSTALL_CLONEPI=false
fi
if [ -d ${CONF_DIR} ]; then
INSTALL_CONF_DIR=false
if [ -f ${CONF_DIR}/clonepi.conf ]; then
INSTALL_CONF_FILE=false
fi
if [ -f ${CONF_DIR}/raspbian.excludes ]; then
INSTALL_EXCLUDES_FILE=false
fi
fi
if ! $INSTALL_CONF_FILE; then
# check if existing version is latest
if [ "$CUR_CONF_VER" = "$NEW_CONF_VER" ]; then
UPGRADE_CONF=false
else
UPGRADE_CONF=true
fi
fi
if ! $INSTALL_EXCLUDES_FILE; then
# check if existing version is latest
if [ "$CUR_EXCLUDES_VER" = "$NEW_EXCLUDES_VER" ]; then
UPGRADE_EXCLUDES=false
else
UPGRADE_EXCLUDES=true
fi
fi
#
# summarise and get user confirmation
#
if [ "$INSTALL_CONF_DIR" = false -a "$INSTALL_CONF_FILE" = false -a "$INSTALL_EXCLUDES_FILE" = false -a "$INSTALL_CLONEPI" = false -a "$UPGRADE_CONF" = false -a "$UPGRADE_EXCLUDES" = false ]; then
doMsg "ClonePi v${CUR_VERSION} already installed with latest config files" "info-abort"
else
echo "This will..."
if $INSTALL_CLONEPI; then
if [ "$CUR_VERSION" = 0 ]; then
echo " - Install ClonePi $NEW_VERSION"
else
echo " - Update ClonePi from $CUR_VERSION to $NEW_VERSION"
fi
fi
if $INSTALL_CONF_DIR; then
echo " - Install missing config directory at ${CONF_DIR}"
fi
if $INSTALL_CONF_FILE; then
echo " - Install missing config file at ${CONF_DIR}/clonepi.conf"
fi
if $INSTALL_EXCLUDES_FILE; then
echo " - Install missing config file at ${CONF_DIR}/raspbian.excludes"
fi
if $UPGRADE_CONF; then
echo " - Upgrade the config file at ${CONF_DIR}/clonepi.conf"
fi
if $UPGRADE_EXCLUDES; then
echo " - Upgrade the excludes file at ${CONF_DIR}/raspbian.excludes"
fi
fi
echo
read -p "Continue with install (yes|no)? " UI < /dev/tty
if [ ! "$UI" = "y" -a ! "$UI" = "yes" ]; then
doMsg "installation not confirmed" "user-abort"
fi
#
# And finally install
#
if $INSTALL_CLONEPI; then
rm -f ${INSTALL_DIR}/clonepi && cp ./src/clonepi ${INSTALL_DIR}/clonepi && chown root:root ${INSTALL_DIR}/clonepi && chmod u+x ${INSTALL_DIR}/clonepi
if [ "$?" = 0 ]; then
echo "Installed ClonePi to ${INSTALL_DIR}/clonepi"
else
doMsg "could not install ClonePi to ${INSTALL_DIR}/clonepi" "error"
fi
fi
if $INSTALL_CONF_DIR; then
mkdir ${CONF_DIR}
if [ "$?" = 0 ]; then
echo "Created config directory at ${CONF_DIR}"
else
doMsg "could not create config directory at ${CONF_DIR}" "error"
fi
fi
if $INSTALL_CONF_FILE; then
cp ./conf/clonepi.conf ${CONF_DIR}/clonepi.conf
if [ "$?" = 0 ]; then
echo "Installed config file at ${CONF_DIR}/clonepi.conf"
else
doMsg "could not install config file at ${CONF_DIR}/clonepi.conf" "error"
fi
fi
if $INSTALL_EXCLUDES_FILE; then
cp ./conf/raspbian.excludes ${CONF_DIR}/raspbian.excludes
if [ "$?" = 0 ]; then
echo "Installed config file at ${CONF_DIR}/raspbian.excludes"
else
doMsg "could not install config file at ${CONF_DIR}/raspbian.excludes" "error"
fi
fi
if [ "$UPGRADE_CONF" = true -o "$UPGRADE_EXCLUDES" = true ]; then
rm -rf ${BAK_DIR} && mkdir ${BAK_DIR}
if $UPGRADE_CONF; then
mv ${CONF_DIR}/clonepi.conf ${BAK_DIR}
cp ./conf/clonepi.conf ${CONF_DIR}/clonepi.conf
if [ "$?" = 0 ]; then
echo "Replaced config file at ${CONF_DIR}/clonepi.conf with latest version"
doMsg "Your old config file has been moved to ${BAK_DIR} - if you have modified this, you may need to merge back in any of your own changes" "warn"
else
doMsg "could not install config file at ${CONF_DIR}/clonepi.conf" "error"
fi
fi
if $UPGRADE_EXCLUDES; then
mv ${CONF_DIR}/raspbian.excludes ${BAK_DIR}
cp ./conf/raspbian.excludes ${CONF_DIR}/raspbian.excludes
if [ "$?" = 0 ]; then
echo "Replaced excludes file at ${CONF_DIR}/raspbian.excludes with latest version"
doMsg "Your old excludes file has been moved to ${BAK_DIR} - if you have modified this, you may need to merge back in your own changes" "warn"
else
doMsg "could not install excludes file at ${CONF_DIR}/raspbian.excludes" "error"
fi
fi
fi
chown -R root:root ${CONF_DIR}
chmod -R 755 ${CONF_DIR}
echo
echo "Installation complete!"
echo
exit 0