#!/bin/bash
# dbench installer
P=dbench-install
DEFAULT_VERSION=3.04
. $SHELLPACK_INCLUDE/common.sh
GIT_LOCATION=git://git.samba.org/sahlberg/dbench.git
WEB_LOCATION=http://samba.org/ftp/tridge/dbench/
MIRROR_LOCATION="$WEBROOT/dbench/"

install-depends popt-devel

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

# Unconditionally fetch the tar to find out the real version number
TARFILE=dbench-${VERSION}.tar.gz
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
        error "$P: tar xf dbench-${VERSION}.tar.gz failed"
        popd > /dev/null
        exit $SHELLPACK_ERROR
fi

# Rename directory to something we expect.
DST_DIR=`tar tf $TARFILE | head -n 1 | awk -F / '{print $1}'`
mv $DST_DIR dbench-${VERSION}
pushd dbench-${VERSION} > /dev/null || die Failed to rename tar

# Build
pushd $SHELLPACK_SOURCES/dbench-${VERSION} || die Failed to change to source directory
./autogen.sh || die Failed to run autogen
./configure --prefix=$SHELLPACK_SOURCES/dbench-${VERSION}-installed
if [ $? -ne 0 ]; then
	error "$P: configure failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make -j$NUMCPUS
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make install
if [ $? -ne 0 ]; then
	error "$P: make install failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

# Fixups
if [ ! -e $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share ]; then
	cp $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/doc/dbench/loadfiles/client.txt $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share
fi

echo dbench installed successfully
#### Description dbench
#### Details dbench 16
