1#!/bin/bash
2
3BUILDNO="${BUILDNO:="0"}"
4
5echo
6echo "*******************************************************************************"
7if [ ! -d "./image/Xpra.app" ]; then
8	echo "./image/Xpra.app is missing - cannot continue"
9	exit 1
10fi
11rm -fr ./appstore/ 2>&1 > /dev/null
12mkdir appstore
13cp -R ./image/Xpra.app ./appstore/
14
15#get the version and build info from the python build records:
16export PYTHONPATH="appstore/Xpra.app/Contents/Resources/lib/python/"
17VERSION=`python -c "from xpra import __version__;import sys;sys.stdout.write(__version__)"`
18REVISION=`python -c "from xpra import src_info;import sys;sys.stdout.write(str(src_info.REVISION))"`
19REV_EXTRA=""
20if [ "$BUILDNO" != "0" ]; then
21	REV_EXTRA=".$BUILDNO"
22fi
23REV_MOD=`python -c "from xpra import src_info;import sys;sys.stdout.write(['','M'][src_info.LOCAL_MODIFICATIONS>0])"`
24BUILD_CPU=`python -c "from xpra import build_info;import sys;sys.stdout.write(str(build_info.BUILD_CPU))"`
25BUILD_INFO=""
26if [ "$BUILD_CPU" != "i386" ]; then
27	BUILD_INFO="-x86_64"
28fi
29echo "VERSION=$VERSION"
30echo "REVISION=$REVISION"
31echo "BUILDNO=$BUILDNO"
32echo "REV_MOD=$REV_MOD"
33echo "BUILD_CPU=$BUILD_CPU"
34echo "BUILD_INFO=$BUILD_INFO"
35PKG_FILENAME="./image/Xpra$BUILD_INFO-$VERSION-r$REVISION$REV_MOD$REV_EXTRA-appstore.pkg"
36rm -f $PKG_FILENAME >& /dev/null
37echo "Making $PKG_FILENAME"
38
39#for creating shell or C command wrappers that live in "Helpers":
40function helper_wrapper() {
41	filename=`basename $1`
42	wrapper=$2
43	cp ./Info-template.plist ./appstore/temp.plist
44	#sed -i '' -e "s+%BUNDLEID%+org.xpra.$filename+g" ./appstore/temp.plist
45	sed -i '' -e "s+%BUNDLEID%+org.xpra.Xpra+g" ./appstore/temp.plist
46	sed -i '' -e "s+%EXECUTABLE%+$filename+g" ./appstore/temp.plist
47	sed -i '' -e "s+%VERSION%+$VERSION+g" ./appstore/temp.plist
48	sed -i '' -e "s+%REVISION%+$REVISION$REV_MOD+g" ./appstore/temp.plist
49	sed -i '' -e "s+%BUILDNO%+$BUILDNO+g" ./appstore/temp.plist
50	gcc -arch i386 -o "appstore/Xpra.app/Contents/Helpers/$filename" "./${wrapper}-wrapper.c" -sectcreate __TEXT __info_plist ./appstore/temp.plist
51	rm appstore/temp.plist
52}
53
54#move all the scripts to Resources/scripts:
55mkdir appstore/Xpra.app/Contents/Resources/scripts
56for x in `ls appstore/Xpra.app/Contents/Helpers/`; do
57	mv "appstore/Xpra.app/Contents/Helpers/$x" "appstore/Xpra.app/Contents/Resources/scripts/"
58	helper_wrapper "$x" "Shell"
59done
60for x in `ls appstore/Xpra.app/Contents/Resources/bin/gst*`; do
61	helper_wrapper "$x" "C"
62done
63helper_wrapper "sshpass" "C"
64#the binaries in "/Contents/Resources/bin" look for "@executable_path/../Resources/lib/*dylib"
65#make it work with a symlink (ugly hack):
66ln -sf . appstore/Xpra.app/Contents/Resources/Resources
67
68#keep only one binary in MacOS:
69rm -f appstore/Xpra.app/Contents/MacOS/*
70gcc -arch i386 -o "appstore/Xpra.app/Contents/MacOS/Launcher" "./Shell-wrapper.c"
71
72echo "MacOS:"
73ls -la@ appstore/Xpra.app/Contents/MacOS
74echo
75echo "Helpers:"
76ls -la@ appstore/Xpra.app/Contents/Helpers
77
78#sound sub-app has a different binary: "Xpra":
79rm -fr ./appstore/Xpra.app/Contents/Xpra_NoDock.app/Contents/MacOS
80mkdir ./appstore/Xpra.app/Contents/Xpra_NoDock.app/Contents/MacOS
81gcc -arch i386 -o "appstore/Xpra.app/Contents/Xpra_NoDock.app/Contents/MacOS/Xpra" "./Shell-wrapper.c" -sectcreate __TEXT __info_plist appstore/Xpra.app/Contents/Xpra_NoDock.app/Contents/Info.plist
82
83CODESIGN_ARGS="--force --verbose --sign \"3rd Party Mac Developer Application\" --entitlements ./Xpra.entitlements"
84eval codesign $CODESIGN_ARGS appstore/Xpra.app/Contents/Helpers/*
85EXES=`find appstore/Xpra.app/Contents/MacOS -type f | xargs`
86eval codesign $CODESIGN_ARGS $EXES
87
88CODESIGN_ARGS="--force --verbose --sign \"3rd Party Mac Developer Application\" --entitlements ./Inherit.entitlements"
89LIBS=`find appstore/Xpra.app/Contents/Resources -name "*so" -or -name "*dylib" | xargs`
90eval codesign $CODESIGN_ARGS $LIBS
91eval "find appstore/Xpra.app/Contents/Resources/bin/ -type f -exec codesign $CODESIGN_ARGS {} \;"
92
93CODESIGN_ARGS="--force --verbose --sign \"3rd Party Mac Developer Application\" --entitlements ./Xpra.entitlements"
94eval codesign $CODESIGN_ARGS appstore/Xpra.app/Contents/Xpra_NoDock.app/Contents/MacOS/Xpra
95eval codesign $CODESIGN_ARGS appstore/Xpra.app/Contents/Xpra_NoDock.app
96eval codesign $CODESIGN_ARGS appstore/Xpra.app
97productbuild --component ./appstore/Xpra.app /Applications $PKG_FILENAME --sign "3rd Party Mac Developer Installer: $CODESIGN_KEYNAME"
98
99#show resulting file and copy it to the desktop
100du -sm $PKG_FILENAME
101cp $PKG_FILENAME ~/Desktop/
102
103echo "Done PKG"
104echo "*******************************************************************************"
105echo
106