|
1 #!/bin/bash |
|
2 # This Source Code Form is subject to the terms of the Mozilla Public |
|
3 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
5 |
|
6 # Unpack a disk image to a specified target folder |
|
7 # |
|
8 # Usage: unpack-diskimage <image_file> |
|
9 # <mountpoint> |
|
10 # <target_path> |
|
11 |
|
12 DMG_PATH=$1 |
|
13 MOUNTPOINT=$2 |
|
14 TARGETPATH=$3 |
|
15 |
|
16 # How long to wait before giving up waiting for the mount to finish (seconds) |
|
17 TIMEOUT=90 |
|
18 |
|
19 # If mnt already exists, then the previous run may not have cleaned up |
|
20 # properly. We should try to umount and remove the mnt directory. |
|
21 if [ -d $MOUNTPOINT ]; then |
|
22 echo "mnt already exists, trying to clean up" |
|
23 hdiutil detach $MOUNTPOINT -force |
|
24 rm -rdfv $MOUNTPOINT |
|
25 fi |
|
26 |
|
27 # Install an on-exit handler that will unmount and remove the '$MOUNTPOINT' directory |
|
28 trap "{ if [ -d $MOUNTPOINT ]; then hdiutil detach $MOUNTPOINT -force; rm -rdfv $MOUNTPOINT; fi; }" EXIT |
|
29 |
|
30 mkdir -p $MOUNTPOINT |
|
31 |
|
32 hdiutil attach -verbose -noautoopen -mountpoint $MOUNTPOINT "$DMG_PATH" |
|
33 # Wait for files to show up |
|
34 # hdiutil uses a helper process, diskimages-helper, which isn't always done its |
|
35 # work by the time hdiutil exits. So we wait until something shows up in the |
|
36 # mnt directory. Due to the async nature of diskimages-helper, the best thing |
|
37 # we can do is to make sure the glob() rsync is making can find files. |
|
38 i=0 |
|
39 while [ "$(echo $MOUNTPOINT/*)" == "$MOUNTPOINT/*" ]; do |
|
40 if [ $i -gt $TIMEOUT ]; then |
|
41 echo "No files found, exiting" |
|
42 exit 1 |
|
43 fi |
|
44 sleep 1 |
|
45 i=$(expr $i + 1) |
|
46 done |
|
47 # Now we can copy everything out of the $MOUNTPOINT directory into the target directory |
|
48 rsync -av $MOUNTPOINT/* $MOUNTPOINT/.DS_Store $MOUNTPOINT/.background $MOUNTPOINT/.VolumeIcon.icns $TARGETPATH/. |
|
49 hdiutil detach $MOUNTPOINT |
|
50 rm -rdf $MOUNTPOINT |
|
51 # diskimage-helper prints messages to stdout asynchronously as well, sleep |
|
52 # for a bit to ensure they don't disturb following commands in a script that |
|
53 # might parse stdout messages |
|
54 sleep 5 |