#!/bin/bash
#

#***********************************************************************
# First time install, needs to run as root:
#***********************************************************************
echo
echo "---: ADDING USER 'postgres' WITH PASSWORD 'postgres' :---"
echo
useradd -m postgres -s /bin/bash && autopasswd postgres postgres postgres

echo
echo "---: MAKING 'postgres' OWNER OF DB DIRECTORY :---"
echo
mkdir /home/postgres/pgdata && chown postgres:users /home/postgres/pgdata

echo
echo "---: INITIALIZING DEFAULT DATABASE CLUSTER :---"
echo
sudo -u postgres initdb -D /home/postgres/pgdata

echo
echo "---: INSTALLING contrib/adminpack TO DEFAULT DATABASE CLUSTER :---"
echo
sudo -u postgres pg_ctl start -w -l /home/postgres/pg.log -D /home/postgres/pgdata
### 9.x uses different contrib installation methods:
#psql -U postgres -d postgres -f /usr/share/postgresql/contrib/uninstall_adminpack.sql
#psql -U postgres -d postgres -f /usr/share/postgresql/contrib/adminpack.sql
psql -U postgres -d postgres -c "DROP EXTENSION IF EXISTS adminpack;"
psql -U postgres -d postgres -c "CREATE EXTENSION IF NOT EXISTS adminpack;"

echo
echo "---: DELETING THIS MENU OPTION :---"
echo
rm /usr/share/applications/postgresql/pg_first-run.desktop

echo
echo "---: POST-INSTALL SCRIPT HAS FINISHED :---"
echo "---: CLOSE THIS TERMINAL AFTER READING SCRIPT RESULTS :---"
echo

