-
Notifications
You must be signed in to change notification settings - Fork 21
/
upgrade.sh
executable file
·323 lines (288 loc) · 6.64 KB
/
upgrade.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/sh
# Generic upgrade script by wkpark at gmail.com
# Since 2006/01/03
# LGPL/GPL dual license
#
# Currently support tar/7z/zip formats
#
CHECKSUM=
PACKAGE=moniwiki
if [ -z "$1" ]; then
cat <<HELP
Usage: $0 ${PACKAGE}.zip
HELP
exit 0
fi
SUCCESS="printf \033[1;32m"
FAILURE="printf \033[1;31m"
WARNING="printf \033[1;33m"
MESSAGE="printf \033[1;34m"
NORMAL="printf \033[0;39m"
MAGENTA="printf \033[1;35m"
NAME="MoniWiki"
$SUCCESS
echo
echo "+-------------------------------+"
echo "| $NAME upgrade script |"
echo "+-------------------------------+"
echo "| This script compare all files |"
echo "| between the current & the new |"
echo "| All different files are |"
echo "| backuped in the backup |"
echo "| directory and so you can |"
echo "| restore old one by manually. |"
echo "+-------------------------------+"
echo
$WARNING
echo -n " Press "
$MAGENTA
echo -n ENTER
$WARNING
echo -n " to continue or "
$MAGENTA
echo -n Control-C
$WARNING
echo -n " to exit "
$NORMAL
read dummy
for arg; do
case $# in
0)
break
;;
esac
option=$1
shift
case $option in
-show|-s)
show=1
;;
*)
if [ -z "$TAR" ]; then
TAR=$option
else
TAR1=$option
fi
esac
done
for T in $TAR $TAR1; do
#
TMP=.tmp$$
# get file extension
ext=${T#*.}
$MESSAGE
if [ "$ext" != "zip" ] && [ "$ext" != "tgz" ] && [ "$ext" != "tar.gz" ] && [ "$ext" != "7z" ]; then
# busybox does not support ${FOO#*.}
ext=$(echo $T | sed 's@^.*\.\(7z\|zip\|tgz\|tar.gz\)$@\1@')
if [ "$ext" != "zip" ] && [ "$ext" != "tgz" ] && [ "$ext" != "tar.gz" ] && [ "$ext" != "7z" ]; then
echo "*** FATAL: unrecognized extension ***"
exit
fi
fi
[ "$ext" = "zip" ] && echo "*** Extract Zip ***"
[ "$ext" = "tgz" -o "$ext" = "tar.gz" ] && echo "*** Extract tarball ***"
[ "$ext" = "7z" ] && echo "*** Extract 7z ***"
$NORMAL
if [ ! -d $TMP/$PACKAGE ]; then
mkdir -p $TMP/$PACKAGE
else
mv $TMP/$PACKAGE $TMP/$PACKAGE.orig
mkdir -p $TMP/$PACKAGE
fi
if [ $ext = "tgz" -o "$ext" = "tar.gz" ]; then
echo tar xzf $T --strip-components=1 -C$TMP/$PACKAGE
tar xzf $T --strip-components=1 -C$TMP/$PACKAGE
fi
if [ $ext = "zip" ]; then
[ $(which unzip) = "" ] && echo "*** FATAL: unzip command not found ***" && exit
echo unzip -d $TMP $T
unzip -d $TMP $T
fi
if [ $ext = "7z" ]; then
[ $(which 7zr) = "" ] && echo "*** FATAL: 7zr command not found ***" && exit
echo 7zr x $T -o$TMP
7zr x $T -o$TMP
fi
done
$MESSAGE
echo "*** Check new upgrade.sh script ***"
DIFF=
[ -f $TMP/$PACKAGE/upgrade.sh ] && DIFF=$(diff $0 $TMP/$PACKAGE/upgrade.sh)
if [ ! -z "$DIFF" ]; then
$FAILURE
echo "WARN: new upgrade.sh script found ***"
$NORMAL
cp -f $TMP/$PACKAGE/upgrade.sh up.sh
$WARNING
echo " new upgrade.sh file was copied as 'up.sh'"
echo " Please execute following command"
echo
$MAGENTA
echo " sh up.sh $TAR"
echo
$WARNING
echo -n "Ignore it and try to continue ? (y/N) "
read YES
if [ x$YES != xy ]; then
rm -r $TMP
$NORMAL
exit;
fi
fi
$MESSAGE
echo "*** Make the checksum list for the new version ***"
$NORMAL
FILELIST=$(find $TMP/$PACKAGE -type f | sort | sed "s@^$TMP/$PACKAGE/@@")
if [ -d $TMP/$PACKAGE.orig ]; then
SRC=$TMP/$PACKAGE.orig
else
SRC=.
fi
(cd $TMP/$PACKAGE; for x in $FILELIST; do test -f $x && md5sum $x;done) > checksum-new
if [ ! -f "$CHECKSUM" ];then
$MESSAGE
echo "*** Make the checksum for current version ***"
$NORMAL
(cd $SRC; for x in $FILELIST; do test -f $x && md5sum $x;done) > checksum-current
CHECKSUM=checksum-current
fi
UPGRADE=`diff -U0 checksum-current checksum-new |grep '^-'|cut -d' ' -f3`
NEW=`diff -U0 checksum-current checksum-new |grep '^\(-\|+\)' | cut -d' ' -f3|sort |uniq`
if [ -z "$UPGRADE" ] && [ -z "$NEW" ] ; then
rm -r $TMP
$FAILURE
echo "No difference found!! You have already installed the latest version"
$NORMAL
exit
fi
if [ $SRC != '.' ]; then
$MESSAGE
echo "*** Make $PACKAGE-changes.tgz file... ***"
CHANGES=$(diff -U0 checksum-current checksum-new |grep '^\(-\|+\)' | cut -d' ' -f3| sed "s@^@$PACKAGE/@" | sort |uniq)
if [ -z "$CHANGES" ]; then
$FAILURE
echo "No difference found!!"
$NORMAL
exit
fi
(cd $TMP;tar czf ../$PACKAGE-changes.tgz $CHANGES)
$SUCCESS
echo
echo "$PACKAGE-changes.tgz is made successfully"
echo
rm -r $TMP
$NORMAL
exit
fi
$MESSAGE
echo "*** Backup the old files ***"
$NORMAL
TYPES=B/t
$WARNING
echo -n " What type of backup do you want to ? ("
$MAGENTA
echo -n B
$WARNING
echo -n "ackup(default)/"
$MAGENTA
echo -n t
$WARNING
echo -n "ar/"
if [ $(which zip) != "" ]; then
$MAGENTA
echo -n z
$WARNING
echo -n "ip/"
TYPES=$TYPES/z
fi
if [ $(which 7zr) != "" ]; then
$MAGENTA
echo -n 7
$WARNING
echo -n "z/"
TYPES=$TYPES/7
fi
$MAGENTA
echo -n p
$WARNING
echo "atch) "
TYPES=$TYPES/p
$NORMAL
echo " (Type '$TYPES')"
read TYPE
DATE=`date +%Y%m%d-%s`
if [ x$TYPE != xt ] && [ x$TYPE != xp ] && [ x$TYPE != x7 ]; then
BACKUP=backup/$DATE
else
BACKUP=$TMP/$PACKAGE-$DATE
fi
$MESSAGE
if [ ! -z "$UPGRADE" ]; then
echo "*** Backup the old files ***"
$NORMAL
mkdir -p backup
mkdir -p $BACKUP
tar cf - $UPGRADE|(cd $BACKUP;tar xvf -)
if [ x$TYPE = xt ]; then
SAVED="backup/$DATE.tar.gz"
(cd $TMP; tar czvf ../backup/$DATE.tar.gz $PACKAGE-$DATE)
$MESSAGE
echo " Old files are backuped as a backup/$DATE.tar.gz"
$NORMAL
elif [ x$TYPE = xz ]; then
SAVED="backup/$DATE.zip"
(cd $TMP; zip -r ../backup/$DATE.zip $PACKAGE-$DATE)
$MESSAGE
echo " Old files are backuped as a backup/$DATE.zip"
$NORMAL
elif [ x$TYPE = x7 ]; then
SAVED="backup/$DATE.7z"
(cd $TMP; 7zr a ../backup/$DATE.7z $PACKAGE-$DATE)
$MESSAGE
echo " Old files are backuped as a backup/$DATE.7z"
$NORMAL
elif [ x$TYPE = xp ]; then
SAVED="backup/$PACKAGE-$DATE.diff"
(cd $TMP; diff -ruN $PACKAGE-$DATE $PACKAGE > ../backup/$PACKAGE-$DATE.diff )
$MESSAGE
echo " Old files are backuped as a backup/$PACKAGE-$DATE.diff"
$NORMAL
else
SAVED="$BACKUP/ dir"
$MESSAGE
echo " Old files are backuped to the $SAVED"
$NORMAL
fi
else
$WARNING
echo " You don't need to backup files !"
$NORMAL
fi
$WARNING
echo " Are you really want to upgrade $PACKAGE ?"
$NORMAL
echo -n " (Type '"
$MAGENTA
echo -n yes
$NORMAL
echo -n "' to upgrade or type others to exit) "
read YES
if [ x$YES != xyes ]; then
rm -r $TMP
echo -n "Please type '"
$MAGENTA
echo -n yes
$NORMAL
echo "' to real upgrade"
exit 1
fi
(cd $TMP/$PACKAGE;tar cf - $NEW|(cd ../..;tar xvf -))
rm -r $TMP
$SUCCESS
echo
echo "$PACKAGE is successfully upgraded."
echo
echo " All different files are "
echo " backuped in the "
echo " $SAVED now. :) "
$NORMAL