#!/bin/bash
# prereq
# 1 use root
# 2 packages: /mnt/nfs/EPM_TOOL/hana
# hanaImage72.tar  hdb_client_linux_x86_64.tgz
# docker pull keyfunc.tencentcloudcr.com/prod/hana:v61 or hanaImage.tar downloaded
# docker tag xxx hana:v61

#epm# for uat
#sha0:/home/leon/epmtool/hana # ./install_inst.sh -installPath /data_disk/EPM2 -sysPass Wang_2017 -sysPort 30015 -dataPort 30115 -hanaVersion 72

# short/for private
#  ./install_inst.sh -installPath /data_disk/EPM2

#set -x
echo "install hana"
baseDir=`pwd`
echo $baseDir

hanaVersion=72
sysPort=30015
dataPort=30115
sysPass=EPM_user_2022
hostNum=52b8fdfa2816
instName=hanaDocker
installPath='/home/epm/EPM2'
imageV='hana:v61'
CONS=true
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -hanaVersion)
      hanaVersion=$2
      shift 2
      ;;
    -CONS)
      CONS=$2
      shift 2
      ;;
    -installPath)
      installPath=$2
      shift 2
      ;;
    -sysPass)
      sysPass=$2
      shift 2
      ;;
    -dataPort)
      dataPort=$2
      shift 2
      ;;
    -sysPort)
      sysPort=$2
      shift 2
      ;;
    -instName)
      instName=$2
      shift 2
      ;;
    -hostNum)
      hostNum=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

dataDir=$installPath/HANA

echo "clean up"
dockerid=`docker ps -a | tail -n+2 | grep $instName | awk '{print $1}'`
echo "docker to rm $dockerid"
if [ $dockerid ];then
    docker stop $dockerid
    docker rm $dockerid
    echo "docker del done"
fi
rm -rf $dataDir
mkdir -p $dataDir/SCHEMA_BACKUP $dataDir/EXPORT
mkdir -p $dataDir/EXPORT
chmod -R 777 $dataDir

echo 'update os config'
#/etc/sysctl.conf
# fs.file-max=20000000
# fs.aio-max-nr=262144
# vm.memory_failure_early_kill=1
# vm.max_map_count=135217728
# net.ipv4.ip_local_port_range=40000 60999
# kernel.shmmax=1073741824
# kernel.shmmni=524288
# kernel.shmall=8388608
sedFile='/etc/sysctl.conf'
grep "net.ipv4.ip_local_port_range" $sedFile
if [ $? = 0 ];then
  sed -i "s/#*net.ipv4.ip_local_port_range.*/net.ipv4.ip_local_port_range=40000 60999/g" $sedFile  
else
  echo "net.ipv4.ip_local_port_range=40000 60999" >> $sedFile 
fi

SED_LIST=("fs.file-max=20000000" "fs.aio-max-nr=262144" "vm.memory_failure_early_kill=1" "vm.max_map_count=135217728" "kernel.shmmax=1073741824" "kernel.shmmni=524288" "kernel.shmall=8388608")
kvRegx="(.+)=(.+)"

for kv in ${SED_LIST[@]}
do
        echo "kv: $kv"
    if [[ $kv =~ $kvRegx ]]; then
        k=${BASH_REMATCH[1]}
        v=${BASH_REMATCH[2]}
        echo "kv1: $k $v"
        grep "$k" $sedFile
        if [ $? = 0 ];then
            sed -i "s/#*$k.*/$kv/g" $sedFile 
        else
            echo "$kv" >> $sedFile  
        fi
    fi
done
sysctl -p
echo "end"

echo 'load image'
hanaImageExist=`docker images | grep -E "^hana.*v$hanaVersion"`
if [[ $hanaImageExist ]]; then
   echo 'hanaImageExist'
else
   echo 'hanaImage not Exist, load image'
   docker load < hanaImage$hanaVersion.tar
fi

# hanaImageId=`docker images | grep 'keyfunc.tencentcloudcr.com/prod/hana' | awk '{print $3}'`
# docker tag $hanaImageId hana:v61
#docker rmi $imageV

echo "{\"master_password\":\"$sysPass\"}" > $dataDir/hana_passwd.json
echo "starting hana"
docker run --restart unless-stopped --name $instName -d -h $hostNum -p $sysPort:39017 -p $dataPort:39041 -v $dataDir:/hana/mounts --ulimit nofile=1048576:1048576 --sysctl kernel.shmmax=1073741824 --sysctl net.ipv4.ip_local_port_range='40000 60999' --sysctl kernel.shmall=8388608 hana:v$hanaVersion --agree-to-sap-license --passwords-url file:///hana/mounts/hana_passwd.json
dockerid=`docker ps -a | tail -n+2 | grep $instName | awk '{print $1}'`
echo "hana docker id: $dockerid"

until docker logs $dockerid --tail=1 | grep 'Startup finished!'
do
   sleep 1
done

echo 'hana started'

#install hdbsql
if test -e $dataDir/hdbclient/hdbsql
then
   echo "hdbClient exist: $dataDir/hdbclient/hdbsql"
   exit 0
fi

cd $baseDir
hdbClient='hdb_client_linux_x86_64.tgz'
if test -e $hdbClient
then
    echo "hdbClient exist: $hdbClient"
else
    echo "hdbClient does not exist, get it: $hdbClient"
    wget http://tool.epm2demo.cn/hana/$hdbClient
fi

tar zxf hdb_client_linux_x86_64.tgz
HDB_CLIENT_LINUX_X86_64/hdbinst --path=$dataDir/hdbclient
ln -sf $dataDir/hdbclient/hdbsql /usr/bin/hdbsql

# echo 'init db'
# echo "./system_init.sh -USER_PASSWORD $sysPass -SYS_PASSWORD $sysPass -SYS others -HANA_ADDR \"127.0.0.1:$dataPort\" -CONS $CONS -USER_NAME EPM -GIT_DIR db"
#  ./system_init.sh -USER_PASSWORD $sysPass -SYS_PASSWORD $sysPass -SYS others -HANA_ADDR "127.0.0.1:$dataPort" -CONS $CONS -USER_NAME EPM -GIT_DIR db
# echo 'finish'