#!/bin/bash
#
##########################################################################
#
version="0.2.2016.02.13.1"
#
########  PARAMETERS ######
#
#prim="cisbwa008"
#secn="cisbwa013"
#sid="lnx"
#SID="LNX"
#ino="00"
#primSite="ROT"
#secnSite="WDF"
#primList="cisbwa009 cisbwa010 cisbwa011 cisbwa012"
#secnList="cisbwa014 cisbwa015 cisbwa016 cisbwa017"
#primRole_cisbwa011="standby"
#primRole_cisbwa012="standby"
#secnRole_cisbwa016="standby"
#secnRole_cisbwa017="standby"
#HANA_INST_DIR="/hana/shared/rev96/SAP_HANA_DATABASE"
#
##########################################################################
#
TAG="fh_reset_ScaleOut"

#
# unmanage
function unmanage()
{
    local res=$1
    local rc=0
    logger -s -i -t $TAG "unmanage $res"
    crm resource unmanage $res; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}


#
# manage
function manage()
{
    local res=$1
    local rc=0
    logger -s -i -t $TAG "manage $res"
    crm resource manage $res; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

#
# cleanup
function cleanup()
{
    local res=$1
    local rc=0
    logger -s -i -t $TAG "cleanup $res"
    crm resource cleanup $res; rc=$?
}


#
# Stop secondary system
#
function stop_secondary()
{
    local rc=0
    logger -s -i -t $TAG "stop_secondary"
    COMMAND="hostname; sapcontrol -nr $ino -function StopSystem; sapcontrol -nr $ino -function WaitforStopped 300 10; hostname"
    ssh root@$secn "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

#
# Stop primary system
#
function stop_primary()
{
    local rc=0
    logger -s -i -t $TAG "stop_primary"
    COMMAND="hostname; sapcontrol -nr $ino -function StopSystem; sapcontrol -nr $ino -function WaitforStopped 300 10; hostname"
    ssh root@$prim "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

###
# Uninstall primary
function uninstall_primary()
{ 
    local rc=0
    logger -s -i -t $TAG "uninstall_primary"
    COMMAND="$HANA_INST_DIR/hdbuninst -b --sid=$SID"
    ssh root@$prim "$COMMAND"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

###
# Uninstall secondary
function uninstall_secondary()
{
    local rc=0
    logger -s -i -t $TAG "uninstall_secondary"
    COMMAND="$HANA_INST_DIR/hdbuninst -b --sid=$SID"
    ssh root@$secn "$COMMAND"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

###
# Install primary
function install_primary()
{
    local rc=0
    local hNode=""
    local newPrimList=""
    logger -s -i -t $TAG "install_primary"

    for hNode in $primList; do
        # if a hostname contents a dash like suse01-site1 than use variables name part suse01_site1 instead
        hNode2=$(echo $hNode | tr "-" "_")
        eval role=\$primRole_$hNode2
        if [ -n "$role" ]; then
           if [ -n "$newPrimList" ]; then
               newPrimList="$newPrimList,$hNode:role=$role"
           else
               newPrimList="$hNode:role=$role"
           fi
        else
           if [ -n "$newPrimList" ]; then
               newPrimList="$newPrimList,$hNode"
           else
               newPrimList="$hNode"
           fi
        fi
    done 
#primRole_cisbwa011
    rsync -va files/hana_passwords root@$prim:.
    COMMAND="cat hana_passwords | $HANA_INST_DIR/hdbinst --addhosts=\"$newPrimList\" --sid=${SID} --number=${ino} --batch --read_password_from_stdin=xml"
    ssh root@$prim "$COMMAND"; rc=$?
    #echo newPrimList: $newPrimList
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

###
# Install secondary
function install_secondary()
{
    local rc=0
    local hNode=""
    local newSecnList=""
    logger -s -i -t $TAG "install_secondary"
    for hNode in $secnList; do
        # if a hostname contents a dash like suse01-site1 than use variables name part suse01_site1 instead
        hNode2=$(echo $hNode | tr "-" "_")
        eval role=\$primRole_$hNode2
        if [ -n "$role" ]; then
           if [ -n "$newSecnList" ]; then
               newSecnList="$newSecnList,$hNode:role=$role"
           else
               newSecnList="$hNode:role=$role"
           fi
        else
           if [ -n "$newSecnList" ]; then
               newSecnList="$newSecnList,$hNode"
           else
               newSecnList="$hNode"
           fi
        fi
    done 
    rsync -va files/hana_passwords root@$secn:.
    COMMAND="cat hana_passwords | $HANA_INST_DIR/hdbinst --addhosts=\"$newSecnList\" --sid=${SID} --number=${ino} --batch --read_password_from_stdin=xml"
    ssh root@$secn "$COMMAND"; rc=$?
    #echo newSecnList: $newSecnList
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

####
# BACKUP PRIMARY SYSTEM
function backup_primary()
{
    local rc=0
    logger -s -i -t $TAG "backup_primary"
    # TODO: need to start that via ssh
    COMMAND="/usr/sap/$SID/HDB${ino}/exe/hdbsql -u system -p SuSE1234 -i ${ino} \"backup data using file ('lala')\""
    ssh root@$prim "$COMMAND"
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

####
# ENABLE AT PRIMARY
function enable_primary()
{ 
    local rc=0
    logger -s -i -t $TAG "enable_primary"
    COMMAND="hostname; hdbnsutil -sr_enable --name=${primSite}"
    ssh root@$prim "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

####
# REGISTER SECONDARY
function register_secondary()
{
    local rc=0
    logger -s -i -t $TAG "register_secondary"
    COMMAND="hostname; hdbnsutil -sr_register --remoteHost=${prim} --remoteInstance=${ino} --mode=sync --name=${secnSite} --force_full_replica"
    ssh root@$secn "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

function cleanup_primary()
{
    local rc=0
    logger -s -i -t $TAG "cleanup_primary"
    COMMAND="hostname; hdbnsutil -sr_cleanup --force; hostname"
    ssh root@$secn "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

####
# START PRIMARY
function start_primary()
{ 
    local rc=0
    logger -s -i -t $TAG "start_primary"
    COMMAND="hostname; hostname; sapcontrol -nr $ino -function StartSystem; sapcontrol -nr $ino -function WaitforStarted 300 10; hostname"
    ssh root@$prim "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

####
# START SECONDARY
function start_secondary()
{ 
    local rc=0
    logger -s -i -t $TAG "start_secondary"
    COMMAND="hostname; hostname; sapcontrol -nr $ino -function StartSystem; sapcontrol -nr $ino -function WaitforStarted 300 10; hostname"
    ssh root@$secn "su - ${sid}adm -c '$COMMAND'"; rc=$?
    logger -s -i -t $TAG "rc=$rc"
    return $rc
}

function manually_check_nameserver_ini()
{
    local rc=1
    logger -s -i -t $TAG "manually_check_nameserver_ini"
    ok=0
    COMMAND="hostname; cat /hana/shared/$SID/global/hdb/custom/config/nameserver.ini"
    ssh root@$secn "su - ${sid}adm -c '$COMMAND'"
    echo "is this nameserver.ini correct?"
    read yn
    case "$yn" in
      "y" | "yes" | "j" | "ja" ) ok=1;;
    esac
 
    if [ $ok -eq 0 ]; then
       echo "EXIT"
       exit 1
    fi
}

check_n_press()
{
   echo "CHECK AND PRESS ENTER"; read
}

mode=all
step=all

while [ $# -gt 0 ]; do
    case "$1" in
        --all )
                mode=all;	
                ;;
        --primary )
                mode=primary
                ;;
        --secondary )
                mode=secondary
                ;;
        --step=* ) # all|uninstall|install|enable|register
                step=${1#--step=}
                ;;
        --step )
                step="$2"
                shift
                ;;
    esac
    shift
done

#echo mode=$mode step=$step; exit 99
#
# cluster
#
#unmanage msl_SAPHana_${SID}_HDB${ino}
#unmanage cln_SAPHanaTopology_${SID}_HDB${ino}

# stop all
#
#stop_secondary
#stop_primary

# primary
#
case "$mode" in
    all | primary )
        case "$step" in
            all )
                uninstall_primary || check_n_press
	        install_primary || check_n_press
	        backup_primary || check_n_press
	        enable_primary || check_n_press
                ;;
            uninstall )
                uninstall_primary || check_n_press
                ;;
            install )
	        install_primary || check_n_press
                ;;
            enable )
                start_primary
	        backup_primary || check_n_press
	        enable_primary || check_n_press
                ;;
        esac
        ;;
esac


case "$mode" in
    all | secondary )
        # secondary
        #
        case "$step" in
            all )
                uninstall_secondary || check_n_press
                install_secondary || check_n_press
                stop_secondary || check_n_press
                register_secondary || check_n_press
                start_secondary || check_n_press
                ;;
            uninstall )
                uninstall_secondary || check_n_press
                ;;
            install )
                install_secondary || check_n_press
                ;;
            register )
                register_secondary || check_n_press
                start_secondary || check_n_press
                ;;
        esac
        ;;
esac

#
# cluster (2/2)
#
#cleanup cln_SAPHanaTopology_${SID}_HDB${ino}
#manage cln_SAPHanaTopology_${SID}_HDB${ino}
#cleanup msl_SAPHana_${SID}_HDB${ino}
#sleep 60
#manage msl_SAPHana_${SID}_HDB${ino}

####
#
# SSH-COPY-ID für alle cluster knoten und user sidadm
