#!/bin/bash
# This is the script to install the sysstat (sar) programs
#
. /etc/autobench.conf || . functions

# Sysstat has been updated to 5.0.1 and is now good to use as is except for 
# the explicit pathing of the sadc executable.  That is fixed with the patch.

# this is a temporary source.  It would be better it put it on GSA
# or something later
EXTERNAL_LATEST="http://elm3b114.beaverton.ibm.com/dats-stable-db/autobench/sources/sysstat-5.0.1.tar.gz"

. $AUTODIR/var/server-repository

BASE_FILE=$AUTODIR/sources/sysstat-latest.tar.gz
PATCH_FILE=$AUTODIR/sources/sysstat.patch

# Download the base package first.
wget -q -O $BASE_FILE $SERVER_REPO/sources/sysstat-latest.tar.gz
if [ $? -ne 0 ]; then 
	# Didn't find it on the repo, try the internal location
	rm $BASE_FILE
	echo "Fetching latest sysstat from the Internet"
	wget -q -O $BASE_FILE $EXTERNAL_LATEST
	if [ $? -ne 0 ]; then
		# Didn't find it 
    	echo "WARNING: could not download the source for sar"
    	exit 1
	fi
fi


# Now get the patch
wget -q -O $PATCH_FILE $SERVER_REPO/sources/sysstat.patch
if [ $? -ne 0 ]; then 
	echo "Error Fetching sysstat patch from the internal network"
fi

# Ok, we've now downloaded a base package, and possibly a patch....

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

DST_DIR=`tar ztf sysstat-latest.tar.gz | head -n 1`
tar zxf sysstat-latest.tar.gz
check_status "Uncompressing sysstat"
ln -sf $DST_DIR sysstat 

# Apply the patch 
pushd sysstat 2> /dev/null
patch -p1 -i $PATCH_FILE > /dev/null
check_status "Applying sysstat patch"
popd 2> /dev/null

# build it...
pushd sysstat > /dev/null
echo | make config
if [ $? -ne 0 ]; then
  echo "WARNING: unable to make sysstat configure"
fi
make
if [ $? -ne 0 ]; then
  echo "ERROR: unable to make sysstat compile"
  popd > /dev/null
  popd > /dev/null
  exit 1
fi
popd > /dev/null

popd > /dev/null
