#!/bin/bash

echo Preparing...

if ! [ -a postgresql ]; then
  git clone git://git.postgresql.org/git/postgresql.git;
fi
cd postgresql
git checkout REL_10_0
cd ..

if ! [ -a rum ]; then
  git clone https://github.com/postgrespro/rum
fi

rm -rf postgresql_original
rm -rf postgresql_optimized
cp -r postgresql postgresql_original
cp -r rum postgresql_original/contrib

cp -r postgresql_original postgresql_optimized
cd postgresql_optimized
patch -p1 < ../generic_xlog_diffdelta_v1.patch
cd ..

test_postgresql_versions="postgresql_optimized"
test_contribs="rum bloom"

prefix=`pwd`

port=5845

for postgresql in $test_postgresql_versions; do
  echo Testing $postgresql...
  rm -rf db_$postgresql
  rm -rf bin_$postgresql
  rm -f log_${postgresql}.log

  cd $postgresql
  ./configure --prefix=$prefix/bin_$postgresql --enable-cassert --enable-nls --with-pgport=$port > /dev/null
  make -j4 > /dev/null
  make install > /dev/null
  for contrib in $test_contribs; do
    cd contrib/$contrib
    make -j4 > /dev/null
    make install > /dev/null
    cd ../..;
  done
  cd ..

  bin_$postgresql/bin/initdb -D db_$postgresql > /dev/null 2> /dev/null
  cp postgresql.conf db_$postgresql/
  bin_$postgresql/bin/pg_ctl -D db_$postgresql -l log_${postgresql}.log start
  while !(bin_$postgresql/bin/psql --port $port -d postgres -c "SELECT 1;" > /dev/null 2> /dev/null); do
    sleep 1;
  done

  echo """CREATE FUNCTION pg_catalog.pg_current_xlog_location() RETURNS pg_lsn AS \$\$
BEGIN
    RETURN pg_current_wal_lsn();
END;
\$\$ LANGUAGE plpgsql;""" | bin_$postgresql/bin/psql --port $port -d postgres

  for contrib in $test_contribs; do
    start=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
    cd $postgresql/contrib/$contrib
    PGPORT=$port time make installcheck > /dev/null
    cd ../../..
    finish=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
    echo InstallCheck of $contrib WAL size is $(($finish - $start)) bytes;
  done

  bin_$postgresql/bin/createdb --port $port test00
  start=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
  start_global=$start
  cat test00.sql | while read line; do
    if test -z "$line"; then
      continue;
    fi
    bin_$postgresql/bin/psql --port $port -d test00 -c "$line" > /dev/null
    finish=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
    echo Rum \"$line\" WAL size is $(($finish - $start)) bytes
    start=$finish
  done 
  finish_global=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
  time bin_$postgresql/bin/psql --port $port -d test00 < test00.sql > /dev/null
  echo Rum test00.sql WAL size is $(($finish_global - $start_global)) bytes

  bin_$postgresql/bin/createdb --port $port test01
  start=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
  start_global=$start
  cat test01.sql | while read line; do
    if test -z "$line"; then
      continue;
    fi
    bin_$postgresql/bin/psql --port $port -d test01 -c "$line" > /dev/null
    finish=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
    echo Bloom \"$line\" WAL size is $(($finish - $start)) bytes
    start=$finish
  done 
  finish_global=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr / x`
  time bin_$postgresql/bin/psql --port $port -d test01 < test01.sql > /dev/null
  echo Bloom test01.sql WAL size is $(($finish_global - $start_global)) bytes

  bin_$postgresql/bin/pg_ctl -D db_$postgresql -l log_${postgresql}.log stop
done
