Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save tjluoma/eed442d0e89e51497010 to your computer and use it in GitHub Desktop.

Select an option

Save tjluoma/eed442d0e89e51497010 to your computer and use it in GitHub Desktop.
Download and install 1Password 5 betas automatically
#!/bin/zsh -f
zmodload zsh/datetime
zmodload zsh/stat
NAME="$0:t:r"
function timestamp { strftime "%Y-%m-%d--%H.%M.%S" "$EPOCHSECONDS" }
LOG="$HOME/Library/Logs/$NAME.log"
INSTALL_TO="/Applications/1Password 5.app"
function msgs
{
MSG="$@"
echo "$NAME [`timestamp`]: $MSG" | tee -a "$LOG"
(( $+commands[growlnotify] )) && \
pgrep -x -q Growl && \
growlnotify --sticky --appIcon "1Password 5" --identifier "$NAME" --message "$MSG" --title "$NAME"
}
function msg
{
MSG="$@"
echo "$NAME: $MSG" | tee -a "$LOG"
(( $+commands[growlnotify] )) && \
pgrep -x -q Growl && \
growlnotify --appIcon "1Password 5" --identifier "$NAME" --message "$MSG" --title "$NAME"
}
# life is too short to try to workaround not having `lynx` installed just because it's not installed by default in OS X
if ((! $+commands[lynx] ))
then
# note: if lynx is a function or alias, it will come back not found
msgs "$NAME: lynx is required but not found in $PATH"
exit 0
fi
# fetch the URL to the latest zip file download
DL_URL=`lynx -listonly -dump -nomargins -nonumbers 'https://app-updates.agilebits.com/product_history/OPM4#beta' \
| egrep -i '^http.*\.zip' \
| head -1`
if [[ "$DL_URL" == "" ]]
then
msgs 'Fatal Error: DL_URL is empty'
exit 0
fi
# what version is on the website ?
CURRENT_VERSION=`echo "$DL_URL:t:r" | sed 's#1Password-##g'`
# What version is installed?
LOCAL_VERSION=`defaults read "$INSTALL_TO/Contents/Info" CFBundleShortVersionString 2>/dev/null`
# If they are the same, nothing else is necessary
if [[ "$LOCAL_VERSION" == "$CURRENT_VERSION" ]]
then
msgs "1Password is current $CURRENT_VERSION"
exit 0
fi
# Where should the zip be saved to, locally
FILENAME="$HOME/Downloads/$DL_URL:t"
# Check the size of the remote zip so we know if we got it all
REMOTE_SIZE=`curl --head -sfL $DL_URL \
| awk -F' ' '/Content-Length:/{print $NF}' \
| tr -dc '[0-9]'`
# tell the user what we are doing
msg "Saving $DL_URL to $FILENAME"
# download the file
curl \
--continue-at - \
--max-time 3600 \
--fail \
--location \
--referer ";auto" \
--progress-bar \
--output "$FILENAME" \
"$DL_URL"
# Get the size of the local zip
LOCAL_SIZE=`zstat -L +size "$FILENAME"`
# If the sizes are not the same, complain
if [[ "$REMOTE_SIZE" != "$LOCAL_SIZE" ]]
then
msgs "Size Mismatch $LOCAL_SIZE vs $REMOTE_SIZE"
exit 0
fi
if (( $+commands[quit] ))
then
# get at http://www.jon.stovell.info/personal/Software.html
quit '1Password 5'
else
# Gentlest way to kill 1Password if it is running
pgrep -x -q '1Password 5' \
&& osascript -e 'tell application "1Password 5" to quit'
fi
/bin/launchctl stop 2BUA8C4S2C.com.agilebits.onepassword4-helper \
|| msgs "Failed to stop launchctl"
[[ -e "$INSTALL_TO" ]] && \
mv -vn "$INSTALL_TO" "$HOME/.Trash/1Password 5.$LOCAL_VERSION.app"
# Unzip the new version right to the /Applications/ folder
ditto -xkv "$FILENAME" "$INSTALL_TO:h/"
/bin/launchctl start 2BUA8C4S2C.com.agilebits.onepassword4-helper \
|| msgs "Failed to start launchctl"
exit 0
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment