#!/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 [-builddir32 [32-bit build dir]] [-builddirarmv6 [ARM v6 build dir]] [-builddirarmv7 [ARM v7 build dir]]"
	exit 1
}

PACKAGE_NAME=libjpeg-turbo
VERSION=1.2.0
BUILD=20120225
SRCDIR=/c/Users/Drew/Documents/Niotso hg/trunk/Libraries/FileHandler/libjpeg-turbo/pkgscripts/..
BUILDDIR32=/c/Users/Drew/Documents/Niotso hg/trunk/Libraries/FileHandler/libjpeg-turbo/pkgscripts/../osxx86
BUILDDIRARMV6=/c/Users/Drew/Documents/Niotso hg/trunk/Libraries/FileHandler/libjpeg-turbo/pkgscripts/../iosarmv6
BUILDDIRARMV7=/c/Users/Drew/Documents/Niotso hg/trunk/Libraries/FileHandler/libjpeg-turbo/pkgscripts/../iosarmv7
WITH_JAVA=0
while [ $# -gt 0 ]; do
	case $1 in
	-h*)             usage 0                   ;;
	-builddir32)
		if [ $# -gt 1 ]; then
			if [[ ! "$2" =~ -.* ]]; then
				BUILDDIR32=$2;  shift
			fi
		fi
		;;
	-builddirarmv6)
		if [ $# -gt 1 ]; then
			if [[ ! "$2" =~ -.* ]]; then
				BUILDDIRARMV6=$2;  shift
			fi
		fi
		;;
	-builddirarmv7)
		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 [ ! "$BUILDDIR32" = "" ]; 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.62.dylib -a \
		! -h $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.dylib ]; then
		lipo -create \
			-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.62.dylib \
			-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.dylib \
			-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.dylib
	elif [ ! -h $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.62.0.0.dylib -a \
		! -h $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.0.0.dylib ]; then
		lipo -create \
			-arch i386 $TMPDIR/dist.x86/opt/$PACKAGE_NAME/lib/libjpeg.62.0.0.dylib \
			-arch x86_64 $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.0.0.dylib \
			-output $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.0.0.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 [ ! "$BUILDDIRARMV6" = "" ]; 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 [ ! "$BUILDDIRARMV7" = "" ]; 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.62.dylib $PKGROOT/opt/$PACKAGE_NAME/lib/libjpeg.62.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
