#!/bin/bash
# This is the script to install the oprofile userspace utilities
#

. /etc/autobench.conf || . functions

VERSION="0.8.1-carl+970"

OPROFILE_BINS="pp/opreport utils/opcontrol daemon/oprofiled utils/op_help"

WEB_LOCATION="http://unc.dl.sourceforge.net/sourceforge/oprofile/oprofile-$VERSION.tar.gz"

. $AUTODIR/var/server-repository

if [ ! -r $AUTODIR/sources/oprofile-$VERSION.tar.gz ] ; then 
    wget -q -O $AUTODIR/sources/oprofile-$VERSION.tar.gz $SERVER_REPO/sources/oprofile-$VERSION.tar.gz
    if [ $? -ne 0 ]; then 
     # Didn't find it on the repo, try the web location
	echo "Fetching from the network"
#	wget -O $AUTODIR/sources/oprofile-$VERSION.tar.gz $WEB_LOCATION
	if [ $? -ne 0 ]; then
        # Couldn't get the source -- complain
	    echo "WARNING: could not download the source for oprofile"
	    exit 1
	fi
    fi
fi

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

DST_DIR=$(tar -tzf oprofile-$VERSION.tar.gz | head -n 1)
if [ -z "$DST_DIR" ] ; then 
    echo "Problem with tarball, aborting"
    exit 1
fi

tar -xzf oprofile-$VERSION.tar.gz
ln -sf $DST_DIR oprofile 

pushd oprofile
./configure --with-kernel-support --prefix=$AUTODIR/sources/oprofile &&
make &&
make install &&
cp $OPROFILE_BINS  $AUTODIR/sources/oprofile/ &&
chmod a+x $AUTODIR/sources/oprofile/*

popd

popd
