#!/bin/bash
P=dvdstore-install
DEFAULT_VERSION=21
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

VERSION=21
DBUSER=root
DBDRIVER=postgres

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--dbdriver)
		DBDRIVER=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

install-depends mono
install-depends unzip

WEB_LOCATION=http://linux.dell.com/dvdstore/
MIRROR_LOCATION=$WEBROOT/dvdstore

# Select database driver
case $DBDRIVER in
postgres)
	echo Checking postgres installation
	DBDRIVER_VERSION=9.6.1
	DVDSTORE_DBDRIVER=postgresql
	;;
*)
	die Unrecognised dbdriver $DBDRIVER
	;;
esac

# Build database and start it for dvdstore config
$SHELLPACK_INCLUDE/shellpack-bench-${DBDRIVER}build --start \
        --effective_cachesize $((MEMTOTAL_BYTES*6/10)) \
        --shared_buffers $((MEMTOTAL_BYTES/4)) \
        --work_mem $((16*1048576)) || die Failed to get usable database installation

echo Building dvdstore for $DBDRIVER
export PATH=$SHELLPACK_SOURCES/${DBDRIVER}build-${DBDRIVER_VERSION}-installed/bin:$PATH
# Unconditionally fetch the tar to find out the real version number
TARFILE=ds${VERSION}.tar.gz
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf ds${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 dvdstore-${VERSION}-installed
pushd dvdstore-${VERSION}-installed > /dev/null || die Failed to rename tar
# Unconditionally fetch the tar to find out the real version number
TARFILE=ds${VERSION}_${DVDSTORE_DBDRIVER}.tar.gz
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf ds${VERSION}_${DVDSTORE_DBDRIVER}.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 dvdstore_$DVDSTOREDRIVER-${VERSION}
pushd dvdstore_$DVDSTOREDRIVER-${VERSION} > /dev/null || die Failed to rename tar
pushd $SHELLPACK_SOURCES/dvdstore-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
mv $SHELLPACK_SOURCES/dvdstore_$DVDSTOREDRIVER-${VERSION}/* .
rmdir $SHELLPACK_SOURCES/dvdstore_$DVDSTOREDRIVER-${VERSION}

pushd $SHELLPACK_SOURCES/dvdstore-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
cd pgsqlds2
file_fetch  http://skynet.ie/~mel/mmtests-archive/dvdstore/Npgsql-2.2.4.1-net35.zip \
	$MIRROR_LOCATION/Npgsql-2.2.4.1-net35.zip \
	Npgsql.zip
unzip Npgsql.zip
if [ $? -ne 0 ]; then
	die Unable to fetch Npgsql-2.2.4.1-net35.zip
fi

echo Rebuilding ds2pgsqldrivermono.exe
gmcs ds2pgsqlfns.cs ../drivers/ds2xdriver.cs /out:ds2pgsqldrivermono.exe /r:/usr/lib/mono/gac/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll /r:Npgsql.dll
if [ $? -ne 0 ]; then
	die Failed to rebuild ds2pgsqldrivermono.exe
fi

#file_fetch http://pgfoundry.org/frs/download.php/3582/Npgsql2.0.14.3-bin-mono2.0.zip \
#	$MIRROR_LOCATION/Npgsql2.0.14.3-bin-mono2.0.zip \
#	Npgsql.zip
#unzip Npgsql.zip
#if [ $? -ne 0 ]; then
#	die Unable to fetch Npgsql2.0.14.3-bin-mono2.0.zip
#fi
#cp Npgsql2.0.14.3/Mono2.0/bin/Npgsql.dll            pgsqlds2 || die Failed to copy Npgsql.dll
#cp Npgsql2.0.14.3/Mono2.0/bin/policy.2.0.Npgsql.dll pgsqlds2 || die Failed to copy policy.2.0.Npgsql.dll

echo dvdstore for $DBDRIVER successfully installed
exit $SHELLPACK_SUCCESS
#### Description dvdstore
#### Details dvdstore 25
