| 1 |
#! /bin/bash |
| 2 |
|
| 3 |
# Create a read-only disk image of the contents of a folder |
| 4 |
# |
| 5 |
# Usage: make-diskimage <image_file> |
| 6 |
# <src_folder> |
| 7 |
# <volume_name> |
| 8 |
# <applescript> |
| 9 |
# <artpath> |
| 10 |
# <eula_resource_file> |
| 11 |
|
| 12 |
set -e; |
| 13 |
|
| 14 |
function pure_version() { |
| 15 |
echo '1.0.0.2' |
| 16 |
} |
| 17 |
|
| 18 |
function version() { |
| 19 |
echo "create-dmg $(pure_version)" |
| 20 |
} |
| 21 |
|
| 22 |
function usage() { |
| 23 |
version |
| 24 |
echo "Creates a fancy DMG file." |
| 25 |
echo "Usage: $(basename $0) options... image.dmg source_folder" |
| 26 |
echo "All contents of source_folder will be copied into the disk image." |
| 27 |
echo "Options:" |
| 28 |
echo " --volname name" |
| 29 |
echo " set volume name (displayed in the Finder sidebar and window title)" |
| 30 |
echo " --background pic.png" |
| 31 |
echo " set folder background image (provide png, gif, jpg)" |
| 32 |
echo " --window-pos x y" |
| 33 |
echo " set position the folder window" |
| 34 |
echo " --window-size width height" |
| 35 |
echo " set size of the folder window" |
| 36 |
echo " --icon-size icon_size" |
| 37 |
echo " set window icons size (up to 128)" |
| 38 |
echo " --icon file_name x y" |
| 39 |
echo " set position of the file's icon" |
| 40 |
echo " --custom-icon file_name custom_icon_or_sample_file x y" |
| 41 |
echo " set position and custom icon" |
| 42 |
echo " --version show tool version number" |
| 43 |
echo " --owner user[:group] fileroot" |
| 44 |
echo " set owner and user to the specified group, you may need sudo" |
| 45 |
echo " permissions and a password for this" |
| 46 |
echo " -h, --help display this help" |
| 47 |
exit 0 |
| 48 |
} |
| 49 |
|
| 50 |
WINX=10 |
| 51 |
WINY=60 |
| 52 |
WINW=600 |
| 53 |
WINH=400 |
| 54 |
ICON_SIZE=128 |
| 55 |
CHOWNFILES="" |
| 56 |
|
| 57 |
while test "${1:0:1}" = "-"; do |
| 58 |
case $1 in |
| 59 |
--volname) |
| 60 |
VOLUME_NAME="$2" |
| 61 |
shift; shift;; |
| 62 |
--background) |
| 63 |
BACKGROUND_FILE="$2" |
| 64 |
BACKGROUND_FILE_NAME="$(basename $BACKGROUND_FILE)" |
| 65 |
BACKGROUND_CLAUSE="set background picture of opts to file \".background:$BACKGROUND_FILE_NAME\"" |
| 66 |
shift; shift;; |
| 67 |
--icon-size) |
| 68 |
ICON_SIZE="$2" |
| 69 |
shift; shift;; |
| 70 |
--window-pos) |
| 71 |
WINX=$2; WINY=$3 |
| 72 |
shift; shift; shift;; |
| 73 |
--window-size) |
| 74 |
WINW=$2; WINH=$3 |
| 75 |
shift; shift; shift;; |
| 76 |
--icon) |
| 77 |
POSITION_CLAUSE="${POSITION_CLAUSE}set position of item \"$2\" to {$3, $4} |
| 78 |
" |
| 79 |
shift; shift; shift; shift;; |
| 80 |
--custom-icon) |
| 81 |
shift; shift; shift; shift; shift;; |
| 82 |
-h | --help) |
| 83 |
usage;; |
| 84 |
--version) |
| 85 |
version; exit 0;; |
| 86 |
--pure-version) |
| 87 |
pure_version; exit 0;; |
| 88 |
--owner) |
| 89 |
OWNER="${OWNER} |
| 90 |
$2 $3"; |
| 91 |
shift; shift; shift;; |
| 92 |
-*) |
| 93 |
echo "Unknown option $1. Run with --help for help." |
| 94 |
exit 1;; |
| 95 |
esac |
| 96 |
done |
| 97 |
|
| 98 |
test -z "$2" && { |
| 99 |
echo "Not enough arguments. Invoke with --help for help." |
| 100 |
exit 1 |
| 101 |
} |
| 102 |
|
| 103 |
DMG_PATH="$1" |
| 104 |
DMG_DIRNAME="$(dirname "$DMG_PATH")" |
| 105 |
DMG_DIR="$(cd $DMG_DIRNAME > /dev/null; pwd)" |
| 106 |
DMG_NAME="$(basename "$DMG_PATH")" |
| 107 |
DMG_TEMP_NAME="$DMG_DIR/rw.${DMG_NAME}" |
| 108 |
SRC_FOLDER="$(cd "$2" > /dev/null; pwd)" |
| 109 |
test -z "$VOLUME_NAME" && VOLUME_NAME="$(basename "$DMG_PATH" .dmg)" |
| 110 |
|
| 111 |
AUX_PATH="$(cd "$(dirname $0)"; pwd)/support" |
| 112 |
|
| 113 |
test -d "$AUX_PATH" || { |
| 114 |
echo "Cannot find support directory: $AUX_PATH" |
| 115 |
exit 1 |
| 116 |
} |
| 117 |
|
| 118 |
# Create the image |
| 119 |
echo "Creating disk image..." |
| 120 |
test -f "${DMG_TEMP_NAME}" && rm -f "${DMG_TEMP_NAME}" |
| 121 |
hdiutil create -srcfolder "$SRC_FOLDER" -volname "${VOLUME_NAME}" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -size 300m "${DMG_TEMP_NAME}" |
| 122 |
|
| 123 |
# mount it |
| 124 |
echo "Mounting disk image..." |
| 125 |
MOUNT_DIR="/Volumes/${VOLUME_NAME}" |
| 126 |
echo "Mount directory: $MOUNT_DIR" |
| 127 |
DEV_NAME=$(hdiutil attach -readwrite -noverify -noautoopen "${DMG_TEMP_NAME}" | egrep '^/dev/' | sed 1q | awk '{print $1}') |
| 128 |
echo "Device name: $DEV_NAME" |
| 129 |
|
| 130 |
#cp RightDS_Store "/Volumes/${VOLUME_NAME}/.DS_Store" |
| 131 |
|
| 132 |
if ! test -z "$BACKGROUND_FILE"; then |
| 133 |
echo "Copying background file..." |
| 134 |
test -d "$MOUNT_DIR/.background" || mkdir "$MOUNT_DIR/.background" |
| 135 |
cp "$BACKGROUND_FILE" "$MOUNT_DIR/.background/$BACKGROUND_FILE_NAME" |
| 136 |
fi |
| 137 |
|
| 138 |
# run applescript |
| 139 |
APPLESCRIPT=$(mktemp -t createdmg) |
| 140 |
cat "$AUX_PATH/template.applescript" | sed -e "s/WINX/$WINX/g" -e "s/WINY/$WINY/g" -e "s/WINW/$WINW/g" -e "s/WINH/$WINH/g" -e "s/BACKGROUND_CLAUSE/$BACKGROUND_CLAUSE/g" -e "s/ICON_SIZE/$ICON_SIZE/g" | perl -pe "s/POSITION_CLAUSE/$POSITION_CLAUSE/g" >"$APPLESCRIPT" |
| 141 |
|
| 142 |
echo "Running Applescript: ./AdiumApplescriptRunner \"${APPLESCRIPT}\" process_disk_image \"${VOLUME_NAME}\"" |
| 143 |
"$AUX_PATH/AdiumApplescriptRunner" "${APPLESCRIPT}" process_disk_image "${VOLUME_NAME}" || true |
| 144 |
echo "Done running the applescript..." |
| 145 |
sleep 4 |
| 146 |
|
| 147 |
# make sure it's not world writeable |
| 148 |
echo "Fixing permissions..." |
| 149 |
chmod -Rf go-w "${MOUNT_DIR}" || true |
| 150 |
|
| 151 |
#Do any chown as indicated by the --owner setting |
| 152 |
cd ${MOUNT_DIR} |
| 153 |
OLDIFS=${IFS} |
| 154 |
IFS=" |
| 155 |
" |
| 156 |
for perms in ${OWNER} |
| 157 |
do |
| 158 |
IFS=${OLDIFS} |
| 159 |
sudo chown -R ${perms} |
| 160 |
done |
| 161 |
IFS=${OLDIFS} |
| 162 |
cd ${OLDPWD} |
| 163 |
|
| 164 |
echo "Done fixing permissions." |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
# make the top window open itself on mount: |
| 169 |
if [ -x /usr/local/bin/openUp ]; then |
| 170 |
echo "Applying openUp..." |
| 171 |
/usr/local/bin/openUp "${MOUNT_DIR}" |
| 172 |
fi |
| 173 |
|
| 174 |
#Set volume icon if existent. |
| 175 |
/Developer/Tools/SetFile -a C "${MOUNT_DIR}" |
| 176 |
|
| 177 |
# unmount |
| 178 |
echo "Unmounting disk image..." |
| 179 |
hdiutil detach "${DEV_NAME}" |
| 180 |
|
| 181 |
# compress image |
| 182 |
echo "Compressing disk image..." |
| 183 |
hdiutil convert "${DMG_TEMP_NAME}" -format UDZO -imagekey zlib-level=9 -o "${DMG_DIR}/${DMG_NAME}" |
| 184 |
rm -f "${DMG_TEMP_NAME}" |
| 185 |
|
| 186 |
# adding EULA resources |
| 187 |
if [ ! -z "${EULA_RSRC}" -a "${EULA_RSRC}" != "-null-" ]; then |
| 188 |
echo "adding EULA resources" |
| 189 |
hdiutil unflatten "${DMG_DIR}/${DMG_NAME}" |
| 190 |
/Developer/Tools/ResMerger -a "${EULA_RSRC}" -o "${DMG_DIR}/${DMG_NAME}" |
| 191 |
hdiutil flatten "${DMG_DIR}/${DMG_NAME}" |
| 192 |
fi |
| 193 |
|
| 194 |
echo "Disk image done" |
| 195 |
exit 0 |