Skip to content

Instantly share code, notes, and snippets.

@jubel-han
Created February 15, 2017 06:48
Show Gist options
  • Save jubel-han/d4789cf110c8a7a354d7bc9e38a0d717 to your computer and use it in GitHub Desktop.
Save jubel-han/d4789cf110c8a7a354d7bc9e38a0d717 to your computer and use it in GitHub Desktop.
Installation or update xquartz on OS X
#!/bin/bash -f
# Download and install (or update) Xquartz
APP_PATH='/Applications/Utilities/XQuartz.app'
UPDATES_URL='http://xquartz.macosforge.org/downloads/sparkle/release.xml'
DOWNLOAD_ACTUAL=`curl -sL "$UPDATES_URL" | tr '"' '\012' | egrep '^http.*\.dmg' | head -1`
REMOTE_VERSION=`curl -sL "$UPDATES_URL" | awk -F'"' '/sparkle:version=/{print $4}' | head -1`
LOCAL_VERSION=`defaults read "${APP_PATH}/Contents/Info.plist" "CFBundleVersion" 2>/dev/null`
FILENAME="$DOWNLOAD_ACTUAL"
IMG=`echo "$FILENAME" | awk -F'/' '{print $NF}'`
TEVAL=0
if [ "$REMOTE_VERSION" = "" ]
then
echo "REMOTE_VERSION is empty, cannot continue"
exit 0
fi
if [ "$DOWNLOAD_ACTUAL" = "" ]
then
echo "DOWNLOAD_ACTUAL is empty, cannot continue"
exit 0
fi
if [ "$LOCAL_VERSION" = "$REMOTE_VERSION" ]
then
echo "$APP_PATH is up-to-date ($LOCAL_VERSION ~ $REMOTE_VERSION)"
exit 0
else
# Update Needed
echo "$APP_PATH is outdated ($LOCAL_VERSION vs $REMOTE_VERSION)"
fi
cd "$HOME/Downloads" || cd "$HOME/Desktop" || cd "$HOME" || cd /tmp
if [ -f "$IMG" ]
then
LOCAL_MD5=`md5 $IMG | awk '{print $NF}'`
REMOTE_MD5=`curl -sL "${DOWNLOAD_ACTUAL}".md5sum | awk '{print $1}'`
if [ "$LOCAL_MD5" == "$REMOTE_MD5" ]
then
echo "$IMG already downloaded."
TEVAL=1
fi
fi
if [ ${TEVAL} -eq 0 ]
then
wget -O "$IMG" "$FILENAME"
if [ $? -ne 0 ]
then
echo "Download $IMG failure."
exit 1;
fi
fi
MNTPNT=$(echo -n "Y" | hdid -plist "$IMG" 2>/dev/null | fgrep -A 1 '<key>mount-point</key>' | tail -1 | sed 's#</string>.*##g ; s#.*<string>##g')
if [ "$MNTPNT" = "" ]
then
echo "MNTPNT is empty"
exit 1
fi
PKG=`find "$MNTPNT" -maxdepth 1 -iname \*.pkg`
if [ "$PKG" = "" ]
then
echo "PKG is empty"
exit 1
fi
echo "Installing $PKG (this may take awhile...)"
sudo installer -pkg "$PKG" -target / -lang en 2>&1
EXIT="$?"
if [ "$EXIT" = "0" ]
then
echo "Successfully installed XQuartz.app version $REMOTE_VERSION"
COUNT='0'
while [ -e "$MNTPNT" ]
do
((COUNT++))
if [ "$COUNT" -gt "10" ]
then
exit 0
fi
# unmount the DMG
diskutil unmount "$MNTPNT" || sleep 5
done
if [ ! -e "$MNTPNT" ]
then
echo "Unmounted $MNTPNT"
fi
exit 0
else
echo "FAILED to install XQuartz.app version $REMOTE_VERSION (exit = $EXIT)"
exit 1
fi
exit 0
@myselfhimself
Copy link

Thank you for posting this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment