#!/bin/sh
set -e
#set -x -v

### BEGIN INIT INFO
# Provides:        postgres-xc
# Required-Start:    $local_fs $remote_fs $network $time
# Required-Stop:    $local_fs $remote_fs $network $time
# Should-Start:        $syslog
# Should-Stop:        $syslog
# Default-Start:    2 3 4 5
# Default-Stop:        0 1 6
# Short-Description:    Postgres-XC RDBMS server
### END INIT INFO

PGXC_DATA=/var/lib/postgres-xc
PGXC_LOG=/var/log/postgres-xc
PGXC_RUN=/var/run/postgresql

. /lib/lsb/init-functions

if [ -r /etc/default/postgres-xc ]; then
    . /etc/default/postgres-xc
fi

# create socket directory
[ -d $PGXC_RUN ] || mkdir -p $PGXC_RUN

chown postgres-xc.postgres-xc $PGXC_RUN
chmod 2775 $PGXC_RUN

check_if_running()
{
    status=0
    case "$1" in
        gtm)
            pidfile=gtm.pid
            sleep 2
            ;;
        postgres)
            pidfile=postmaster.pid
            confport=$(grep -si '^port *=' $PGXC_DATA/$2/postgresql.conf |
	        cut -f2 -d= | sed -e 's/#.*$//' -e 's/ //g')
            port=${confport:=5432}

            # wait for server to come up, but no more than 10 seconds
            i=1
            [ -z "$3" ] &&
                while [ ! -S $PGXC_RUN/.s.PGSQL.$port ]; do
                        i=$((i+1))
                        sleep 1
                        [ $i -gt 10 ] && break
                done
            ;;
    esac

    # check if it is really running
    if [ -r $PGXC_DATA/$2/$pidfile ]; then 
        PID=`head -n 1 $PGXC_DATA/$2/$pidfile` || true
        if [ -z "$PID" ]; then
            status=1
        else
            [ "`ps h -o comm -p $PID`" != $1 ] && status=1
        fi
    else
        status=1
    fi
    return 0
}

do_ctl()
{
    ACTION=$1
    STATUS=0
    status=0
    [ "$ACTION" = "stop" ] && {
        $FAST_STOP &&
            STOP_MODE="-mf"
    }
    for TYPE in datanode coordinator gtm; do
        case $TYPE in
            gtm)
            PG_CTL=gtm_ctl
            PG_START=gtm
            ;;
            coordinator|datanode)
            PG_CTL=pg_ctl
            PG_START=postgres
            ;;
        esac
        log_daemon_msg "$2 Postgres-XC ${TYPE}"
        for NODE in $(ls /etc/postgres-xc/$TYPE/); do
            [ -f /etc/postgres-xc/$TYPE/$NODE/run ] && {
                if [ "$ACTION" = "reload" ] && [ "$TYPE" = "gtm" ]; then 
                    ACTION="restart"
                fi
                [ "$ACTION" = "stop" ] &&
                check_if_running $PG_START $NODE stop
                if [ $status -eq 1 ]; then
                    log_progress_msg "$NODE is not running"
                else
                    ERRMSG=$(start-stop-daemon -c postgres-xc \
		    	   	-Sx /usr/bin/$PG_CTL  -- $ACTION $STOP_MODE \
		    	   	-D $PGXC_DATA/$NODE -Z $TYPE \
		    	   	-l $PGXC_LOG/datanode.log
                    	STATUS=$?
                    	if $FAST_STOP && [ $STATUS -gt 0 ] && 
		        	[ "$ACTION" = "stop" ]; then
                            		STATUS=0
                            		start-stop-daemon -c postgres-xc \
					-Sx /usr/bin/$PG_CTL  -- stop -mi \
					-D $PGXC_DATA/$NODE -Z $TYPE \
					-l $PGXC_LOG/datanode.log || STATUS=$?
                    	fi) 
                    REPORT=$NODE
                    [ "$ACTION" = "status" ] && REPORT=$ERRMSG
                    log_progress_msg "$REPORT"
                    if [ $STATUS -eq 0 ] && [ "$ACTION" = "start" ]; then 
                        check_if_running $PG_START $NODE
                        STATUS=$status
                    fi
                fi
            }
        done
        log_end_msg $STATUS
    done
}


case "$1" in
    start)
    do_ctl start Starting
    ;;
    restart)
    do_ctl restart Restarting
        ;;
    status)
    do_ctl status Status
        ;;
    stop)
    do_ctl stop Stopping
        ;;
    reload)
    do_ctl reload Reloading
    ;;
    force-reload)
    $0 restart
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|force-reload|status}"
        exit 1
        ;;
esac

exit 0

