#!/bin/sh

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

TMPDIR=

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

usage()
{
	echo "$0 [-build32 [32-bit build dir]] [-buildarmv6 [ARM v6 build dir]] [-buildarmv7 [ARM v7 build dir]]"
	exit 1
}

PACKAGE_NAME=@PACKAGE_NAME@
VERSION=@VERSION@
BUILD=@BUILD@
SRCDIR=@abs_top_srcdir@
BUILDDIR32=@abs_top_srcdir@/osxx86
BUILD32=0
BUILDDIRARMV6=@abs_top_srcdir@/iosarmv6
BUILDARMV6=0
BUILDDIRARMV7=@abs_top_srcdir@/iosarmv7
BUILDARMV7=0
WITH_JAVA=@WITH_JAVA@
while [ $# -gt 0 ]; do
	case $1 in
	-h*)             usage 0                   ;;
	-build32)
		BUILD32=1
		if [ $# -gt 1 ]; then
			if [[ ! "$2" =~ -.* ]]; then
				BUILDDIR32=$2;  shift
			fi
		fi
		;;
	-buildarmv6)
		BUILDARMV6=1
		if [ $# -gt 1 ]; then
			if [[ ! "$2" =~ -.* ]]; then
				BUILDDIRARMV6=$2;  shift
			fi
		fi
		;;
	-buildarmv7)
		BUILDARMV7=1
		if [ $# -gt 1 ]; then
			if [[ ! "$2" =~ -.* ]]; then
				BUILDDIRARMV7=$2;  shift
			fi
		fi
		;;
	esac
	shift
done
PACKAGEMAKER=/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker

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
mkdir -p $PKGROOT/opt/$PACKAGE_NAME/bin
make install DESTDIR=$PKGROOT mandir=/opt/$PACKAGE_NAME/man \
	docdir=/Library/Documentation/$PACKAGE_NAME \
	exampledir=/Library/Documentation/$PACKAGE_NAME
rm -f $PKGROOT/opt/$PACKAGE_NAME/lib/*.la
mkdir -p $PKGROOT/usr/lib
mv $PKGROOT/opt/$PACKAGE_NAME/lib/libturbojpeg.* $PKGROOT/usr/lib
mkdir -p $PKGROOT/usr/include
mv $PKGROOT/opt/$PACKAGE_NAME/include/turbojpeg.h $PKGROOT/usr/include

if [ $BUILD32 = 1 ]; then
	if [ ! -d $BUILDDIR32 ]; then
		echo ERROR: 32-bit build directory $BUILDDIR32 does not exist
		exit 1
	fi
	if [ ! -f $BUILDDIR32/Makefile ]; then
		echo ERROR: 32-bit build directory $BUILDDIR32 is not configured
		exit 1
	fi
	mkdir -p $TMPDIR/dist.x86
	pushd $BUILDDIR32
	make install DESTDIR=$TMPDIR/dist.x86
	popd
	if [ ! -h $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib -a \
		! -h $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib ]; then
		lipo -create \
			-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib \
			-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib \
			-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib
	elif [ ! -h $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@.dylib -a \
		! -h $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@.dylib ]; then
		lipo -create \
			-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@.dylib \
			-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@.dylib \
			-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@.dylib
	fi
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.a \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.a \
		-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.a
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libturbojpeg.dylib \
		-arch x86_64 $PKGROOT/usr/lib/libturbojpeg.dylib \
		-output $PKGROOT/usr/lib/libturbojpeg.dylib
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libturbojpeg.a \
		-arch x86_64 $PKGROOT/usr/lib/libturbojpeg.a \
		-output $PKGROOT/usr/lib/libturbojpeg.a
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/bin/cjpeg \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/bin/cjpeg \
		-output $PKGROOT/opt/$PACKAGE_NAME/bin/cjpeg
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/bin/djpeg \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/bin/djpeg \
		-output $PKGROOT/opt/$PACKAGE_NAME/bin/djpeg
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/bin/jpegtran \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/bin/jpegtran \
		-output $PKGROOT/opt/$PACKAGE_NAME/bin/jpegtran
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/bin/tjbench \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/bin/tjbench \
		-output $PKGROOT/opt/$PACKAGE_NAME/bin/tjbench
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/bin/rdjpgcom \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/bin/rdjpgcom \
		-output $PKGROOT/opt/$PACKAGE_NAME/bin/rdjpgcom
	lipo -create \
		-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/bin/wrjpgcom \
		-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/bin/wrjpgcom \
		-output $PKGROOT/opt/$PACKAGE_NAME/bin/wrjpgcom

fi

if [ $BUILDARMV6 = 1 ]; then
	if [ ! -d $BUILDDIRARMV6 ]; then
		echo ERROR: ARM v6 build directory $BUILDDIRARMV6 does not exist
		exit 1
	fi
	if [ ! -f $BUILDDIRARMV6/Makefile ]; then
		echo ERROR: ARM v6 build directory $BUILDDIRARMV6 is not configured
		exit 1
	fi
	mkdir -p $TMPDIR/dist.armv6
	pushd $BUILDDIRARMV6
	make install DESTDIR=$TMPDIR/dist.armv6
	popd
	lipo -create \
		$PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.a \
		-arch arm $TMPDIR/dist.armv6/opt/$PACKAGE_NAME/lib/libjpeg.a \
		-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.a
	lipo -create \
		$PKGROOT/usr/lib/libturbojpeg.a \
		-arch arm $TMPDIR/dist.armv6/opt/$PACKAGE_NAME/lib/libturbojpeg.a \
		-output $PKGROOT/usr/lib/libturbojpeg.a
fi

if [ $BUILDARMV7 = 1 ]; then
	if [ ! -d $BUILDDIRARMV7 ]; then
		echo ERROR: ARM v7 build directory $BUILDDIRARMV7 does not exist
		exit 1
	fi
	if [ ! -f $BUILDDIRARMV7/Makefile ]; then
		echo ERROR: ARM v7 build directory $BUILDDIRARMV7 is not configured
		exit 1
	fi
	mkdir -p $TMPDIR/dist.armv7
	pushd $BUILDDIRARMV7
	make install DESTDIR=$TMPDIR/dist.armv7
	popd
	lipo -create \
		$PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.a \
		-arch arm $TMPDIR/dist.armv7/opt/$PACKAGE_NAME/lib/libjpeg.a \
		-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.a
	lipo -create \
		$PKGROOT/usr/lib/libturbojpeg.a \
		-arch arm $TMPDIR/dist.armv7/opt/$PACKAGE_NAME/lib/libturbojpeg.a \
		-output $PKGROOT/usr/lib/libturbojpeg.a
fi

install_name_tool -id /opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.@SO_MAJOR_VERSION@.dylib
install_name_tool -id libturbojpeg.dylib $PKGROOT/usr/lib/libturbojpeg.dylib

ln -fs /usr/include/turbojpeg.h $PKGROOT/opt/$PACKAGE_NAME/include/
ln -fs /usr/lib/libturbojpeg.a $PKGROOT/opt/$PACKAGE_NAME/lib/
if [ $WITH_JAVA = 1 ]; then
	ln -fs libturbojpeg.dylib $PKGROOT/usr/lib/libturbojpeg.jnilib
fi
if [ ! -h $PKGROOT/opt/$PACKAGE_NAME/lib32 ]; then
	ln -fs lib $PKGROOT/opt/$PACKAGE_NAME/lib32
fi
if [ ! -h $PKGROOT/opt/$PACKAGE_NAME/lib64 ]; then
	ln -fs lib $PKGROOT/opt/$PACKAGE_NAME/lib64
fi

chmod 1775 $PKGROOT/Library
chmod 775 $PKGROOT/Library/Documentation 
mkdir -p $TMPDIR/pkg/Resources 

cp pkgscripts/Description.plist $TMPDIR/pkg/
cp pkgscripts/Info.plist $TMPDIR/pkg/
install -m 755 pkgscripts/uninstall $PKGROOT/opt/$PACKAGE_NAME/bin/

sudo chown -R root:admin $PKGROOT 
sudo chown -R root:0 $PKGROOT/usr 
cp $SRCDIR/release/License.rtf $SRCDIR/release/Welcome.rtf $SRCDIR/release/ReadMe.rtf $TMPDIR/pkg/Resources/ 

mkdir $TMPDIR/dmg
$PACKAGEMAKER -build -v -p $TMPDIR/dmg/$PACKAGE_NAME.pkg \
	-f $PKGROOT -r $TMPDIR/pkg/Resources \
	-i $TMPDIR/pkg/Info.plist -d $TMPDIR/pkg/Description.plist 
hdiutil create -fs HFS+ -volname $PACKAGE_NAME-$VERSION \
	-srcfolder "$TMPDIR/dmg" $TMPDIR/$PACKAGE_NAME-$VERSION.dmg 
cp $TMPDIR/$PACKAGE_NAME-$VERSION.dmg . 

exit
