PGUSER='postgres'
PG_UID='88'
PG_GRP='postgres'
PG_GID='88'
PGDATA='/srv/pgData-8.4'
PGLOG='/var/log/postgres.log'

# arg 1:  the new package version
post_install() {  
  # Create new group if not already existing
  getent group $PG_GRP >/dev/null 2>&1 || groupadd -g $PG_GID $PG_GRP &>/dev/null
  # Create new user if not already existing
  getent passwd $PGUSER >/dev/null 2>&1 || useradd -u $PG_UID -g $PG_GRP -s /bin/bash -d $PGDATA $PGUSER &>/dev/null
  
  # Init database files
  if [ ! -d "$PGDATA" ] ; then
    echo "==> postgres: I'm going to initialize a database in $PGDATA for you... This will probably take a minute..."
    # Generate a psuedo-random password and store in
    # a temp file to use for setting the superuser password
    TMPFILE=`mktemp`
    date +%a%^b\@%G%H%N > $TMPFILE
    chown ${PGUSER}:${PG_GRP} $TMPFILE
    
    # Make the data directory, fix permissions and init the db
    mkdir $PGDATA
    chown ${PGUSER}:${PG_GRP} -R $PGDATA
    su - postgres -c "/usr/bin/initdb -D '$PGDATA' --locale=C --pwfile=$TMPFILE &> /dev/null"
    mv $TMPFILE $PGDATA/password.txt
    echo "==> postgres: Password for 'postgres' user in database stored in $PGDATA/password.txt"
  fi
  
  # Init log file
  if [ ! -e $PGLOG ]; then
    touch $PGLOG
    chown postgres $PGLOG
  fi  
}

# arg 1:  the new package version
# arg 2:  the old package version
post_upgrade() {
   post_install $1
}

# arg 1:  the old package version
pre_remove() {
  echo "==> postgres: Your data remains in $PGDATA"
  echo "==> postgres: I refuse to remove it automatically."
}
# vim:set ts=2 sw=2 et:
