#!/bin/sh

### BEGIN INIT INFO
# Provides:            PostgreSQL
# Required-Start:      $network
# Should-Start:        $remote_fs
# Required-Stop:       $network
# Should-Stop:         $remote_fs
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts PostgreSQL server.
# Description:         Starts PostgreSQL server.
# X-LFS-Provided-By:   BLFS / LFS 7.0
### END INIT INFO

. /lib/lsb/init-functions

case "$1" in
   start)
      log_info_msg "Starting PostgreSQL daemon..."
	  
	  # ilk başlatma ayarları
	  if [ ! -d /srv/pgsql/data ];then
		  install -v -dm700 /srv/pgsql/data
		  install -v -dm755 /run/postgresql
		  chown -Rv postgres:postgres /srv/pgsql /run/postgresql
		  su - postgres -c '/usr/bin/initdb -E UTF8 -D /srv/pgsql/data'
	  fi
      mkdir -p /run/postgresql
      chown -Rv postgres:postgres /run/postgresql
      su - postgres -c '/usr/bin/pg_ctl start -W -D /srv/pgsql/data \
                         -l /srv/pgsql/data/logfile -o "-i" '
      evaluate_retval
      ;;

   stop)
      log_info_msg "Stopping PostgreSQL daemon..."
      su - postgres -c "/usr/bin/pg_ctl stop -m smart -D /srv/pgsql/data"
      evaluate_retval
      ;;

   restart)
      $0 stop
      sleep 1
      $0 start
      ;;

   status)
      su - postgres -c "/usr/bin/pg_ctl status -D /srv/pgsql/data"
      ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac

# End /etc/init.d/postgresql
