dev.stuconnolly.com / svn / safaritabs

  1. #! /bin/bash
  2.  
  3. ## $Id: make-dmg.sh 224 2011-08-13 20:39:45Z stuart $
  4. ##
  5. ## Author:      Stuart Connolly (stuconnolly.com)
  6. ##              Copyright (c) 2011 Stuart Connolly. All rights reserved.      
  7. ##
  8. ## Parameters:  -s -- The source directory from which you want to create the disk image from.
  9. ##              -t -- The directory you want the newly created disk image to be placed in.
  10. ##              -n -- The name of the new disk image.
  11. ##              -a -- The path to an Applescript script to run in order to customize the disk image's appearance (optional).
  12. ##              -d -- The path to a custom .DS_Store file to be included within the newly created disk image (optional).
  13. ##
  14. ## Description: Creates a disk image (dmg) using the supplied source, destination and name.
  15.  
  16. usage()
  17. {
  18.     echo "Usage: `basename $0` -s source_directory -t target_directory -n dmg_volume_name [-a applescript_path -d dsstore_path]"   
  19.     exit 1
  20. }
  21.  
  22. if [ $# -lt 3 ]
  23. then
  24.     usage
  25. fi
  26.  
  27. while getopts ':s:t:n:a:d:' OPTION
  28. do
  29.     case $OPTION in
  30.         s) SOURCE="$OPTARG";;
  31.         t) TARGET="$OPTARG";;
  32.         n) DMG_VOLUME_NAME="$OPTARG";;
  33.         a) APPLESCRIPT=1;
  34.            APPLESCRIPT_PATH="$OPTARG";;
  35.         d) CUSTOM_DSSTORE=1;
  36.            DSSTORE_PATH="$OPTARG";;
  37.         *) usage;;
  38.     esac
  39. done
  40.  
  41. DMG_TEMP_NAME="${DMG_VOLUME_NAME}-temp"
  42. DMG_TEMP_PATH="${TARGET}/${DMG_TEMP_NAME}"
  43. DMG_PATH="${TARGET}/${DMG_VOLUME_NAME}"
  44.  
  45. # Remove any existing disk images
  46. if [ -e "${DMG_PATH}.dmg" ]
  47. then
  48.     rm -f "${DMG_PATH}.dmg"
  49. fi
  50.  
  51. # Create disk image
  52. echo 'Creating disk image...'
  53. 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
  54.  
  55. MOUNT_PATH="/Volumes/${DMG_VOLUME_NAME}" > /dev/null
  56.  
  57. # Mount image
  58. echo 'Mounting disk image...'
  59. hdiutil attach "${DMG_TEMP_PATH}.dmg" -readwrite -noverify -noautoopen > /dev/null
  60.  
  61. # Ensure there is no previous .DS_Store
  62. if [ -e "${MOUNT_PATH}/.DS_Store" ]
  63. then
  64.     rm -f "${MOUNT_PATH}/.DS_Store"
  65. fi
  66.  
  67. # Copy custom .DS_Store if required
  68. if [ $CUSTOM_DSSTORE ]
  69. then
  70.     if [ -e "$DSSTORE_PATH" ]
  71.     then
  72.         cp "$DSSTORE_PATH" "/${MOUNT_PATH}/.DS_Store"
  73.     fi
  74. fi
  75.  
  76. if [ $APPLESCRIPT ]
  77. then
  78.     if [ -e "$APPLESCRIPT_PATH" ]
  79.     then
  80.         echo "Running AppleScript: $APPLESCRIPT_PATH $DMG_VOLUME_NAME"
  81.         osascript -l AppleScript "$APPLESCRIPT_PATH" "$DMG_VOLUME_NAME" > /dev/null
  82.     fi
  83. fi
  84.  
  85. # Fix the disk's permissions
  86. echo 'Fixing disk image permissions...'
  87. chmod -Rf go-w "${DMG_TEMP_PATH}.dmg"
  88.  
  89. # Unmount image
  90. echo 'Unmounting disk image...'
  91. hdiutil detach "$MOUNT_PATH" > /dev/null
  92.  
  93. # Compress image
  94. echo 'Compressing disk image...'
  95. hdiutil convert "${DMG_TEMP_PATH}.dmg" -format UDBZ -o "$DMG_PATH" > /dev/null
  96.  
  97. rm -f "${DMG_TEMP_PATH}.dmg"
  98.  
  99. echo "Disk image creation complete. Successfully created '${DMG_VOLUME_NAME}.dmg' at '`dirname ${DMG_PATH}`'."
  100.  
  101. exit 0
  102.