#!/bin/bash

echo Preparing...

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

if ! [ -a t_pglist-inserts.sql ]; then
  wget http://www.sai.msu.su/~megera/postgres/files/t_pglist-inserts.sql;
fi

if ! [ -a t_pglist-inserts_1.sql ] || ! [ -a t_pglist-inserts_2.sql ] || ! [ -a t_pglist-inserts_3.sql ]; then
  head -n -7 t_pglist-inserts.sql > tmp
  cp tmp tmp1
  echo """CREATE EXTENSION rum;

CREATE INDEX pglist_fts_rum_idx4 ON t_pglist USING rum(fts rum_tsvector_ops);

--
-- PostgreSQL database dump complete
--""" >> tmp1
  mv tmp1 t_pglist-inserts_1.sql
  cp tmp tmp2
  echo """CREATE EXTENSION rum;

CREATE INDEX pglist_fts_ts_rum_idx4 ON t_pglist USING rum(fts rum_tsvector_timestamp_ops, sent)
WITH (attach = 'sent', to = 'fts');

--
-- PostgreSQL database dump complete
--""" >> tmp2
  mv tmp2 t_pglist-inserts_2.sql
  cp tmp tmp3
  echo """CREATE EXTENSION rum;

CREATE INDEX pglist_fts_ts_order_rum_idx ON t_pglist USING rum(fts rum_tsvector_timestamp_ops, sent)
WITH (attach = 'sent', to = 'fts', order_by_attach='t');

--
-- PostgreSQL database dump complete
--""" >> tmp3
  mv tmp3 t_pglist-inserts_3.sql
  rm tmp;
fi

if ! [ -a t_pglist-inserts.dump ]; then
  wget http://www.sai.msu.su/~megera/postgres/files/t_pglist-inserts.dump.gz
  gzip -d t_pglist-inserts.dump.gz;
fi

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_v3.patch
cd ..

test_postgresql_versions="postgresql_original 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-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 -d /`
    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 -d /`
    echo InstallCheck of $contrib WAL size is $((0x$finish - 0x$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 -d /`
  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 -d /`
    echo Rum \"$line\" WAL size is $((0x$finish - 0x$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 -d /`
  time bin_$postgresql/bin/psql --port $port -d test00 < test00.sql > /dev/null
  echo Rum test00.sql WAL size is $((0x$finish_global - 0x$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 -d /`
  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 -d /`
    echo Bloom \"$line\" WAL size is $((0x$finish - 0x$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 -d /`
  time bin_$postgresql/bin/psql --port $port -d test01 < test01.sql > /dev/null
  echo Bloom test01.sql WAL size is $((0x$finish_global - 0x$start_global)) bytes

  bin_$postgresql/bin/createdb --port $port test_pglists_1
  start=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr -d /`
  bin_$postgresql/bin/psql --port $port -d test_pglists_1 < t_pglist-inserts_1.sql > /dev/null
  time bin_$postgresql/bin/psql --port $port -d test_pglists_1 < t_pglist-inserts.dump > /dev/null
  finish_global=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr -d /`
  echo PgLists RUM_1 WAL size is $((0x$finish_global - 0x$start)) bytes
  bin_$postgresql/bin/dropdb --port $port test_pglists_1

  bin_$postgresql/bin/createdb --port $port test_pglists_2
  start=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr -d /`
  bin_$postgresql/bin/psql --port $port -d test_pglists_2 < t_pglist-inserts_2.sql > /dev/null
  time bin_$postgresql/bin/psql --port $port -d test_pglists_2 < t_pglist-inserts.dump > /dev/null
  finish_global=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr -d /`
  echo PgLists RUM_2 WAL size is $((0x$finish_global - 0x$start)) bytes
  bin_$postgresql/bin/dropdb --port $port test_pglists_2

  bin_$postgresql/bin/createdb --port $port test_pglists_3
  start=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr -d /`
  bin_$postgresql/bin/psql --port $port -d test_pglists_3 < t_pglist-inserts_3.sql > /dev/null
  time bin_$postgresql/bin/psql --port $port -d test_pglists_3 < t_pglist-inserts.dump > /dev/null
  finish_global=`bin_$postgresql/bin/psql --port $port -d postgres -c "COPY (SELECT pg_current_xlog_location()) TO stdout;" | tr -d /`
  echo PgLists RUM_3 WAL size is $((0x$finish_global - 0x$start)) bytes
  bin_$postgresql/bin/dropdb --port $port test_pglists_3

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