
OUTFN="${OUTDIRNAME}/${OUTFILENAME}"

VOL="${VOLNAME}"
VER="${PRODVERSION}"
FILES="${INDIR}"


VD="/Volumes/$VOL"
ls "$VD" > /dev/null 2>&1
if [ $? -eq 0 ]; then
    hdiutil eject "$VD" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
	echo failed ejecting volume $VD - it is busy.
	echo Close all files opened on that volume and stop all apps running from it.
	echo Aborting creation of DMG!
	exit 1
    fi
fi


cd "${OUTDIRNAME}"
DMG="tmp-$VOL.dmg"

# create temporary disk image and format, ejecting when done
SIZE=`du -sk "${FILES}" | awk '{print $1}'`
SIZE=$((${SIZE} / 1000 +10))

hdiutil create -megabytes ${SIZE} -attach -mode 777 -ov -type UDIF -fs HFS+ -volname "$VOL" "$DMG" > /dev/null 2>&1

( cd "${FILES}"; cp -R .  "/Volumes/$VOL" )

hdiutil eject "/Volumes/$VOL" >/dev/null 2>&1
# convert to compressed image, delete temp image
rm -f "${OUTFN}"
hdiutil convert "$DMG" -format UDZO -imagekey zlib-level=9 -o "${OUTFN}" > /dev/null 2>&1
rm -f "$DMG"
