#!/bin/bash
# This is the script to install the readprofile programs
#

. /etc/autobench.conf || . functions

WEB_LOCATION="http://www.kernel.org/pub/linux/utils/util-linux/util-linux-2.11z.tar.gz"
. $AUTODIR/var/server-repository

wget -q -O $AUTODIR/sources/util-linux-latest.tar.gz $SERVER_REPO/sources/util-linux-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/util-linux-latest.tar.gz $WEB_LOCATION
  if [ $? -ne 0 ]; then
    # Couldn't get the source -- complain
    echo "WARNING: could not download the source for readprofil"
    exit 1
  fi
fi

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

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

# This may not compile out of the box... we'll try and make it work though...
pushd util-linux > /dev/null
./configure
pushd sys-utils > /dev/null
make readprofile
mkdir -p $AUTODIR/sources/readprofile/
cp readprofile $AUTODIR/sources/readprofile/

# get some helper scripts
wget -q "$SERVER_REPO/sources/kernbench/avgprofile"
check_status "Getting avgprofile script"
chmod a+x avgprofile
mv avgprofile $AUTODIR/sources/readprofile/

popd > /dev/null
popd > /dev/null

popd > /dev/null
