-
Notifications
You must be signed in to change notification settings - Fork 10
/
di-backuploupe.sh
executable file
·160 lines (98 loc) · 3.65 KB
/
di-backuploupe.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
#!/usr/bin/env zsh -f
# Purpose: get the latest https://www.soma-zone.com/BackupLoupe/
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2019-06-06
[[ -e "$HOME/.path" ]] && source "$HOME/.path"
[[ -e "$HOME/.config/di/defaults.sh" ]] && source "$HOME/.config/di/defaults.sh"
INSTALL_TO="${INSTALL_DIR_ALTERNATE-/Applications}/BackupLoupe.app"
NAME="$0:t:r"
# https://www.soma-zone.com/BackupLoupe/a/appcast_update.xml
XML_FEED='https://www.soma-zone.com/BackupLoupe/a/appcast-update-3.xml'
TEMPFILE="${TMPDIR-/tmp}/${NAME}.$$.$RANDOM.xml"
rm -f "$TEMPFILE"
curl -sfLS "$XML_FEED" > "$TEMPFILE" || exit 1
ITEM_COUNT=$(fgrep '<item>' "$TEMPFILE" |wc -l | tr -dc '[0-9]')
INFO=$(awk "/<item>/{i++}i==${ITEM_COUNT}" "$TEMPFILE")
VERSIONS=($(echo "$INFO" | tr -s ' |\t' '\012' | egrep 'sparkle:(version=|shortVersionString)' | sort | awk -F'"' '//{print $2}'))
LATEST_VERSION="$VERSIONS[1]"
LATEST_BUILD="$VERSIONS[2]"
URL=$(echo "$INFO" | awk -F'"' '/url=/{print $2}')
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'
else
FIRST_INSTALL='yes'
fi
FILENAME="$HOME/Downloads/${${INSTALL_TO:t:r}// /}-${LATEST_VERSION}_${LATEST_BUILD}.tbz2"
RELEASE_NOTES_TXT="$FILENAME:r.txt"
if [[ -e "$RELEASE_NOTES_TXT" ]]
then
cat "$RELEASE_NOTES_TXT"
else
if (( $+commands[lynx] ))
then
RELEASE_NOTES=$(echo "${INFO}" \
| sed '1,/<description>/d; /<\/description>/,$d' \
| lynx -dump -nomargins -width='10000' \
-display_charset=UTF-8 -assume_charset=UTF-8 \
-pseudo_inlines -stdin)
echo "${RELEASE_NOTES}\n\nSource: ${XML_FEED}\nVersion: ${LATEST_VERSION} / ${LATEST_BUILD}\nURL: ${URL}" | tee "$RELEASE_NOTES_TXT"
fi
fi
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
(cd "$FILENAME:h" ; echo "\n\nLocal sha256:" ; shasum -a 256 "$FILENAME:t" ) >>| "$FILENAME:r.txt"
UNZIP_TO=$(mktemp -d "${TMPDIR-/tmp/}${NAME}-XXXXXXXX")
echo "$NAME: Installing $FILENAME to $UNZIP_TO"
tar -x -C "$UNZIP_TO" -f "$FILENAME"
EXIT="$?"
if [ "$EXIT" != "0" ]
then
echo "$NAME: 'tar' failed (\$EXIT = $EXIT)"
exit 1
fi
if [[ -e "$INSTALL_TO" ]]
then
pgrep -xq "$INSTALL_TO:t:r" \
&& LAUNCH='yes' \
&& osascript -e "tell application \"$INSTALL_TO:t:r\" to quit"
# move installed version to trash
mv -vf "$INSTALL_TO" "$HOME/.Trash/$INSTALL_TO:t:r.$INSTALLED_VERSION.app"
EXIT="$?"
if [[ "$EXIT" != "0" ]]
then
echo "$NAME: failed to move existing $INSTALL_TO to $HOME/.Trash/"
exit 1
fi
fi
# move the app from the temp folder to the regular installation dir
mv -vf "$UNZIP_TO/$INSTALL_TO:t" "$INSTALL_TO"
EXIT="$?"
if [[ "$EXIT" != "0" ]]
then
echo "$NAME: 'mv' failed (\$EXIT = $EXIT)"
exit 1
fi
[[ "$LAUNCH" = "yes" ]] && open -a "$INSTALL_TO"
exit 0
#EOF