-
Notifications
You must be signed in to change notification settings - Fork 10
/
di-bbedit-meta.sh
executable file
·370 lines (245 loc) · 10.6 KB
/
di-bbedit-meta.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env zsh -f
# Purpose: Download or install/update the latest version of BBEdit
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2019-10-15
## @todo - failed to clean download
# See note at EOF
NAME="$0:t:r"
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
fi
INSTALL_TO='/Applications/BBEdit.app'
# Not current in use, but useful reference info
SUMMARY='BBEdit is the leading professional HTML and text editor for macOS. It doesn’t suck.®'
HOMEPAGE="https://www.barebones.com/products/bbedit/"
DOWNLOAD_PAGE="https://www.barebones.com/support/bbedit/updates.html"
# This is the current version of macOS that this computer is running
OS_VER=$(SYSTEM_VERSION_COMPAT=1 sw_vers -productVersion)
# a zsh-specific function for comparing numbers, including version numbers
autoload is-at-least
################################################################################################################################################
while
do
## The first one of these to match will break the loop. We start with the newest version and work back.
# for Mojave (10.14.2) and newer, look for the most recent version of BBEdit 13
is-at-least "10.14.2" "$OS_VER" \
&& URL='check' \
&& break
# for Sierra (10.12.6) or later, use BBEdit 12.6.7
is-at-least '10.12.6' "$OS_VER" \
&& URL='https://s3.amazonaws.com/BBSW-download/BBEdit_12.6.7.dmg' \
&& SHA256_EXPECTED='d0647c864268b187343bd95bfcf490d6a2388579b1f8fce64a289c65341b1144' \
&& LATEST_VERSION='12.6.7' \
&& LATEST_BUILD='412120' \
&& break
# for El Capitan (10.11.6) or later, use BBEdit 12.1.6
is-at-least '10.11.6' "$OS_VER" \
&& URL='https://s3.amazonaws.com/BBSW-download/BBEdit_12.1.6.dmg' \
&& SHA256_EXPECTED='23b9fc6ef5c03cbcab041566503c556d5baf56b2ec18f551e6f0e9e6b48dc690' \
&& LATEST_VERSION='12.1.6' \
&& LATEST_BUILD='410110' \
&& break
# for Mavericks (10.9.5) or later, use BBEdit 11.6.8
is-at-least '10.9.5' "$OS_VER" \
&& URL='https://s3.amazonaws.com/BBSW-download/BBEdit_11.6.8.dmg' \
&& SHA256_EXPECTED='aa4a9f8ed12206dbf1d9e61c213be02789b87f963d8171743a3a057bfd1ede2a' \
&& LATEST_VERSION='11.6.8' \
&& LATEST_BUILD='397082' \
&& break
# for Mountain Lion (10.8.5) or later, use BBEdit 11.1.4
is-at-least '10.8.5' "$OS_VER" \
&& URL='https://s3.amazonaws.com/BBSW-download/BBEdit_11.1.4.dmg' \
&& SHA256_EXPECTED='9e14bcafaa2f1e9900a9826e2d51c194e530641b6fd5f55334444531736f68df' \
&& LATEST_VERSION='11.1.4' \
&& LATEST_BUILD='3780' \
&& break
# for Snow Leopard (10.6.8) or later, use BBEdit 10.5.13
is-at-least '10.6.8' "$OS_VER" \
&& URL='https://s3.amazonaws.com/BBSW-download/BBEdit_10.5.13.dmg' \
&& SHA256_EXPECTED='2de7baf01ba12650e158e86c65bea72103eca840ab2de45121e3460d09a58ebd' \
&& LATEST_VERSION='10.5.13' \
&& LATEST_BUILD='3396' \
&& break
# if you get here, you're probably Stephen Hackett or John Moltz. Sorry, I don't know
# which versions of BBEdit go with older versions of Mac OS X
echo "$NAME: Sorry, I do not know what to do for Mac OS X version $OS_VER."
exit 1
done
################################################################################################################################################
if [[ "$URL" == "check" ]]
then
## if we get here, we need to check for the latest version of BBEdit
## this is the feed wherein the latest information is made available from Barebones
XML_FEED='https://versioncheck.barebones.com/BBEdit.xml'
URL=$(curl -sfLS "$XML_FEED" \
| egrep -i '<string>https://.*bbedit.*\.dmg</string>' \
| tail -1 \
| sed -e 's#.*<string>##g' -e 's#</string>##g')
## Beta Feed?
## this is where the file will be downloaded and what it will be named
FILENAME="$HOME/Downloads/${${INSTALL_TO:t:r}// /}-${LATEST_VERSION}_${LATEST_BUILD}.dmg"
else
## if we get here, we are using a pre-defined version of BBEdit for our version of macOS/Mac OS X
# don't try to fetch release notes for old versions
RELEASE_NOTES_URL=''
FILENAME="$HOME/Downloads/$URL:t"
fi
################################################################################################################################################
## This is where we check the installed version -- if there is one -- against the most current information
if [[ -e "$INSTALL_TO" ]]
then
INSTALLED_VERSION=$(defaults read "${INSTALL_TO}/Contents/Info" CFBundleShortVersionString)
INSTALLED_BUILD=$(defaults read "${INSTALL_TO}/Contents/Info" CFBundleVersion)
autoload is-at-least
is-at-least "$LATEST_VERSION" "$INSTALLED_VERSION"
VERSION_COMPARE="$?"
is-at-least "$LATEST_BUILD" "$INSTALLED_BUILD"
BUILD_COMPARE="$?"
if [ "$VERSION_COMPARE" = "0" -a "$BUILD_COMPARE" = "0" ]
then
echo "$NAME: Up-To-Date ($INSTALLED_VERSION/$INSTALLED_BUILD)"
exit 0
fi
echo "$NAME: Outdated: $INSTALLED_VERSION/$INSTALLED_BUILD vs $LATEST_VERSION/$LATEST_BUILD"
if [[ ! -w "$INSTALL_TO" ]]
then
echo "$NAME: '$INSTALL_TO' exists, but you do not have 'write' access to it, therefore you cannot update it." >>/dev/stderr
exit 2
fi
fi
################################################################################################################################################
if [[ "$RELEASE_NOTES_URL" != "" ]]
then
## if we have a release notes URL, then we want to save the information therein to a file
RELEASE_NOTES_FILE="$FILENAME:r.txt"
if [[ -s "$RELEASE_NOTES_FILE" ]]
then
# if the file already exists, just show the contents
cat "$RELEASE_NOTES_FILE"
else
# Barebones' release notes are good enough that I'll go to extra lengths to get them
if (( $+commands[wget] ))
then
if (( $+commands[html2text.py] ))
then
TEMPFILE="${TMPDIR-/tmp}/${NAME}.${TIME}.$$.$RANDOM.html"
wget --quiet --convert-links --output-document="$TEMPFILE" "$RELEASE_NOTES_URL"
sed '1,/<p class="title">/d; /<p><em>fin<\/em><\/p>/,$d' "$TEMPFILE" | html2text.py | tee -a "$RELEASE_NOTES_FILE"
else
TEMPFILE="$FILENAME:r.html"
echo "$NAME: saving Release Notes to '$TEMPFILE'."
wget --quiet --convert-links --output-document="$TEMPFILE" "$RELEASE_NOTES_URL"
fi # if html2text.py
fi # if wget
fi # if release notes file
else
echo "$NAME: no Release Notes URL found"
fi # if release notes URL
################################################################################################################################################
## Here is where the download happens and is verified
echo "$NAME: Downloading '$URL' to '$FILENAME':"
curl --continue-at - --fail --location --output "$FILENAME" "$URL"
EXIT="$?"
## exit 22 means 'the file was already fully downloaded'
[ "$EXIT" != "0" -a "$EXIT" != "22" ] && echo "$NAME: Download of $URL failed (EXIT = $EXIT)" && exit 0
[[ ! -e "$FILENAME" ]] && echo "$NAME: $FILENAME does not exist." && exit 0
[[ ! -s "$FILENAME" ]] && echo "$NAME: $FILENAME is zero bytes." && rm -f "$FILENAME" && exit 0
################################################################################################################################################
## The XML_FEED includes a shasum for the latest version, so we should use that to check that we got what we expected
## For older versions of BBEdit, I downloaded them from Barebones and checked them myself, and then added them.
if [[ "$SHA256_EXPECTED" != "" ]]
then
SHA256_FILE="$FILENAME:r.sha256.txt"
# if we get here, we have something to compare against
# put the expected value (which we got from the XML_FEED
# into a text file with the full path of the filename of the file we just downloaded
# and then we can check it with 'shasum --check'
##
echo "${SHA256_EXPECTED} $FILENAME:t" >| "$SHA256_FILE"
echo -n "$NAME: Verifying sha256 checksum of '$FILENAME': "
cd "$FILENAME:h"
shasum --check "$SHA256_FILE"
SHASUM_EXIT="$?"
if [ "$SHASUM_EXIT" = "0" ]
then
echo "$NAME: '$FILENAME' matches the expected signature."
else
echo "$NAME: checksum failed (\$SHASUM_EXIT = $SHASUM_EXIT)"
echo "$NAME: Moving '$FILENAME' and related files to the Trash, as they may be unsafe."
[[ -e "$FILENAME" ]] && mv -f "$FILENAME" "$HOME/.Trash/"
[[ -e "$RELEASE_NOTES_FILE" ]] && mv -f "$RELEASE_NOTES_FILE" "$HOME/.Trash/"
[[ -e "$SHA256_FILE" ]] && mv -f "$SHA256_FILE" "$HOME/.Trash/"
exit 1
fi
fi
################################################################################################################################################
##
## Once the DMG is verified, we mount it
##
echo "$NAME: Mounting $FILENAME:"
MNTPNT=$(hdiutil attach -nobrowse -plist "$FILENAME" 2>/dev/null \
| fgrep -A 1 '<key>mount-point</key>' \
| tail -1 \
| sed 's#</string>.*##g ; s#.*<string>##g')
if [[ "$MNTPNT" == "" ]]
then
echo "$NAME: MNTPNT is empty"
exit 1
else
echo "$NAME: MNTPNT is $MNTPNT"
fi
################################################################################################################################################
##
## Once the DMG is mounted, we should move the existing version of BBEdit out of the way, if it exists.
##
if [[ -e "$INSTALL_TO" ]]
then
# move installed version to trash
mv -f "$INSTALL_TO" "$HOME/.Trash/$INSTALL_TO:t:r.${INSTALLED_VERSION}_${INSTALLED_BUILD}.app"
EXIT="$?"
if [[ "$EXIT" != "0" ]]
then
echo "$NAME: failed to move '$INSTALL_TO' to Trash. ('mv' \$EXIT = $EXIT)"
exit 1
fi
fi
################################################################################################################################################
##
## This is where we copy from the DMG to the expected destination
##
echo "$NAME: Installing '$MNTPNT/$INSTALL_TO:t' to '$INSTALL_TO': "
ditto --noqtn -v "$MNTPNT/$INSTALL_TO:t" "$INSTALL_TO"
EXIT="$?"
if [[ "$EXIT" == "0" ]]
then
echo "$NAME: Successfully installed $INSTALL_TO"
else
echo "$NAME: ditto failed"
exit 1
fi
################################################################################################################################################
##
## if we made it this far, the installation went OK, so let's unmount the DMG
##
echo -n "$NAME: Unmounting $MNTPNT: " && diskutil eject "$MNTPNT"
################################################################################################################################################
exit 0
#EOF
XML_FEED='https://versioncheck.barebones.com/BBEdit-415.xml'
PLIST="${TMPDIR-/tmp/}${NAME}.${TIME}.$$.$RANDOM.plist"
curl -sfLS "https://versioncheck.barebones.com/BBEdit-414.xml" \
| sed -e '1,/<array>/d' -e '/<\/array>/,$d' \
| tr -d '\t|\r\n' \
| sed 's#<dict>#\
<dict>#g' \
| tail -1 > "${PLIST}"
LC_ALL=C
RELEASE_NOTES_URL=$(sed -e 's#</data>.*##g' -e 's#.*<data>##g' "$PLIST" \
| base64 --decode \
| sed 's#"#\
#g' \
| egrep --text '^http')
echo ">$RELEASE_NOTES_URL<"