#!/bin/bash

git clone "git://git.postgresql.org/git/postgresql.git"
cd postgresql

git checkout REL9_4_STABLE
./configure --prefix=/usr/lib/postgresql/9.4 --enable-debug
make -j 32
make install
make -j 32 -C contrib
make -C contrib install

git checkout REL9_5_STABLE
./configure --prefix=/usr/lib/postgresql/9.5 --enable-debug
make -j 32
make install
make -j 32 -C contrib
make -C contrib install

git checkout master
./configure --prefix=/usr/lib/postgresql/9.6 --enable-debug
make -j 32
make install
make -j 32 -C contrib
make -C contrib install

echo '"postgres" ""' >/etc/pgbouncer/userlist.txt
mkdir /dev/shm/pg
chown postgres /dev/shm/pg
su - postgres

/usr/lib/postgresql/9.4/bin/initdb -D /dev/shm/pg/9.4/
/usr/lib/postgresql/9.5/bin/initdb -D /dev/shm/pg/9.5/
/usr/lib/postgresql/9.6/bin/initdb -D /dev/shm/pg/9.6/

/usr/lib/postgresql/9.4/bin/pg_ctl -D /dev/shm/pg/9.4/ -o "-p 5432" start
/usr/lib/postgresql/9.5/bin/pg_ctl -D /dev/shm/pg/9.5/ -o "-p 5433" start
/usr/lib/postgresql/9.6/bin/pg_ctl -D /dev/shm/pg/9.6/ -o "-p 5434" start

/usr/lib/postgresql/9.4/bin/pgbench -i -s 100 'host=localhost port=6432 dbname=pg94'
/usr/lib/postgresql/9.5/bin/pgbench -i -s 100 'host=localhost port=6432 dbname=pg95'
/usr/lib/postgresql/9.6/bin/pgbench -i -s 100 'host=localhost port=6432 dbname=pg96'
