michael@0: #!/bin/bash michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: # Unpack a disk image to a specified target folder michael@0: # michael@0: # Usage: unpack-diskimage michael@0: # michael@0: # michael@0: michael@0: DMG_PATH=$1 michael@0: MOUNTPOINT=$2 michael@0: TARGETPATH=$3 michael@0: michael@0: # How long to wait before giving up waiting for the mount to finish (seconds) michael@0: TIMEOUT=90 michael@0: michael@0: # If mnt already exists, then the previous run may not have cleaned up michael@0: # properly. We should try to umount and remove the mnt directory. michael@0: if [ -d $MOUNTPOINT ]; then michael@0: echo "mnt already exists, trying to clean up" michael@0: hdiutil detach $MOUNTPOINT -force michael@0: rm -rdfv $MOUNTPOINT michael@0: fi michael@0: michael@0: # Install an on-exit handler that will unmount and remove the '$MOUNTPOINT' directory michael@0: trap "{ if [ -d $MOUNTPOINT ]; then hdiutil detach $MOUNTPOINT -force; rm -rdfv $MOUNTPOINT; fi; }" EXIT michael@0: michael@0: mkdir -p $MOUNTPOINT michael@0: michael@0: hdiutil attach -verbose -noautoopen -mountpoint $MOUNTPOINT "$DMG_PATH" michael@0: # Wait for files to show up michael@0: # hdiutil uses a helper process, diskimages-helper, which isn't always done its michael@0: # work by the time hdiutil exits. So we wait until something shows up in the michael@0: # mnt directory. Due to the async nature of diskimages-helper, the best thing michael@0: # we can do is to make sure the glob() rsync is making can find files. michael@0: i=0 michael@0: while [ "$(echo $MOUNTPOINT/*)" == "$MOUNTPOINT/*" ]; do michael@0: if [ $i -gt $TIMEOUT ]; then michael@0: echo "No files found, exiting" michael@0: exit 1 michael@0: fi michael@0: sleep 1 michael@0: i=$(expr $i + 1) michael@0: done michael@0: # Now we can copy everything out of the $MOUNTPOINT directory into the target directory michael@0: rsync -av $MOUNTPOINT/* $MOUNTPOINT/.DS_Store $MOUNTPOINT/.background $MOUNTPOINT/.VolumeIcon.icns $TARGETPATH/. michael@0: hdiutil detach $MOUNTPOINT michael@0: rm -rdf $MOUNTPOINT michael@0: # diskimage-helper prints messages to stdout asynchronously as well, sleep michael@0: # for a bit to ensure they don't disturb following commands in a script that michael@0: # might parse stdout messages michael@0: sleep 5