#!/usr/bin/env zsh -f # Purpose: Download and install the latest version of Evernote # # From: Tj Luo.ma # Mail: luomat at gmail dot com # Web: http://RhymesWithDiploma.com # Date: 2015-05-01 NAME="$0:t:r" INSTALL_TO='/Applications/Evernote.app' HOMEPAGE="https://evernote.com" DOWNLOAD_PAGE="https://evernote.com/download" SUMMARY="Evernote helps you capture and prioritize ideas, projects, and to-do lists, so nothing falls through the cracks." XML_FEED='https://update.evernote.com/public/ENMacSMD/EvernoteMacUpdate.xml' #RELEASE_NOTES_URL=$(curl -sfL "$XML_FEED" \ # | fgrep '' \ # | head -1 \ # | sed 's#.*##g ; s###g') RELEASE_NOTES_URL="https://update.evernote.com/public/ENMacSMD/releaseNotes-en.html" if [[ -e "$HOME/.path" ]] then source "$HOME/.path" fi INFO=($(curl -sfL "${XML_FEED}" \ | tr -s ' ' '\012' \ | egrep 'sparkle:version|sparkle:shortVersionString|url=' \ | head -3 \ | sort \ | awk -F'"' '/^/{print $2}')) # "Sparkle" will always come before "url" because of "sort" LATEST_VERSION="$INFO[1]" LATEST_BUILD="$INFO[2]" URL="$INFO[3]" # If any of these are blank, we should not continue if [ "$INFO" = "" -o "$LATEST_BUILD" = "" -o "$URL" = "" -o "$LATEST_VERSION" = "" ] then echo "$NAME: Error: bad data received: INFO: $INFO LATEST_VERSION: $LATEST_VERSION LATEST_BUILD: $LATEST_BUILD URL: $URL " 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 [[ -e "$INSTALL_TO/Contents/_MASReceipt/receipt" ]] then echo "$NAME: $INSTALL_TO was installed from the Mac App Store and cannot be updated by this script." echo " See or" echo " " echo " Please use the App Store app to update it: " exit 0 fi else FIRST_INSTALL='yes' fi FILENAME="$HOME/Downloads/$INSTALL_TO:t:r-${LATEST_VERSION}_${LATEST_BUILD}.zip" if (( $+commands[lynx] )) then ( curl -sfL "$RELEASE_NOTES_URL" \ | sed '1,//d; //,$d' \ | lynx -dump -nomargins -width='10000' -assume_charset=UTF-8 -pseudo_inlines -stdin ; echo "\nSource: <$RELEASE_NOTES_URL>" ) \ | 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