#!/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
}

VIEWER="@CMAKE_PROJECT_NAME@ Viewer"
LAUNCHER="$VIEWER"

PKGNAME=@PKGNAME@
PROJECT=@CMAKE_PROJECT_NAME@
VERSION=@VERSION@
BUILD=@BUILD@
CPU_TYPE=@CPU_TYPE@
SRCDIR=@CMAKE_SOURCE_DIR@
PREFIX=@CMAKE_INSTALL_PREFIX@
BINDIR=@CMAKE_INSTALL_FULL_BINDIR@
DOCDIR=@CMAKE_INSTALL_FULL_DOCDIR@
JAVADIR=@CMAKE_INSTALL_FULL_JAVADIR@
MANDIR=@CMAKE_INSTALL_FULL_MANDIR@
MACOS_APP_CERT_NAME="@MACOS_APP_CERT_NAME@"
MACOS_INST_CERT_NAME="@MACOS_INST_CERT_NAME@"

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

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

DESTDIR=$PKGROOT @CMAKE_MAKE_PROGRAM@ install
if [ -d $PKGROOT$BINDIR ]; then
	rm $PKGROOT$BINDIR/*
	rmdir $PKGROOT$BINDIR
fi
if [ -d $PKGROOT$JAVADIR ]; then
	if [ -d $PKGROOT$JAVADIR/jre ]; then
		rm -rf $PKGROOT$JAVADIR/jre/*
		rmdir $PKGROOT$JAVADIR/jre
	fi
	mv -f $PKGROOT$JAVADIR/README.md $PKGROOT$PREFIX/
	rm $PKGROOT$JAVADIR/*
	rmdir $PKGROOT$JAVADIR
fi

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

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

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

mkdir $TMPDIR/dmg
pkgbuild --root $PKGROOT --component-plist pkgscripts/Package.plist \
	--version $VERSION.$BUILD --identifier @PKGID@ \
	$TMPDIR/pkg/$PKGNAME.pkg
SUFFIX=
if [ "$MACOS_INST_CERT_NAME" != "" ]; then
	SUFFIX=-unsigned
fi
productbuild --distribution $TMPDIR/pkg/Distribution.xml --package-path \
	$TMPDIR/pkg/ --resources $TMPDIR/pkg/ $TMPDIR/dmg/$PKGNAME$SUFFIX.pkg
if [ "$MACOS_INST_CERT_NAME" != "" ]; then
	productsign --sign "$MACOS_INST_CERT_NAME" --timestamp \
		$TMPDIR/dmg/$PKGNAME$SUFFIX.pkg $TMPDIR/dmg/$PKGNAME.pkg
	rm -r $TMPDIR/dmg/$PKGNAME$SUFFIX.pkg
	pkgutil --check-signature $TMPDIR/dmg/$PKGNAME.pkg
fi
mv "$TMPDIR/Uninstall $PROJECT.app" $TMPDIR/dmg/
hdiutil create -fs HFS+ -volname $PKGNAME-$VERSION \
	-srcfolder "$TMPDIR/dmg" $TMPDIR/$PKGNAME-$VERSION-$CPU_TYPE.dmg
if [ "$MACOS_APP_CERT_NAME" != "" ]; then
	codesign -s "$MACOS_APP_CERT_NAME" --timestamp $TMPDIR/$PKGNAME-$VERSION-$CPU_TYPE.dmg
	codesign -vv $TMPDIR/$PKGNAME-$VERSION-$CPU_TYPE.dmg
fi
cp $TMPDIR/$PKGNAME-$VERSION-$CPU_TYPE.dmg .

exit
