#!/bin/sh

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

TMPDIR=

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

usage()
{
	echo "$0 [combined [32-bit build dir.]]"
	exit 1
}

COMBINED=0

PACKAGE_NAME=@PACKAGE_NAME@
VERSION=@VERSION@
BUILD=@BUILD@
PKGARCH=@DEBARCH@
SRCDIR=@abs_top_srcdir@
BUILDDIR32=@abs_top_srcdir@/solx86
WITH_JAVA=@WITH_JAVA@
if [ $# -gt 0 ]; then
	if [ "$1" = "combined" ]; then
		COMBINED=1
		if [ $# -gt 1 ]; then	BUILDDIR32=$2; fi
	fi
fi

umask 022
TMPDIR=`mktemp -d /tmp/$PACKAGE_NAME-build.XXXXXX`
rm -f $PACKAGE_NAME.pkg.bz2
cp $SRCDIR/release/copyright $TMPDIR
touch $TMPDIR/depend
cp pkgscripts/pkginfo $TMPDIR/pkginfo 

if [ "$PKGARCH" = "i386" ]; then
	__LIB=lib
else
	__LIB=lib/$PKGARCH
fi

if [ $COMBINED = 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
	PWD=`pwd`
	cd $BUILDDIR32
	make install DESTDIR=$TMPDIR mandir=/opt/$PACKAGE_NAME/man AM_MAKEFLAGS="mandir=/opt/$PACKAGE_NAME/man"
	cd $PWD
fi
# This mess is to work around a bug in /usr/ccs/bin/make
make install DESTDIR=$TMPDIR libdir=/opt/$PACKAGE_NAME/$__LIB \
	mandir=/opt/$PACKAGE_NAME/man docdir=/opt/$PACKAGE_NAME/doc \
	exampledir=/opt/$PACKAGE_NAME/doc \
	AM_MAKEFLAGS="libdir=/opt/$PACKAGE_NAME/$__LIB mandir=/opt/$PACKAGE_NAME/man docdir=/opt/$PACKAGE_NAME/doc exampledir=/opt/$PACKAGE_NAME/doc"
rm -f $TMPDIR/opt/$PACKAGE_NAME/$__LIB/*.la

cat >$TMPDIR/proto <<EOF
i copyright
i depend
i pkginfo
d none $PACKAGE_NAME 0755 root bin
d none $PACKAGE_NAME/bin 0755 root bin
f none $PACKAGE_NAME/bin/cjpeg 0755 root bin
f none $PACKAGE_NAME/bin/djpeg 0755 root bin
f none $PACKAGE_NAME/bin/jpegtran 0755 root bin
f none $PACKAGE_NAME/bin/tjbench 0755 root bin
f none $PACKAGE_NAME/bin/rdjpgcom 0755 root bin
f none $PACKAGE_NAME/bin/wrjpgcom 0755 root bin
d none $PACKAGE_NAME/lib 0755 root bin
EOF
if [ $COMBINED = 1 ]; then
cat >>$TMPDIR/proto <<EOF
f none $PACKAGE_NAME/lib/libjpeg.so.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@ 0755 root bin
s none $PACKAGE_NAME/lib/libjpeg.so.@SO_MAJOR_VERSION@=libjpeg.so.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@
s none $PACKAGE_NAME/lib/libjpeg.so=libjpeg.so.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@
f none $PACKAGE_NAME/lib/libjpeg.a 0644 root bin
f none $PACKAGE_NAME/lib/libturbojpeg.so 0755 root bin
f none $PACKAGE_NAME/lib/libturbojpeg.a 0644 root bin
EOF
fi
if [ "${__LIB}" != "lib" ]; then
echo d none $PACKAGE_NAME/${__LIB} 0755 root bin >>$TMPDIR/proto
echo s none $PACKAGE_NAME/lib64=${__LIB} >>$TMPDIR/proto
echo s none $PACKAGE_NAME/lib/64=$PKGARCH >>$TMPDIR/proto
fi
cat >>$TMPDIR/proto <<EOF
f none $PACKAGE_NAME/${__LIB}/libjpeg.so.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@ 0755 root bin
s none $PACKAGE_NAME/${__LIB}/libjpeg.so.@SO_MAJOR_VERSION@=libjpeg.so.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@
s none $PACKAGE_NAME/${__LIB}/libjpeg.so=libjpeg.so.@SO_MAJOR_VERSION@.0.@SO_MINOR_VERSION@
f none $PACKAGE_NAME/${__LIB}/libjpeg.a 0644 root bin
f none $PACKAGE_NAME/${__LIB}/libturbojpeg.so 0755 root bin
f none $PACKAGE_NAME/${__LIB}/libturbojpeg.a 0644 root bin
s none $PACKAGE_NAME/lib32=lib
d none $PACKAGE_NAME/man 0755 root bin
d none $PACKAGE_NAME/man/man1 0755 root bin
d none $PACKAGE_NAME/man/man1/cjpeg.1 0644 root bin
d none $PACKAGE_NAME/man/man1/djpeg.1 0644 root bin
d none $PACKAGE_NAME/man/man1/jpegtran.1 0644 root bin
d none $PACKAGE_NAME/man/man1/rdjpgcom.1 0644 root bin
d none $PACKAGE_NAME/man/man1/wrjpgcom.1 0644 root bin
d none $PACKAGE_NAME/include 0755 root bin
f none $PACKAGE_NAME/include/jconfig.h 0644 root bin
f none $PACKAGE_NAME/include/jerror.h 0644 root bin
f none $PACKAGE_NAME/include/jmorecfg.h 0644 root bin
f none $PACKAGE_NAME/include/jpeglib.h 0644 root bin
f none $PACKAGE_NAME/include/turbojpeg.h 0644 root bin
d none $PACKAGE_NAME/doc 0755 root bin
f none $PACKAGE_NAME/doc/README 0644 root bin
f none $PACKAGE_NAME/doc/README-turbo.txt 0644 root bin
f none $PACKAGE_NAME/doc/example.c 0644 root bin
f none $PACKAGE_NAME/doc/libjpeg.txt 0644 root bin
f none $PACKAGE_NAME/doc/structure.txt 0644 root bin
f none $PACKAGE_NAME/doc/usage.txt 0644 root bin
f none $PACKAGE_NAME/doc/wizard.txt 0644 root bin
EOF
if [ $WITH_JAVA = 1 ]; then
echo d none $PACKAGE_NAME/classes 0755 root bin >>$TMPDIR/proto
echo f none $PACKAGE_NAME/classes/turbojpeg.jar 0644 root bin >>$TMPDIR/proto
fi

pkgmk -o -r $TMPDIR/opt -d $TMPDIR -a i386 -f $TMPDIR/proto
pkgtrans -s $TMPDIR $TMPDIR/$PACKAGE_NAME-$VERSION.pkg $PACKAGE_NAME
bzip2 $TMPDIR/$PACKAGE_NAME-$VERSION.pkg
cp $TMPDIR/$PACKAGE_NAME-$VERSION.pkg.bz2 . 

exit
