#! /bin/bash
## $Id: make-dmg.sh 224 2011-08-13 20:39:45Z stuart $
##
## Author: Stuart Connolly (stuconnolly.com)
## Copyright (c) 2011 Stuart Connolly. All rights reserved.
##
## Parameters: -s -- The source directory from which you want to create the disk image from.
## -t -- The directory you want the newly created disk image to be placed in.
## -n -- The name of the new disk image.
## -a -- The path to an Applescript script to run in order to customize the disk image's appearance (optional).
## -d -- The path to a custom .DS_Store file to be included within the newly created disk image (optional).
##
## Description: Creates a disk image (dmg) using the supplied source, destination and name.
usage()
{
echo "Usage: `basename $0` -s source_directory -t target_directory -n dmg_volume_name [-a applescript_path -d dsstore_path]"
exit 1
}
if [ $# -lt 3 ]
then
usage
fi
while getopts ':s:t:n:a:d:' OPTION
do
case $OPTION in
s) SOURCE="$OPTARG";;
t) TARGET="$OPTARG";;
n) DMG_VOLUME_NAME="$OPTARG";;
a) APPLESCRIPT=1;
APPLESCRIPT_PATH="$OPTARG";;
d) CUSTOM_DSSTORE=1;
DSSTORE_PATH="$OPTARG";;
*) usage;;
esac
done
DMG_TEMP_NAME="${DMG_VOLUME_NAME}-temp"
DMG_TEMP_PATH="${TARGET}/${DMG_TEMP_NAME}"
DMG_PATH="${TARGET}/${DMG_VOLUME_NAME}"
# Remove any existing disk images
if [ -e "${DMG_PATH}.dmg" ]
then
rm -f "${DMG_PATH}.dmg"
fi
# Create disk image
echo 'Creating disk image...'
hdiutil create -srcfolder "$SOURCE" -volname "$DMG_VOLUME_NAME" -fs HFS+ -fsargs '-c c=64,a=16,e=16' -format UDRW "$DMG_TEMP_PATH" > /dev/null
MOUNT_PATH="/Volumes/${DMG_VOLUME_NAME}" > /dev/null
# Mount image
echo 'Mounting disk image...'
hdiutil attach "${DMG_TEMP_PATH}.dmg" -readwrite -noverify -noautoopen > /dev/null
# Ensure there is no previous .DS_Store
if [ -e "${MOUNT_PATH}/.DS_Store" ]
then
rm -f "${MOUNT_PATH}/.DS_Store"
fi
# Copy custom .DS_Store if required
if [ $CUSTOM_DSSTORE ]
then
if [ -e "$DSSTORE_PATH" ]
then
cp "$DSSTORE_PATH" "/${MOUNT_PATH}/.DS_Store"
fi
fi
if [ $APPLESCRIPT ]
then
if [ -e "$APPLESCRIPT_PATH" ]
then
echo "Running AppleScript: $APPLESCRIPT_PATH $DMG_VOLUME_NAME"
osascript -l AppleScript "$APPLESCRIPT_PATH" "$DMG_VOLUME_NAME" > /dev/null
fi
fi
# Fix the disk's permissions
echo 'Fixing disk image permissions...'
chmod -Rf go-w "${DMG_TEMP_PATH}.dmg"
# Unmount image
echo 'Unmounting disk image...'
hdiutil detach "$MOUNT_PATH" > /dev/null
# Compress image
echo 'Compressing disk image...'
hdiutil convert "${DMG_TEMP_PATH}.dmg" -format UDBZ -o "$DMG_PATH" > /dev/null
rm -f "${DMG_TEMP_PATH}.dmg"
echo "Disk image creation complete. Successfully created '${DMG_VOLUME_NAME}.dmg' at '`dirname ${DMG_PATH}`'."
exit 0