Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tjluoma committed Jun 14, 2019
1 parent da8621d commit c780741
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions di-ibettercharge.sh
@@ -0,0 +1,142 @@
#!/bin/zsh -f
# Purpose: Download iBetterCharge from https://softorino.com/ibettercharge/
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2019-06-14

NAME="$0:t:r"

if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
else
PATH='/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'
fi

INSTALL_TO='/Applications/iBetterCharge.app'

INFO=($(curl -sfLS "https://updates.devmate.com/com.softorino.iBetterCharge.xml" \
| tr ' ' '\012' \
| egrep '<sparkle:releaseNotesLink>|url=|sparkle:version=' \
| head -3 \
| sed 's#.*<sparkle:releaseNotesLink>##g; s#</sparkle:releaseNotesLink>##g; s#url="##g ; s#"$##g ; s#sparkle:version="##g'))

RELEASE_NOTES_URL="$INFO[1]"
URL="$INFO[2]"
LATEST_VERSION="$INFO[3]"

if [[ -e "$INSTALL_TO" ]]
then

INSTALLED_VERSION=$(defaults read "${INSTALL_TO}/Contents/Info" CFBundleShortVersionString)

autoload is-at-least

is-at-least "$LATEST_VERSION" "$INSTALLED_VERSION"

VERSION_COMPARE="$?"

if [ "$VERSION_COMPARE" = "0" ]
then
echo "$NAME: Up-To-Date ($INSTALLED_VERSION)"
exit 0
fi

echo "$NAME: Outdated: $INSTALLED_VERSION vs $LATEST_VERSION"

FIRST_INSTALL='no'

else

FIRST_INSTALL='yes'
fi

FILENAME="$HOME/Downloads/${${INSTALL_TO:t:r}// /}-${LATEST_VERSION}.zip"

if (( $+commands[lynx] ))
then

curl -sfLS "$RELEASE_NOTES_URL" >| "$FILENAME:r.html"

( lynx -dump -nomargins -width='150' -assume_charset=UTF-8 -pseudo_inlines -nonumbers "$RELEASE_NOTES_URL" \
| uniq \
) | tee "$FILENAME:r.txt"

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

UNZIP_TO=$(mktemp -d "${TMPDIR-/tmp/}${NAME}-XXXXXXXX")

echo "$NAME: Unzipping '$FILENAME' to '$UNZIP_TO':"

ditto -xk --noqtn "$FILENAME" "$UNZIP_TO"

EXIT="$?"

if [[ "$EXIT" == "0" ]]
then
echo "$NAME: Unzip successful"
else
# failed
echo "$NAME failed (ditto -xkv '$FILENAME' '$UNZIP_TO')"

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"

echo "$NAME: Moving existing (old) '$INSTALL_TO' to '$HOME/.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

echo "$NAME: Moving new version of '$INSTALL_TO:t' (from '$UNZIP_TO') to '$INSTALL_TO'."

# Move the file out of the folder
mv -vn "$UNZIP_TO/$INSTALL_TO:t" "$INSTALL_TO"

EXIT="$?"

if [[ "$EXIT" = "0" ]]
then

echo "$NAME: Successfully installed '$UNZIP_TO/$INSTALL_TO:t' to '$INSTALL_TO'."

else
echo "$NAME: Failed to move '$UNZIP_TO/$INSTALL_TO:t' to '$INSTALL_TO'."

exit 1
fi

[[ "$LAUNCH" = "yes" ]] && open -a "$INSTALL_TO"

exit 0
#EOF

0 comments on commit c780741

Please sign in to comment.