#!/bin/bash
# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this code except in compliance
# with the License. You may obtain a copy of the License
# at http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software  
# distributed under the License is distributed on an "AS IS" BASIS,  
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  
# implied. See the License for the specific language governing  
# permissions and limitations under the License. 


set -x

die()
{
    echo "$@" 1>&2
    echo $0: "$@"
    umask 022
    echo $0: "$@" >> /tmp/pgaas-failures
    exit 1
}

[ -n "$CFGDIR" ] || die "CFGDIR is not set"
[ -n "$OPENECOMP" ] || die "OPENECOMP is not set"
[ -n "$NOTOPENECOMP" ] || die "NOTOPENECOMP is not set"

if $OPENECOMP
then export PGDIR=${INSTALL_ROOT}/usr/lib/postgresql/9.5
else export PGDIR=${INSTALL_ROOT}/opt/app/postgresql-9.5.2
fi
export DBROOT=/dbroot/pgdata/main
export PATH=$PATH:${INSTALL_ROOT}/opt/app/postgresql-prep/bin

$CFGDIR/etc/makecerts 

cat $CFGDIR/lib/profile.additions >> ~postgres/.profile

# Determine which system is the master.
# For central, we look first in /tmp/postgres.conf. 
#	If we don't find that, we look at the pgnodes list and pick the first one.
# For edge, we ignore /tmp/postgres.conf and go directly to the pgnodes list. 
#	Each edge site has its own master.
clustertype=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n cluster )
ismaster=no

case $clustertype in
    central )
        CONF=/tmp/postgres.conf
	if [ -f $CONF ] # OpenDCAE
	then
	    umask 077
	    TMP=$(mktemp /tmp/tmp.pi1.XXXXXXXXXX)
	    trap 'rm -f $TMP' 0 1 2 3 15
	    sed -e 's/ *: */="/' -e 's/$/"/' -e 's/=""/="/' -e 's/""$/"/' < $CONF > $TMP
	    . $TMP
	    case `hostname` in
	        $master ) ismaster=yes ;;
		*?* ) ismaster=no ;;
		'' ) die "master is not set in $CONF"
	    esac
	    PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
	    export MASTER=$( gen-repmgr-info -n "$PGNODES" -M "$master" )
	    [ -n "$MASTER" ] || die "Cannot determine master system. $CONF has '$master' (from env.yaml), which cannot be found in pgnodes."

	else
	    # not OpenDCAE
	    ismaster=yes
	    PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
	    export MASTER=$( gen-repmgr-info -n "$PGNODES" -m )
	fi
	;;
    edge )
        host=$( hostname -f )
	PGNODES=$( ${INSTALL_ROOT}/opt/app/cdf/bin/getpropvalue -n pgnodes )
	export MASTER=$( gen-repmgr-info -n "$PGNODES" -C $host )
	case $MASTER in
	    '' ) die "Cannot determine master system. Does cdf.cfg have pgnodes= in it? Is $host listed as a site?" ;;
	    DEFAULT ) ismaster=yes MASTER=$host ;;
	esac
        ;;
esac

ssh_and_cdf_okay=no

case $ismaster in
    yes ) # master
	$CFGDIR/etc/create-ssh-master &&
	$CFGDIR/etc/create-cdf-master &&
	ssh_and_cdf_okay=yes
	;;

    no ) # secondary
	$CFGDIR/etc/create-ssh-secondary &&
	$CFGDIR/etc/create-cdf-secondary &&
	touch $CFGDIR/lib/ignore-database-reconfiguration # prevent dcae_admin_db.py from looking at json DB reconfigurations &&
	ssh_and_cdf_okay=yes
	;;
esac

[ "$ssh_and_cdf_okay" = yes ] || die "Could not set up ssh or cdf"

$CFGDIR/etc/common-db-tasks
# check if we have a database already
if [ ! -s $DBROOT/PG_VERSION ]
then
    # need to create it
    case $ismaster in
	yes ) $CFGDIR/etc/create-db-master ;;
	no  ) $CFGDIR/etc/create-db-secondary ;;
    esac
else
    # need to update it
    case $ismaster in
	yes ) $CFGDIR/etc/update-db-master ;;
	no  )
	    $CFGDIR/etc/create-db-secondary # use repmgr clone even if secondary previously existed
	    # $CFGDIR/etc/update-db-secondary
	    ;;
    esac
fi
