#!/bin/bash

# Terminate configure if no PgPool-II installed in system
array=($(rpmquery -a | grep pgpool))
if [[ ${#array[@]} == 0 ]]; then
    echo "PgPool-II not installed!"
    exit 0
fi

. ../contrib/functions.sh
. ../contrib/config

TYPE=primary
VERBOSE=false
CATEGORY=a
DB_CLUSTER=false

usage="$(basename "$0") [-h] [-p|s] [-a|b] [-d] -- Script configure pgpool-II
 where:
     -h  show this help text and exit
     -p  configure pgpool as primary
     -s  configure pgpool as standby
     -a  configure pgpool for category A (4 servers) (separate jboss and postgresql clusters) (Default: a)
     -b  configure pgpool for category B (2 servers) (jboss and postgresql cluster on same hasts) (Default: a)
     -d  configure pgpool for PostgreSQL cluster (Default: false)
 Example usage:
    configure.sh -p -a    -- PgPool will be configured for primary server Category A without PostgreSQL (app-primary)
    configure.sh -s -a    -- PgPool will be configured for primary server Category A without PostgreSQL (app-standby)
    configure.sh -p -a -d -- PgPool will be configured for primary server Category A with PostgreSQL (db-primary)
    configure.sh -s -a -d -- PgPool will be configured for primary server Category A with PostgreSQL (db-standby)
    configure.sh -p -b    -- PgPool will be configured for primary server Category B with PostgreSQL (primary)
    configure.sh -s -b    -- PgPool will be configured for primary server Category B with PostgreSQL (standby)
"

while getopts ':psvhabd' option; do
    case "$option" in
        p) TYPE=primary
            ;;
        s) TYPE=standby
            ;;
        v) VERBOSE=true
            ;;
        a) CATEGORY=a
            ;;
        b) CATEGORY=b
           DB_CLUSTER=true
            ;;
        d) DB_CLUSTER=true
            ;;
        h) echo "$usage"
            exit
            ;;
        \?) printf "illegal option: -%s\n" "$OPTARG" >&2
            echo "$usage" >&2
            exit 1
           ;;
   esac
done

read -r -p "Configure PgPool-II. Continue ? [y/N]" choice
if ! [[ $choice =~ ^([yY][eE][sS]]|[yY])$ ]]; then
    exit 0
fi

read -r -p "For which server will be configured PgPool-II(primary | standby) (Default: $TYPE):" _TYPE
if [[ ! -z $_TYPE ]]; then
    TYPE=$_TYPE
fi

if [[ $DB_CLUSTER == false && "$CATEGORY" == "a" ]]; then
    PGPOOL_PRIMARY_HOST=$JBOSS_PRIMARY_HOST
    PGPOOL_STANDBY_HOST=$JBOSS_STANDBY_HOST
fi

read -r -p "PgPool primary host (Default: $PGPOOL_PRIMARY_HOST):" _PGPOOL_PRIMARY_HOST
if [[ ! -z $_PGPOOL_PRIMARY_HOST ]]; then
    PGPOOL_PRIMARY_HOST=$_PGPOOL_PRIMARY_HOST
fi
read -r -p "PgPool standby host (Default: $PGPOOL_STANDBY_HOST):" _PGPOOL_STANDBY_HOST
if [[ ! -z $_PGPOOL_STANDBY_HOST ]]; then
    PGPOOL_STANDBY_HOST=$_PGPOOL_STANDBY_HOST
fi
read -r -p "PgPool trusted servers (Default: $PGPOOL_TRUSTED_SERVERS):" _PGPOOL_TRUSTED_SERVERS
if [[ ! -z $_PGPOOL_TRUSTED_SERVERS ]]; then
    PGPOOL_TRUSTED_SERVERS=$_PGPOOL_TRUSTED_SERVERS
fi
read -r -p "PgPool delegate ip (Default: $PGPOOL_DELEGATE_IP):" _PGPOOL_DELEGATE_IP
if [[ ! -z $_PGPOOL_DELEGATE_IP ]]; then
    PGPOOL_DELEGATE_IP=$_PGPOOL_DELEGATE_IP
fi
read -r -p "PgPool virtual ip interface (Default: $PGPOOL_INTERFACE):" _PGPOOL_INTERFACE
if [[ ! -z $_PGPOOL_INTERFACE ]]; then
    PGPOOL_INTERFACE=$_PGPOOL_INTERFACE
fi

# pgpool dir
PGPOOL_DIR=/etc/pgpool-II
PGPOOL_CONFIG=$PGPOOL_DIR/pgpool.conf

cp -rf pgpool-II/pgpool-$TYPE.conf $PGPOOL_CONFIG
cp -rf init.d/pgpool /etc/init.d/pgpool
cp -rf sysconfig/pgpool /etc/sysconfig/pgpool

sed -i 's/__PGPOOL_PRIMARY_HOST__/'$PGPOOL_PRIMARY_HOST'/g' $PGPOOL_CONFIG
sed -i 's/__PGPOOL_STANDBY_HOST__/'$PGPOOL_STANDBY_HOST'/g' $PGPOOL_CONFIG
sed -i 's/__PGPOOL_TRUSTED_SERVERS__/'$PGPOOL_TRUSTED_SERVERS'/g' $PGPOOL_CONFIG
sed -i 's/__PGPOOL_DELEGATE_IP__/'$PGPOOL_DELEGATE_IP'/g' $PGPOOL_CONFIG
sed -i 's/__PGPOOL_INTERFACE__/'$PGPOOL_INTERFACE'/g' $PGPOOL_CONFIG

if [[ $DB_CLUSTER == false ]]; then
    sed -i 's/PGPOOLUSER=postgres/PGPOOLUSER=root/g' /etc/init.d/pgpool
    sed -i 's/socket_dir = '\''\/var\/pgtmp'\''/socket_dir = '\''\/var\/tmp'\''/g' $PGPOOL_CONFIG
    sed -i 's/load_balance_mode = on/load_balance_mode = off/g' $PGPOOL_CONFIG
    sed -i 's/master_slave_mode = on/master_slave_mode = off/g' $PGPOOL_CONFIG
    sed -i '/^backend_hostname/s/^/#/' $PGPOOL_CONFIG
    sed -i '/^backend_port/s/^/#/' $PGPOOL_CONFIG
    sed -i '/^backend_weight/s/^/#/' $PGPOOL_CONFIG
    sed -i '/^backend_data_directory/s/^/#/' $PGPOOL_CONFIG
    sed -i '/^backend_flag/s/^/#/' $PGPOOL_CONFIG
fi

echo "postgres:"`pg_md5 postgres` >> $PGPOOL_DIR/pcp.conf

chmod 4755 /sbin/ifconfig
chmod 4755 /usr/sbin/arping


mkdir -p /var/log/pgpool
if [[ $DB_CLUSTER == true ]]; then
    cp -rf bin/* /usr/local/bin/
    touch /etc/pgpool-II/pool_passwd
    chown postgres /etc/pgpool-II/pool_passwd

    chown postgres /var/log/pgpool
    # Change default PostgreSQL port to 5433
    #sed -i "s/PGPORT=5432/PGPORT=5433/g" /etc/sysconfig/pgsql/postgresql
    /etc/init.d/postgresql restart


    # Add check status of nodes every minute into cron
    if ! grep -Fq "pg_auto_attach_node" /etc/crontab; then
        echo '* * * * * root /usr/local/bin/pg_auto_attach_node' >> /etc/crontab
    fi
    /etc/init.d/crond restart
fi

chkconfig pgpool on
