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

PKGNAME=@PKGNAME@
PROJECT=@CMAKE_PROJECT_NAME@
VERSION=@VERSION@
BUILD=@BUILD@
SRCDIR=@CMAKE_SOURCE_DIR@
MACOS_APP_CERT_NAME="@MACOS_APP_CERT_NAME@"
MACOS_INST_CERT_NAME="@MACOS_INST_CERT_NAME@"

PREFIX=@CMAKE_INSTALL_PREFIX@
BINDIR=@CMAKE_INSTALL_FULL_BINDIR@
DOCDIR=@CMAKE_INSTALL_FULL_DOCDIR@

if [ -f $PKGNAME-$VERSION.dmg ]; then
	rm -f $PKGNAME-$VERSION.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 [ "$MACOS_APP_CERT_NAME" != "" ]; then
	find $PKGROOT$BINDIR/ -type f ! -name vglconnect | while read file; do
		codesign -s "$MACOS_APP_CERT_NAME" --options=runtime \
			--entitlements $SRCDIR/release/entitlements.plist --timestamp $file
		codesign --verify --strict -v $file
	done
fi

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

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

mkdir -p "$PKGROOT/Applications/$PROJECT"
install -m 644 $SRCDIR/release/ReadMe-MacApp.txt "$PKGROOT/Applications/$PROJECT/Read Me.txt"
ln -fs /Library/Documentation/$PKGNAME/index.html "$PKGROOT/Applications/$PROJECT/$PROJECT User's Guide.html"
if [ "$PREFIX" = "@CMAKE_INSTALL_DEFAULT_PREFIX@" ]; then
	ln -fs /Library/Documentation/$PKGNAME $PKGROOT/$PREFIX/doc
fi
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" --options runtime --timestamp \
		"$TMPDIR/Uninstall $PROJECT.app"
	codesign --verify --deep --strict -v "$TMPDIR/Uninstall $PROJECT.app"
fi
cp -Rp "$TMPDIR/Uninstall $PROJECT.app" "$PKGROOT/Applications/$PROJECT/"

mkdir $TMPDIR/dmg
pkgbuild --root $PKGROOT --version $VERSION.$BUILD --identifier @PKGID@ \
	$TMPDIR/pkg/$PKGNAME.pkg
SUFFIX=
if [ "$MACOS_INST_CERT_NAME" != "" ]; then
	SUFFIX=-unsigned
fi
productbuild --distribution pkgscripts/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.dmg
if [ "$MACOS_APP_CERT_NAME" != "" ]; then
	codesign -s "$MACOS_APP_CERT_NAME" --timestamp $TMPDIR/$PKGNAME-$VERSION.dmg
	codesign -vv $TMPDIR/$PKGNAME-$VERSION.dmg
fi
cp $TMPDIR/$PKGNAME-$VERSION.dmg .

exit
