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


APACHE_VERSION=2.4.17
APR_VERSION=1.5.2
PCRE_VERSION=8.35

WEB_LOCATION=http://download.joedog.org/siege

MIRROR_LOCATION="$WEBROOT/siege/"

# 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
		;;
	*)
		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

# Unconditionally fetch the tar to find out the real version number
TARFILE=siege-${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 siege-${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 siege-${VERSION}
pushd siege-${VERSION} > /dev/null || die Failed to rename tar
if [ ! -e $SHELLPACK_SOURCES/apache-build-${APACHE_VERSION}-installed ]; then
	echo Installing apache server
	$SHELLPACK_INCLUDE/shellpack-install-apachebuild -v ${APACHE_VERSION} || die Failed to install apache httpd
	echo Apache server successfully installed.
fi

# Build
export LDFLAGS=-lrt
pushd $SHELLPACK_SOURCES/siege-${VERSION} || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
export CFLAGS="-O2 $CFLAGS_MMTESTS_EXTRA"
eval ./configure --prefix=$SHELLPACK_SOURCES/siege-${VERSION}-installed 
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/siege-${VERSION}-installed 
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi
unset CFLAGS
aclocal
automake
make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make install
if [ $? -ne 0 ]; then
	error "$P: make install failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo Creating static test page
LINECOUNT=`wc -l $0 | awk '{print $1}'`
HTMLSTART=`grep -n "BEGIN HTML FILE" $0 | tail -1 | awk -F : '{print $1}'`
tail -$(($LINECOUNT-$HTMLSTART)) $0 | grep -v "^###" > $SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/htdocs/siege.html
wget -O $SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/htdocs/valid-html401 https://www.w3.org/Icons/valid-html401 || exit -1

cat > $HOME/.siegerc << EOF
verbose = false
quiet = true
gmethod = HEAD
show-logfile = true
logging = false
protocol = HTTP/1.1
chunked = true
cache = false
connection = keep-alive
concurrent = 500
file = $SHELLPACK_SOURCES/siege-${VERSION}-installed/etc/urls.txt
delay = 1
timeout = 40
failures = 10
internet = false
benchmark = false
user-agent = MMTests
accept-encoding = gzip
url-escaping = true
spinner = true
unique = true
EOF

echo siege successfully
exit $SHELLPACK_SUCCESS

==== BEGIN HTML FILE ====
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>siege dummy tester</title>
</head>
<body>
<h1>siege dummy tester</h1>

This is a basic HTML page with no useful information and serves as a static
web page for testing siege. Obviously more complex tests of the software
stack would require pages that exercise the system of interest.

Here is a text to bring the page size up to 1K.
Here is a text to bring the page size up to 1K.
Here is a text to bring the page size up to 1K.
Here is a bunch of text to bring the page size up to 1K.
Here is a bunch of text to bring the page size up to 1K.
Here is a bunch of text to bring the page size up to 1K.
Here is a bunch of text to bring the page size up to 1K.
Here is a bunch of text to bring the page size up to 1K.
Here is a bunch of text to bring the page size up to 1K.
<img src="http://localhost/valid-html401" alt="Valid HTML 4.01 Transitional" height="31" width="88">
</p>
</body>
</html>
#### Description siege
#### Details siege 55
