#!/bin/sh
# PostgreSQL boot time script for Slackware, taken from the
# contributed boot-time script for FreeBSD. Please put this in:
# /etc/rc.d/rc.postgresql

# The path that is to be used for the script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Required programs should be in the PATH.
DAEMON=/usr/sbin/pg_ctl
INITDB=/usr/sbin/initdb
DATADIR=/var/lib/postgresql
LOGDIR=/var/log/postgresql
LOGFILE=${LOGDIR}/postgresql.log

# Verify that the user and group exists.
if ! grep '^postgres:' /etc/group >/dev/null 2>&1 ; then
  echo "Adding group: postgres"
  groupadd postgres
fi

if ! grep '^postgres:' /etc/passwd >/dev/null 2>&1 ; then
  echo "Adding user: postgres"
  useradd -g postgres -d "${DATADIR}" postgres
fi

# Verify log location.
mkdir -p "${LOGDIR}"
touch "${LOGFILE}"
chgrp postgres "${LOGDIR}"
chown postgres:postgres "${LOGFILE}"
chmod 0755 "${LOGDIR}"
chmod 0644 "${LOGFILE}"

# Verify data directory.
if ! test -d "${DATADIR}" ; then
  mkdir -p "${DATADIR}"
  chown -R postgres:postgres "${DATADIR}"
  find "${DATADIR}" -type d -exec chmod 700 {} \;
  find "${DATADIR}" -type f -exec chmod 600 {} \;
  su postgres -c "${INITDB} -D '${DATADIR}'"
fi

# Check for presence of /etc/rc.d/rc.postgresql*
if ! test -f /etc/rc.d/rc.postgresql ; then
  echo "There's no /etc/rc.d/rc.postgresql, installing ours now."
  mv /etc/rc.d/rc.postgresql.new /etc/rc.d/rc.postgresql
  RC=/etc/rc.d/rc.postgresql
else
  echo "There seems to already be an /etc/rc.d/rc.postgresql script."
  echo "the new one is /etc/rc.d/rc.postgresql.new"
  RC=/etc/rc.d/rc.postgresql.new
fi
echo ""
echo "Remember to make $RC executable"
echo "so the database server will be available at system bootup."

( cd usr/lib ; rm -rf libpgtypes.so )
( cd usr/lib ; ln -sf libpgtypes.so.1.2 libpgtypes.so )
( cd usr/lib ; rm -rf libecpg_compat.so.1 )
( cd usr/lib ; ln -sf libecpg_compat.so.1.1 libecpg_compat.so.1 )
( cd usr/lib ; rm -rf libpq.so.3 )
( cd usr/lib ; ln -sf libpq.so.3.2 libpq.so.3 )
( cd usr/lib ; rm -rf libecpg_compat.so )
( cd usr/lib ; ln -sf libecpg_compat.so.1.1 libecpg_compat.so )
( cd usr/lib ; rm -rf libecpg.so.4 )
( cd usr/lib ; ln -sf libecpg.so.4.2 libecpg.so.4 )
( cd usr/lib ; rm -rf libpq.so )
( cd usr/lib ; ln -sf libpq.so.3.2 libpq.so )
( cd usr/lib ; rm -rf libecpg.so )
( cd usr/lib ; ln -sf libecpg.so.4.2 libecpg.so )
( cd usr/lib ; rm -rf libpgtypes.so.1 )
( cd usr/lib ; ln -sf libpgtypes.so.1.2 libpgtypes.so.1 )
( cd usr/sbin ; rm -rf postmaster )
( cd usr/sbin ; ln -sf postgres postmaster )
