for f12
[myplc.git] / plc.d / postgresql
index 4512536..8b5739e 100755 (executable)
@@ -7,7 +7,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: postgresql 635 2007-07-05 11:08:14Z thierry $
+# $Id$
 #
 
 # Source function library and configuration
@@ -46,13 +46,19 @@ postgresql_start ()
     return 1
 }
 
+postgresql_init ()
+{
+    service postgresql initdb &> /dev/null || :
+    postgresql_start
+}
+
 case "$1" in
     start)
        if [ "$PLC_DB_ENABLED" != "1" ] ; then
            exit 0
        fi
 
-       MESSAGE=$"Starting database server"
+       MESSAGE=$"Starting PostgreSQL server"
        dialog "$MESSAGE"
 
        # Set data directory and redirect startup output to /var/log/pgsql
@@ -69,7 +75,7 @@ case "$1" in
        # PostgreSQL must be started at least once to bootstrap
        # /var/lib/pgsql/data
        if [ ! -f $postgresql_conf ] ; then
-           postgresql_start
+           postgresql_init
            check
            service postgresql stop
            check
@@ -95,6 +101,27 @@ case "$1" in
        PLC_API_IP=$(gethostbyname $PLC_API_HOST)
        PLC_BOOT_IP=$(gethostbyname $PLC_BOOT_HOST)
        PLC_WWW_IP=$(gethostbyname $PLC_WWW_HOST)
+       ip_failure=0
+       if [ -z "$PLC_API_IP" ] ; then
+           MESSAGE=$"PLC_API_IP is not set"
+           dialog "$MESSAGE"
+           ip_failure=1
+       fi
+       if [ -z "$PLC_BOOT_IP" ] ; then
+           MESSAGE=$"PLC_BOOT_IP is not set"
+           dialog "$MESSAGE"
+           ip_failure=1
+       fi
+       if [ -z "$PLC_WWW_IP" ] ; then
+           MESSAGE=$"PLC_WWW_IP is not set"
+           dialog "$MESSAGE"
+           ip_failure=1
+       fi
+       if [ $ip_failure -eq 1 ] ; then
+           /bin/false
+           check
+       fi
+
        (
            echo "host $PLC_DB_NAME $PLC_DB_USER 127.0.0.1/32 password"
            echo "host $PLC_DB_NAME $PLC_DB_USER $PLC_API_IP/32 password"
@@ -132,13 +159,13 @@ case "$1" in
 
        # Create the databases if necessary
        if ! psql -U $PLC_DB_USER -c "" $PLC_DB_NAME >/dev/null 2>&1 ; then
-           createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
+           createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER $PLC_DB_NAME
            psql -U $PLC_DB_USER -f /usr/share/plc_api/$PLC_DB_NAME.sql $PLC_DB_NAME
        fi
        check
        if ! psql -U $PLC_DB_USER -c "" drupal >/dev/null 2>&1 ; then
-           createdb -U postgres --encoding=UNICODE --owner=$PLC_DB_USER drupal
-           psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal
+           createdb -U postgres --template=template0 --encoding=UNICODE --owner=$PLC_DB_USER drupal
+            psql -U $PLC_DB_USER -f /var/www/html/database/database.pgsql drupal 
        fi
        check
 
@@ -146,7 +173,7 @@ case "$1" in
        ;;
 
     stop)
-       MESSAGE=$"Stopping database server"
+       MESSAGE=$"Stopping PostgreSQL server"
        dialog "$MESSAGE"
 
        # Drop the current user in case the username changes