#!/bin/bash -e

if [ ! -d "/srv/postgresql" ]; then
    /bin/mkdir -p /srv/postgresql/postgresql
    /bin/chown -R postgres: /srv/postgresql
    /usr/bin/postgresql-setup --initdb
    #/bin/rm /srv/postgresql/postgresql.conf
    #/bin/rm /srv/postgresql/pg_hba.conf  
    #/bin/rm /srv/postgresql/pg_ident.conf
elif [ ! -d "/srv/postgresql/postgresql" ]; then
    # migrate /srv/postgresql to /srv/postgresql/postgresql
    # needed for upgrade...
    mkdir /srv/postgresql/postgresql
    mv /srv/postgresql/* /srv/postgresql/postgresql || true
    chown postgres: /srv/postgresql/postgresql
    chmod 700 /srv/postgresql/postgresql
fi
# for postgresql-setup...
/bin/ln -sf /etc/postgresql/postgresql.conf /srv/postgresql/postgresql/postgresql.conf
/bin/ln -sf /etc/postgresql/pg_hba.conf /srv/postgresql/postgresql/pg_hba.conf  
/bin/ln -sf /etc/postgresql/pg_ident.conf /srv/postgresql/postgresql/pg_ident.conf
exit 0
