#!/usr/bin/env zsh -f # Purpose: get the beta of Drafts for Mac # # From: Timothy J. Luoma # Mail: luomat at gmail dot com # Date: 2021-04-09 NAME="$0:t:r" if [[ -e "$HOME/.path" ]] then source "$HOME/.path" fi INSTALL_TO='/Applications/Drafts.app' URL='https://s3-us-west-2.amazonaws.com/downloads.agiletortoise.com/Drafts.app.zip' LATEST_VERSION=$(curl -sfLS "https://config.agiletortoise.com/drafts-mac/beta/build-status.json" \ | awk -F'"' '/latest-version/{print $4}') if [[ "$LATEST_VERSION" == "" ]] then echo "$NAME: '\$LATEST_VERSION' is empty." >>/dev/stderr exit 0 fi if [[ -e "$INSTALL_TO" ]] then INSTALLED_VERSION=$(defaults read "${INSTALL_TO}/Contents/Info" CFBundleVersion) 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 vs $LATEST_VERSION)" exit 0 fi echo "$NAME: Beta version $LATEST_VERSION available (Installed version: $INSTALLED_VERSION)." FIRST_INSTALL='no' if [[ -e "${INSTALL_TO}/Contents/_MASReceipt/receipt" ]] then echo "\n\tWARNING! The version of Drafts found at '$INSTALL_TO' is from the Mac App Store.\n '$0' can only install or update the _beta_ version of Drafts.\n If you wish to use the beta, you must first remove the Drafts.app from the Mac App Store.\n Please note that having _both_ versions installed is **HIGHLY DISCOURAGED** and could cause **data loss or corruption**. \n\n\tPlease don't do that." >>/dev/stderr exit 2 fi 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}// /}.zip" RELEASE_NOTES_TXT="$FILENAME:r.txt" if [[ -e "$RELEASE_NOTES_TXT" ]] then cat "$RELEASE_NOTES_TXT" else if (( $+commands[lynx] )) then RELEASE_NOTES_URL='https://docs.getdrafts.com/beta/changelog-mac' RELEASE_NOTES=$(curl -sfLS "${RELEASE_NOTES_URL}" \ | tidy --tidy-mark no --char-encoding utf8 --wrap 0 --show-errors 0 --indent yes \ --input-xml no --output-xml no --quote-nbsp no --show-warnings no \ --uppercase-attributes no --uppercase-tags no --clean yes --force-output yes \ --join-classes yes --join-styles yes --markup yes --output-xhtml yes --quiet yes \ --quote-ampersand yes --quote-marks yes \ | awk '/

/dev/null EXIT="$?" if [ "$EXIT" = "1" -o ! -e "$FILENAME:r.txt" ] then (cd "$FILENAME:h" ; \ echo "\n\nLocal sha256:" ; \ shasum -a 256 "$FILENAME:t" \ ) >>| "$FILENAME:r.txt" fi TEMPDIR=$(mktemp -d "${TMPDIR-/tmp/}${NAME-$0:r}-XXXXXXXX") ## make sure that the .zip is valid before we proceed (command unzip -l "$FILENAME" 2>&1 )>/dev/null EXIT="$?" if [ "$EXIT" = "0" ] then echo "$NAME: '$FILENAME' is a valid zip file." else echo "$NAME: '$FILENAME' is an invalid zip file (\$EXIT = $EXIT)" mv -fv "$FILENAME" "$TEMPDIR/" mv -fv "$FILENAME:r".* "$TEMPDIR/" exit 0 fi ## unzip to a temporary directory UNZIP_TO=$(mktemp -d "${TEMPDIR}/${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 '$TEMPDIR/'." mv -f "$INSTALL_TO" "$TEMPDIR/$INSTALL_TO:t:r.$INSTALLED_VERSION.app" EXIT="$?" if [[ "$EXIT" != "0" ]] then echo "$NAME: failed to move existing '$INSTALL_TO' to '$TEMPDIR'." 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 -n "$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