#!/usr/bin/sh

function usage()
{
   echo "$0 --SID=<SID> --INO=<INO> --passwd=<pwd> --mode=<srMode> --name=<srSiteName> --remoteHost=<srRemoteHost>"
   return 0
}

while [ $# -gt 0 ]; do
   case "$1" in  
      --SID=* )
               SID="${1#*=}";;
      --INO=* )
               INO="${1#*=}";;
      --passwd=* )
               thePasswd="${1#*=}";;
      --mode=* )
               theMode="${1#*=}";;
      --name=* )
               theSite="${1#*=}";;
      --remoteHost=* )
               theRemoteHost="${1#*=}";;
   esac
   shift
done

echo "SID=$SID INO=$INO thePasswd=$thePasswd theMode=$theMode theSite=$theSite theRemoteHost=$theRemoteHost"

test    -z "$SID" \
     -o -z "$INO" \
     -o -z "$thePasswd" \
     -o -z "$theMode" \
     -o -z "$theSite" \
     -o -z "$theRemoteHost" \
     && usage && exit 2

sid=$(echo ${SID} | tr '[:upper:]' '[:lower:]')  # to be bash 3.x compatible
#
# install hana
#
cd /hana_inst/SAPHANAINST
cat <<EOF |
<?xml version="1.0" encoding="UTF-8"?> 
<Passwords> 
<password>${thePasswd}</password> 
<system_user_password>${thePasswd}</system_user_password> 
<root_password>${thePasswd}</root_password> 
</Passwords>
EOF
__installer.HDB/hdbinst --ignore=check_platform,check_diskspace,check_hardware,check_min_mem \
    --batch \
    --sapmnt=/hana/shared \
    --sid=${SID} \
    --number=${INO} \
    --system_usage=test \
    --read_password_from_stdin=xml
#
# install and activate saphostagent
#
cd
rpm -ivh /hana_inst/rpm/saphostagent.rpm
/etc/init.d/sapinit start
#
# Creating user store key
# 
/usr/sap/${SID}/HDB${INO}/exe/hdbuserstore set slehaloc localhost:3${INO}15 system $thePasswd
#
# Preparing system replication
#
case "$theMode" in
   primary )
	    PATH="$PATH:/usr/sap/${SID}/HDB${INO}/exe"
	    hdbsql -U slehaloc "BACKUP DATA USING FILE ('firstbackup')"
            su - ${sid}adm -c "hdbnsutil -sr_enable --name=${theSite}"
             ;;
   secondary )
	    PATH="$PATH:/usr/sap/${SID}/HDB${INO}/exe"
            sapcontrol -nr ${INO} -function StopWait 60 10
            su - ${sid}adm -c "hdbnsutil -sr_register --name=${theSite} --remoteHost=${theRemoteHost} --remoteInstance=${INO} --mode=syncmem"
             ;;
esac
