#!/bin/sh

set -u
set -e

SRCDIR=@CMAKE_SOURCE_DIR@

FLAVOR=AppleJava
LAUNCHER=/System/Library/Frameworks/JavaVM.framework/Versions/Current/Resources/MacOS/JavaApplicationStub
if [ $# -gt 0 ]; then
	if [ "$1" = "oracle" ]; then
		FLAVOR=OracleJava
		LAUNCHER=$SRCDIR/release/JavaAppLauncher/JavaAppLauncher
	fi
fi

umask 022
APP="@CMAKE_PROJECT_NAME@ Viewer-$FLAVOR.app"

if [ -d "$APP" ]; then
	rm -rf "$APP"
fi

mkdir -p "$APP/Contents/MacOS"
mkdir -p "$APP/Contents/Resources/Java"
mkdir -p "$APP/Contents/Resources/TurboJPEG"

cp pkgscripts/Info-$FLAVOR.plist "$APP/Contents/Info.plist"
if [ "$FLAVOR" = "AppleJava" ]; then
  cp $LAUNCHER "$APP/Contents/MacOS/"
else
  cp $LAUNCHER "$APP/Contents/MacOS/@CMAKE_PROJECT_NAME@ Viewer"
fi
echo "APPL????" >"$APP/Contents/PkgInfo"
cp java/VncViewer.jar "$APP/Contents/Resources/Java/"
cp $SRCDIR/java/com/turbovnc/vncviewer/README.txt "$APP/Contents/Resources/Java/"
cp $SRCDIR/LICENSE.txt "$APP/Contents/Resources/Java/"
cp $SRCDIR/release/turbovnc.icns "$APP/Contents/Resources/"
cp $SRCDIR/release/vncviewer.icns "$APP/Contents/Resources/"
cp @TJPEG_JNILIBRARY@ "$APP/Contents/Resources/TurboJPEG/"
cp @TJPEG_JNILIBRARY@ "$APP/Contents/Resources/TurboJPEG/"

exit
