#!/bin/sh

# EnterpriseDB cluster startup script
# 
# This script will start all clusters listed the /etc/edbtab file
# that have a 'Y' indicating that the cluster should be started at
# system boot time.
# This script is meant to be run at boot time
# (called by /etc/init.d/edb_autostart) but can be run manually.
# If you choose to run this script manually, it will attemp to start all
# clusters with a 'Y' unless you provide a data directory as an argument:
# i.e ./edbstart /opt/PostgresPlus/9.3AS/data, in which case, only that 
# cluster will be started. 

startAll ()
{
    # Retrieve all entries from edbtab
    for ENTRY in `cat /etc/edbtab | grep -v '^[ \t]*\#' | egrep -v ':[nN][ \t]*'`
    do
        EDB_HOME=`echo $ENTRY | awk -F: '{print $1}'`
        PGDATA=`echo $ENTRY | awk -F: '{print $2}'`
	EDBLOG=$PGDATA/pg_log/log.txt
        printf "Starting ${EDB_HOME} (${PGDATA})...\n"
        $EDB_HOME/bin/pg_ctl -D $PGDATA -W start >> $EDBLOG 2>&1
    done
}

startOne ()
{
PGDATA=$1
    # Retrieve entry from edbtab
    ENTRY=`cat /etc/edbtab | grep "${PGDATA}"`
    if [ -z $ENTRY ]; then
        printf "Entry could not be found!"
        exit 1
    fi

    # Extract components
    EDB_HOME=`echo $ENTRY | awk -F: '{print $1}'`
    EDBLOG=$PGDATA/pg_log/log.txt

    printf "Starting ${EDB_HOME} (${PGDATA})...\n"
    $EDB_HOME/bin/pg_ctl -D $PGDATA -w start >> $EDBLOG 2>&1
}

main ()
{
    if [ -z "$1" ]; then
        startAll
    else
        startOne $1
    fi
}
main $*
