-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdi-bbedit.sh
executable file
·257 lines (167 loc) · 6.1 KB
/
di-bbedit.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
#!/usr/bin/env zsh -f
# Purpose: download and install the latest version of BBEdit
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2021-05-04
NAME="$0:t:r"
[[ -e "$HOME/.path" ]] && source "$HOME/.path"
[[ -e "$HOME/.config/di/defaults.sh" ]] && source "$HOME/.config/di/defaults.sh"
INSTALL_TO="${INSTALL_DIR_ALTERNATE-/Applications}/BBEdit.app"
# if this file exists (regardless of size or content, just exists)
# it will be assumed that the user wants to use beta versions of BBEdit
PREFERS_BETA="$HOME/.config/di/prefers/BBEdit-prefer-betas.txt"
if [[ -e "$PREFERS_BETA" ]]
then
# @todo - next time there is a beta, double check this is correct
# XML_FEED='https://versioncheck.barebones.com/BBEdit-414.xml'
XML_FEED='https://versioncheck.barebones.com/BBEdit-415.xml'
echo "$NAME: Don't know what to use for 'RELEASE_NOTES_URL'" >>/dev/stderr
RELEASE_NOTES_URL=''
else
XML_FEED='https://versioncheck.barebones.com/BBEdit.xml'
RELEASE_NOTES_URL='https://www.barebones.com/support/bbedit/current_notes.html'
fi
zmodload zsh/datetime
TIME=$(strftime "%Y-%m-%d--%H.%M.%S" "$EPOCHSECONDS")
function timestamp { strftime "%Y-%m-%d--%H.%M.%S" "$EPOCHSECONDS" }
PLIST="${TMPDIR-/tmp/}${NAME}.${TIME}.$$.$RANDOM.plist"
curl -sfLS "$XML_FEED" \
| sed -e '1,/<array>/d' -e '/<\/array>/,$d' \
| tr -d '\t|\r\n' \
| sed 's#<dict>#\
<dict>#g' \
| tail -1 > "${PLIST}"
LATEST_BUILD=$(defaults read "${PLIST}" SUFeedEntryVersion)
LATEST_VERSION=$(defaults read "${PLIST}" SUFeedEntryShortVersionString)
URL=$(defaults read "${PLIST}" SUFeedEntryDownloadURL)
MIN_VERSION=$(defaults read "${PLIST}" SUFeedEntryMinimumSystemVersion)
OS_VER=$(SYSTEM_VERSION_COMPAT=1 sw_vers -productVersion)
autoload is-at-least
is-at-least "$MIN_VERSION" "$OS_VER"
EXIT="$?"
if [[ "$EXIT" != "0" ]]
then
echo "$NAME: BBEdit version $LATEST_VERSION/$LATEST_BUILD requires '$MIN_VERSION' and you are running '$OS_VER'." >>/dev/stderr
exit 1
fi
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"
FIRST_INSTALL='no'
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
else
FIRST_INSTALL='yes'
fi
FILENAME="$HOME/Downloads/${${INSTALL_TO:t:r}// /}-${${LATEST_VERSION}// /}_${${LATEST_BUILD}// /}.dmg"
CHECKSUM_FILE="$FILENAME:r.sha256.txt"
############################################################################################################
if [[ "$RELEASE_NOTES_URL" != "" ]]
then
if (( $+commands[wget] ))
then
if (( $+commands[html2text.py] ))
then
RELEASE_NOTES_FILE="$FILENAME:r.md"
if [[ -e "$RELEASE_NOTES_FILE" ]]
then
cat "$RELEASE_NOTES_FILE"
else
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" \
| egrep -v '<p><a href=".*">back to top</a></p>' \
| fgrep -vi 'please make sure that you have updated to the latest available OS version' \
| html2text.py \
| egrep -v '^\* \* \*' \
| uniq \
| sed -e 's#^ *##g' \
| tee -a "$RELEASE_NOTES_FILE"
fi
else
RELEASE_NOTES_FILE="$FILENAME:r.html"
if [[ -e "$RELEASE_NOTES_FILE" ]]
then
echo "$NAME: '$RELEASE_NOTES_FILE' already exists"
else
echo "$NAME: saving Release Notes to '$RELEASE_NOTES_FILE'."
wget --quiet --convert-links --output-document="$RELEASE_NOTES_FILE" "$RELEASE_NOTES_URL"
fi
fi # if html2text.py
fi # if wget
fi # if RELEASE_NOTES_URL
############################################################################################################
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
egrep -q '^Local sha256:$' "$CHECKSUM_FILE" 2>/dev/null
EXIT="$?"
if [ "$EXIT" = "1" -o ! -e "$CHECKSUM_FILE" ]
then
(cd "$FILENAME:h" ; \
echo "\nLocal sha256:" ; \
shasum -a 256 "$FILENAME:t" \
) >>| "$CHECKSUM_FILE"
fi
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
if [[ -e "$INSTALL_TO" ]]
then
# Quit app, if running
pgrep -xq "$INSTALL_TO:t:r" \
&& LAUNCH='yes' \
&& osascript -e "tell application \"$INSTALL_TO:t:r\" to quit"
# move installed version to trash
echo "$NAME: moving old installed version to '$HOME/.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 '$HOME/.Trash'. ('mv' \$EXIT = $EXIT)"
exit 1
fi
fi
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
[[ "$LAUNCH" = "yes" ]] && open -a "$INSTALL_TO"
echo -n "$NAME: Unmounting $MNTPNT: " && diskutil eject "$MNTPNT"
exit 0
#EOF