#!/bin/sh


# Path to postgres installation
export PG_INSTALL=/usr/local/pg82
export PG_VERSION=8.2.4

# Fire up the database
su - postgres -c "${PG_INSTALL}/bin/initdb -D ${PG_INSTALL}/data"
su - postgres -c "${PG_INSTALL}/bin/pg_ctl -D ${PG_INSTALL}/data -l ${PG_INSTALL}/data/server.log start"
su - postgres -c "sleep 4"

# Create plpgsql language
su - postgres -c "createlang plpgsql template1"
su - postgres -c "createlang -l template1"

# su - postgres -c "${PG_INSTALL}/bin/createlang plpgsql template1"
# su - postgres -c "${PG_INSTALL}/bin/createlang -l template1"

# Install tsearch2
su - postgres -c "cd /usr/local/src/postgresql-${PG_VERSION}/contrib/tsearch2 && make install"
su - postgres -c "psql -U postgres -f /usr/local/src/postgresql-${PG_VERSION}/contrib/tsearch2/tsearch2.sql template1"

# Install ltree
su - postgres -c "cd /usr/local/src/postgresql-${PG_VERSION}/contrib/ltree && make install"
su - postgres -c "psql -U postgres -f /usr/local/src/postgresql-${PG_VERSION}/contrib/ltree/ltree.sql template1"


# configure pg via pg_hba.conf and postgres.conf
#	- pg_hba.conf: host all all 192.168.168.199 255.255.255.255 trust
#	- postgres.conf: see 3 file in pg dir
echo "........ copying pg config files over ........"

su - postgres -c "cp /home/start/oacs-installation-scripts/data/pg_hba.conf /usr/local/pgsql/data"
su - postgres -c "cp /home/start/oacs-installation-scripts/data/postgresql.conf /usr/local/pgsql/data"
/etc/init.d/pg82 restart


# Tune Postgres. The default values for PostgreSQL are very conservative; we can safely change some of them and improve performance.
echo 134217728 >/proc/sys/kernel/shmmax

# Make that change permanent by editing /etc/sysctl.conf to add these lines at the end
echo "" >>/etc/sysctl.conf
echo "#increase shared memory limit for postgres" >>/etc/sysctl.conf
echo "kernel.shmmax = 134217728" >>/etc/sysctl.conf
echo "........ DONE setting up PG82 ........"


echo "........ DONE copying pg config files over ........"
