#!/bin/bash set -euo pipefail if [ "$#" -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; then echo "Usage: $0 " exit 0 fi if [[ $# -ne 2 ]]; then echo "Usage: $0 " >&2 exit 2 fi app="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")" output="$2" if [[ ! -f "$app/Contents/Info.plist" ]]; then echo "Missing macOS application: $app" >&2 exit 1 fi icon="$app/Contents/Resources/AppIcon.icns" if [[ ! -f "$icon" ]]; then echo "Missing application icon: $icon" >&2 exit 1 fi if [[ "$output" != *.dmg ]]; then echo "Output must have a .dmg extension: $output" >&2 exit 2 fi mkdir -p "$(dirname "$output")" output="$(cd "$(dirname "$output")" && pwd)/$(basename "$output")" stage="$(mktemp -d "${TMPDIR:-/tmp}/cutdex-dmg.XXXXXX")" source="$stage/source" mount="$stage/mounted" mounted=false cleanup() { if [[ "$mounted" == true ]]; then hdiutil detach -quiet "$mount" || true fi rm -rf "$stage" } trap cleanup EXIT mkdir "$source" "$mount" ditto "$app" "$source/$(basename "$app")" ln -s /Applications "$source/Applications" ditto "$icon" "$source/.VolumeIcon.icns" hdiutil create -quiet -format UDRW -volname Cutdex -srcfolder "$source" "$stage/uncompressed.dmg" hdiutil attach -quiet -readwrite -nobrowse -mountpoint "$mount" "$stage/uncompressed.dmg" mounted=true xcrun SetFile -a C "$mount" hdiutil detach -quiet "$mount" mounted=false hdiutil convert -quiet "$stage/uncompressed.dmg" -format UDZO -o "$output" -ov hdiutil verify -quiet "$output" swift "$(dirname "$0")/set_macos_file_icon.swift" "$icon" "$output" echo "DMG: $output" shasum -a 256 "$output"