#!/bin/sh

set -u
set -e
trap onexit INT
trap onexit TERM
trap onexit EXIT

TMPDIR=

onexit()
{
	if [ ! "$TMPDIR" = "" ]; then
		rm -rf $TMPDIR
	fi
}

safedirmove ()
{
	if [ "$1" = "$2" ]; then
		return 0
	fi
	if [ "$1" = "" -o ! -d "$1" ]; then
		echo safedirmove: source dir $1 is not valid
		return 1
	fi
	if [ "$2" = "" -o -e "$2" ]; then
		echo safedirmove: dest dir $2 is not valid
		return 1
	fi
	if [ "$3" = "" -o -e "$3" ]; then
		echo safedirmove: tmp dir $3 is not valid
		return 1
	fi
	mkdir -p $3
	mv $1/* $3/
	rmdir $1
	mkdir -p $2
	mv $3/* $2/
	rmdir $3
	return 0
}

FLAVOR=AppleJava
LAUNCHER=JavaApplicationStub
if [ $# -gt 0 ]; then
	if [ "$1" = "oracle" ]; then
		FLAVOR=OracleJava
		LAUNCHER="@CMAKE_PROJECT_NAME@ Viewer"
	fi
fi

PACKAGE_NAME=@CMAKE_PROJECT_NAME@
VERSION=@VERSION@
BUILD=@BUILD@
SRCDIR=@CMAKE_SOURCE_DIR@
PREFIX=@CMAKE_INSTALL_PREFIX@
BINDIR=@TVNC_BINDIR@
DOCDIR=@TVNC_DOCDIR@
JAVADIR=@TVNC_JAVADIR@

if [ -f $PACKAGE_NAME-$VERSION.dmg ]; then
	rm -f $PACKAGE_NAME-$VERSION.dmg
fi

umask 022
TMPDIR=`mktemp -d /tmp/$PACKAGE_NAME-build.XXXXXX`
PKGROOT=$TMPDIR/pkg/Package_Root
mkdir -p $PKGROOT

make install DESTDIR=$PKGROOT
if [ -d $PKGROOT$JAVADIR ]; then
	mv -f $PKGROOT$JAVADIR/README.txt $PKGROOT$PREFIX/
	rm $PKGROOT$JAVADIR/*
	rmdir $PKGROOT$JAVADIR
fi

mkdir -p $PKGROOT/Library/Documentation
safedirmove $PKGROOT$DOCDIR $PKGROOT/Library/Documentation/$PACKAGE_NAME $TMPDIR/__tmpdoc

mkdir -p $TMPDIR/pkg/
cp $SRCDIR/release/License.rtf $SRCDIR/release/Welcome.txt $SRCDIR/release/ReadMe.txt $TMPDIR/pkg/
cp pkgscripts/Distribution-$FLAVOR.xml $TMPDIR/pkg/Distribution.xml
mkdir -p $PKGROOT$BINDIR/
install -m 755 pkgscripts/uninstall $PKGROOT$BINDIR/

mkdir -p "$PKGROOT/Applications/$PACKAGE_NAME"
if [ "$FLAVOR" = "OracleJava" ]; then
	sh pkgscripts/makemacapp oracle
else
	sh pkgscripts/makemacapp
fi
ln -fs /Library/Documentation/$PACKAGE_NAME/index.html "$PKGROOT/Applications/$PACKAGE_NAME/$PACKAGE_NAME User's Guide.html"
install -m 644 @CMAKE_BINARY_DIR@/pkgscripts/uninstall.applescript $TMPDIR
osacompile -t APPL -a i386 -o "$TMPDIR/Uninstall $PACKAGE_NAME.app" $TMPDIR/uninstall.applescript
echo "do shell script \"@CMAKE_INSTALL_PREFIX@/bin/vncviewer -listen >/dev/null 2>&1 &\"" >$TMPDIR/listenmode.applescript
osacompile -t APPL -a i386 -o "$TMPDIR/$PACKAGE_NAME Viewer (Listen Mode).app" $TMPDIR/listenmode.applescript
cp -RX "$PACKAGE_NAME Viewer-$FLAVOR.app" "$PKGROOT/Applications/$PACKAGE_NAME/$PACKAGE_NAME Viewer.app"
cp -RX "$TMPDIR/$PACKAGE_NAME Viewer (Listen Mode).app" "$PKGROOT/Applications/$PACKAGE_NAME/"
cp -RX "$TMPDIR/Uninstall $PACKAGE_NAME.app" "$PKGROOT/Applications/$PACKAGE_NAME/"
echo "\"/Applications/$PACKAGE_NAME/$PACKAGE_NAME Viewer.app/Contents/MacOS/$LAUNCHER\" \${1+\"\$@\"}" >$PKGROOT$BINDIR/vncviewer
chmod 755 $PKGROOT$BINDIR/vncviewer

mkdir $TMPDIR/dmg
pkgbuild --root $PKGROOT --component-plist $SRCDIR/release/Package.plist \
	--version $VERSION.$BUILD --identifier com.virtualgl.turbovnc \
	$TMPDIR/pkg/$PACKAGE_NAME.pkg
productbuild --distribution $TMPDIR/pkg/Distribution.xml --package-path \
	$TMPDIR/pkg/ --resources $TMPDIR/pkg/ $TMPDIR/dmg/$PACKAGE_NAME.pkg
mv "$TMPDIR/Uninstall $PACKAGE_NAME.app" $TMPDIR/dmg/
hdiutil create -fs HFS+ -volname $PACKAGE_NAME-$VERSION \
	-srcfolder "$TMPDIR/dmg" $TMPDIR/$PACKAGE_NAME-$VERSION-$FLAVOR.dmg
cp $TMPDIR/$PACKAGE_NAME-$VERSION-$FLAVOR.dmg .

exit
