#!/bin/bash
# This script installs postgres and leaves it ready for benchmarking
###SHELLPACK preamble postgresbuild-install 11.3

POSTGRES_USER=nobody
POSTGRES_GROUP=nogroup

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

###SHELLPACK parseargBegin
###SHELLPACK parseargParam --postgres-user	 POSTGRES_USER
###SHELLPACK parseargEnd

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
###SHELLPACK sources_fetch postgresql-${VERSION}.tar.bz2 postgresbuild-${VERSION}
###SHELLPACK build_start postgresbuild-${VERSION}
###SHELLPACK build_configure postgresbuild-${VERSION} --enable-thread-safety --without-krb5 --without-readline --enable-assembler

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

###SHELLPACK make_make_install

# Make pgbench
echo Building pgbench
cd $PGBENCHDIR || die Failed to locate pgbench directory
cp /tmp/pgbench/* .
rm -rf /tmp/pgbench
###SHELLPACK make_make_install

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 false

echo postgres successfully installed
exit $SHELLPACK_SUCCESS
