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