#!/bin/sh

# invoke this script like: create_dmg image_name "Volume Name" srcPath destPath size
# it will produce image_name.dmg
# size must be provided like this, Eg: 05m = 5MB

# if a previous copy of the image exists, remove it
rm -f $4/$1.dmg

# create the image.
hdiutil create -size $5 -srcfolder $3  -fs HFS+ -volname $2 $4$1.dmg

# mount the image and store the device name into dev_handle
dev_handle=`hdid $4$1.dmg | grep Apple_HFS | perl -e '\$_=<>; /^\\/dev\\/(disk.)/; print \$1'`
#hdiutil attach $4$1.dmg

# Obtain device information
DEVS=$(hdiutil attach $4$1.dmg | cut -f 1)
DEV=$(echo $DEVS | cut -f 1 -d ' ')

# copy the software onto the disk
#ditto -rsrcFork $3 "/Volumes/$2/Product"
#mv $3 "/Volumes/$2/"
#mkdir "/Volumes/$2/Album"
#cp -r $3 "/Volumes/$2/"

# unmount the volume
hdiutil detach $dev_handle
#hdiutil detach $DEV

# compress the image
hdiutil convert $4$1.dmg -format UDZO -o $4/$1.udzo.dmg

# remove the uncompressed image
rm -f $4$1.dmg

# move the compressed image to take its place
mv $4$1.udzo.dmg $4$1.dmg
