#!/bin/bash
# This script installs postgres and leaves it ready for benchmarking
P=postgresbuild-install
DEFAULT_VERSION=9.6.1
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

POSTGRES_USER=nobody
POSTGRES_GROUP=nogroup

NUMCPUS=`grep processor /proc/cpuinfo | wc -l`

# 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
		;;
	--postgres-user)
		POSTGRES_USER=$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

install-depends bison gcc-c++ libstdc++-devel popt-devel zlib-devel

# Only updates the first occurance of the parameter
update_entry_cnf() {
	PARAMETER=$1
	VALUE=$2
	CONF=$POSTGRES_DATADIR/postgresql.conf

	LINE=`grep -n "^$PARAMETER" $CONF | cut -d: -f1 | head -1`
	if [ "$LINE" = "" ]; then
		LINE=`grep -n "^#$PARAMETER" $CONF | cut -d: -f1 | head -1`
		if [ "$LINE" = "" ]; then
			die Failed to locate parameter $PARAMETER
		fi
	fi
	LINEC=`wc -l $CONF | awk '{print $1}'`
	head -$(($LINE-1)) $CONF > ${CONF}.tmp
	echo $PARAMETER = $VALUE >> ${CONF}.tmp
	tail -$(($LINEC-$LINE)) $CONF >> ${CONF}.tmp

	mv ${CONF}.tmp $CONF
}

CFLAGS_MMTESTS_EXTRA="-pipe"
WEB_LOCATION=http://ftp.postgresql.org/pub/source/v${VERSION}/
MIRROR_LOCATION=$WEBROOT/postgres/
if [ ! -e /usr/lib/libncurses.so ]; then
	ln -s /usr/lib/libncurses.so.5 /usr/lib/libncurses.so
fi
# Unconditionally fetch the tar to find out the real version number
TARFILE=postgresql-${VERSION}.tar.bz2
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 postgresql-${VERSION}.tar.bz2 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 postgresbuild-${VERSION}
pushd postgresbuild-${VERSION} > /dev/null || die Failed to rename tar
pushd $SHELLPACK_SOURCES/postgresbuild-${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/postgresbuild-${VERSION}-installed --enable-thread-safety --without-krb5 --without-readline --enable-assembler
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/postgresbuild-${VERSION}-installed --enable-thread-safety --without-krb5 --without-readline --enable-assembler
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi
unset CFLAGS

# Backup pgbench contents
PGBENCHDIR="src/bin/pgbench"
if [ ! -e $PGBENCHDIR ]; then
	PGBENCHDIR="contrib/pgbench"
fi
cp -r $PGBENCHDIR /tmp

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

# Make pgbench
echo Building pgbench
cd $PGBENCHDIR || die Failed to locate pgbench directory
cp /tmp/pgbench/* .
rm -rf /tmp/pgbench
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

touch $SHELLPACK_SOURCES/postgresbuild-${VERSION}-installed/unconfigured

cd $SHELLPACK_SOURCES/postgresbuild-${VERSION}-installed || die Failed to change to installation directory.

# Configuring user
chown -R $POSTGRES_USER .
GROUPNAME=`groups $POSTGRES_USER | awk '{print $3}'`
if [ "$GROUPNAME" = "" ]; then
	GROUPNAME=`groups $POSTGRES_USER`
fi
GROUPID=`grep ^$GROUPNAME: /etc/group | cut -d: -f3`

echo Initialising database
PSQL="su -s /bin/bash $POSTGRES_USER -c"
POSTGRES_DATADIR=$SHELLPACK_DATA/pgdata
mkdir -p $POSTGRES_DATADIR
chown $POSTGRES_USER $POSTGRES_DATADIR
$PSQL "bin/initdb -D $POSTGRES_DATADIR"
if [ $? -ne 0 ]; then
	echo WARNING: initdb failed to execute, forcing access permissions on home
	chmod a+x $HOME
	$PSQL "bin/initdb -D $POSTGRES_DATADIR"
	if [ $? -ne 0 ]; then
		$PSQL "bin/initdb -D $POSTGRES_DATADIR --locale=en_GB" || die Failed to call initdb
	fi
fi

# Update the max connection count if necessary
echo o Setting max_connections: $(($NUMCPUS*6))
update_entry_cnf max_connections $(($NUMCPUS*6))

# This option just wastes time
update_entry_cnf update_process_title off

# Record the PID file
update_entry_cnf external_pid_file \'$POSTGRES_DATADIR/postmaster.pid\'
update_entry_cnf random_page_cost 3.0

# AutoVacumn
update_entry_cnf autovacuum on

# Use unix domain sockets
mkdir -p /var/run/postgresql/
chown $POSTGRES_USER /var/run/postgresql/
chmod a+rwx /var/run/postgresql/
chmod a+x $HOME
update_entry_cnf unix_socket_directories \'/var/run/postgresql/\'
update_entry_cnf unix_socket_group $GROUPID
update_entry_cnf unix_socket_permissions 0777

# Disable logging
update_entry_cnf log_connections off
update_entry_cnf log_duration off
update_entry_cnf log_hostname off

# Disable encryption
update_entry_cnf password_encryption off

echo postgres successfully installed
exit $SHELLPACK_SUCCESS
#### Description Build and setup postgres
#### Details postgresbuild 71
