1#!/bin/bash
2set -e -o pipefail
3trap 'echo "${BASH_SOURCE[0]}{${FUNCNAME[0]}}:${LINENO}: Error: command \`${BASH_COMMAND}\` failed with exit code $?"' ERR
4cd "$(dirname "$0")"/
5
6lensfun-update-data || true
7cp -R ~/.local/share/lensfun .
8~/.local/bin/gtk-mac-bundler darktable.bundle
9mv package/darktable.app/Contents/MacOS/darktable-bin package/darktable.app/Contents/MacOS/darktable
10mv package/darktable.app/Contents/Resources/lib/gdk-pixbuf-2.0/*/loaders.cache package/darktable.app/Contents/Resources/etc/gtk-3.0/
11rm -Rf lensfun
12sed -i '' 's|/opt/local/share/locale||' package/darktable.app/Contents/Resources/etc/gtk-3.0/gtk.immodules
13glib-compile-schemas package/darktable.app/Contents/Resources/share/glib-2.0/schemas/
14sed -i '' 's|{VERSION}|'$(git describe --tags --long --match '*[0-9.][0-9.][0-9]'|cut -d- -f2|sed 's/^\([0-9]*\.[0-9]*\)$/\1.0/')'|' package/darktable.app/Contents/Info.plist
15sed -i '' 's|{COMMITS}|'$(git describe --tags --long --match '*[0-9.][0-9.][0-9]'|cut -d- -f3)'|' package/darktable.app/Contents/Info.plist
16for i in package/darktable.app/Contents/MacOS/darktable{,-chart,-cli,-cltest,-generate-cache}
17do
18	install_name_tool -rpath @loader_path/../lib/darktable @loader_path/../Resources/lib/darktable "$i"
19done
20ln -s /Applications package/
21
22if [ -z "$STRIPDEBUGINFO" ]
23then
24	find package/darktable.app/Contents/Resources/lib/darktable \( -name \*.dylib -or -name \*.so \) -exec dsymutil \{} \;
25	for i in package/darktable.app/Contents/MacOS/darktable{,-chart,-cli,-cltest,-generate-cache,-curve-tool,-noiseprofile}
26	do
27		dsymutil "$i"
28	done
29fi
30
31if [ -n "$CODECERT" ]
32then
33	find package/darktable.app/Contents/Resources/lib{,exec} -type d -name \*.dSYM -prune -or -type f -exec codesign --verbose --force --options runtime -s "${CODECERT}" \{} \;
34	codesign --deep --verbose --force --options runtime -s "${CODECERT}" package/darktable.app
35fi
36
37PROGN=darktable
38
39# Creating temporary rw image
40hdiutil create -srcfolder package -volname "${PROGN}" -fs HFS+ \
41      -fsargs "-c c=64,a=16,e=16" -format UDRW pack.temp.dmg
42
43# mounting image without autoopen to create window style params
44device=$(hdiutil attach -readwrite -noverify -autoopen "pack.temp.dmg" | \
45         egrep '^/dev/' | sed 1q | awk '{print $1}')
46
47echo '
48 tell application "Finder"
49	tell disk "'${PROGN}'"
50		set current view of container window to icon view
51		set toolbar visible of container window to false
52		set statusbar visible of container window to false
53		set the bounds of container window to {400, 100, 885, 330}
54		set theViewOptions to the icon view options of container window
55		set arrangement of theViewOptions to not arranged
56		set icon size of theViewOptions to 72
57		set position of item "'${PROGN}'" of container window to {100, 100}
58		set position of item "Applications" of container window to {375, 100}
59		update without registering applications
60	end tell
61 end tell
62' | osascript
63
64# Finalizing creation
65chmod -Rf go-w /Volumes/"${PROGN}"
66sync
67hdiutil detach ${device}
68DMG="${PROGN}-$(git describe --tags|sed 's/^release-//;s/-/+/;s/-/~/;s/rc/~rc/')"
69hdiutil convert "pack.temp.dmg" -format UDZO -imagekey zlib-level=9 -o "${DMG}"
70rm -f pack.temp.dmg
71rm -Rf package
72if [ -n "${APPLEID}" ]
73then
74	UUID="$(xcrun altool --notarize-app --primary-bundle-id org.darktable -u "${APPLEID}" -p "${APPLEPW}" -f "${DMG}".dmg --output-format xml | grep -A 1 '<key>RequestUUID</key>' | tail -n 1 | cut -d'>' -f2 | cut -d'<' -f1)"
75	if [ -n "${UUID}" ]
76	then
77		STATUS="in progress"
78		while [ "${STATUS}" = "in progress" ]
79		do
80			echo "Waiting 1 minute for notarization to complete..."
81			sleep 60
82			STATUS="$(xcrun altool --notarization-info "${UUID}" -u "${APPLEID}" -p "${APPLEPW}" --output-format xml | grep -A 1 '<key>Status</key>' | tail -n 1 | cut -d'>' -f2 | cut -d'<' -f1)"
83		done
84		if [ "${STATUS}" = "success" ]
85		then
86			xcrun stapler staple "${DMG}".dmg
87		else
88			echo "Notarization unsuccessful: ${STATUS}!"
89		fi
90	else
91		echo "Failed to upload package for notarization!"
92	fi
93fi
94