#!/bin/sh
# This is the script to install the rawio program
#

. /etc/autobench.conf || . functions

WEB_LOCATION="ftp://ausgsa.austin.ibm.com/projects/l/ltcperformance/2003benchmarks/rawio/resources/rawio-latest.tar.gz"
WEB_LOCATION2="ftp://ausgsa.austin.ibm.com/projects/l/ltcperformance/2003benchmarks/rawio/resources/libaio-0.3.98.tar.gz"
. $AUTODIR/var/server-repository

wget -q -O $AUTODIR/sources/rawio-latest.tar.gz $SERVER_REPO/sources/rawio-latest.tar.gz
if [ $? -ne 0 ]; then
	# Didn't find it on the repo, try the web location
	echo "Fetching from the network"
	wget -q -O $AUTODIR/sources/rawio-latest.tar.gz $WEB_LOCATION
	if [ $? -ne 0 ]; then
		# Couldn't get the source -- complain
		echo "WARNING: could not download the source for rawio"
		exit 1
	fi
fi
#also need to get the libaio library since most didtros either don't ship it
# or ship an old incompatible version
wget -q -O $AUTODIR/sources/libaio-0.3.98.tar.gz $SERVER_REPO/sources/libaio-0.3.98.tar.gz
if [ $? -ne 0 ]; then
	# Didn't find it on the repo, try the web location
	echo "Fetching from the network"
	wget -q -O $AUTODIR/sources/libaio-0.3.98.tar.gz $WEB_LOCATION2
	if [ $? -ne 0 ]; then
		# Couldn't get the source -- complain
		echo "WARNING: could not download the source for libaio"
		exit 1
	fi
fi


# Uncompress it
pushd $AUTODIR/sources > /dev/null
rm -rf rawio 2> /dev/null

LIB_DIR=`tar ztf libaio-0.3.98.tar.gz | head -n 1`
rm -rf $LIB_DIR
tar zxf libaio-0.3.98.tar.gz
cd $LIB_DIR 
#./configure
make
if [ $? -ne 0 ]; then
	echo "ERROR: unable to make libaio"
	popd > /dev/null
	exit 1
fi
if [ ! -e /usr/include/libaio.h.old ]; then
	mv /usr/include/libaio.h /usr/include/libaio.h.old
fi
cp src/libaio.h /usr/include
cd ..

DST_DIR=`tar ztf rawio-latest.tar.gz | head -n 1`
tar zxf rawio-latest.tar.gz
ln -sf $DST_DIR rawio

# build it...
popd > /dev/null
pushd $AUTODIR/sources/rawio > /dev/null
cp ../$LIB_DIR/src/libaio.so.1 .
ln -s libaio.so.1 libaio.so
cp ../$LIB_DIR/src/libaio.a .

./makeraw
if [ $? -ne 0 ]; then
	echo "ERROR: unable to make rawio"
	popd > /dev/null
	exit 1
fi
popd > /dev/null

