#!/bin/bash
#in /home/hanainstall/dbscript
#install hana 61 via docker
#sha0:/home/leon/epmtool/hana # ./hana_docker_install.sh -installPath /home/epm/EPM2 -sysPass Wang_2017 -sysPort 30015 -dataPort 30115 -instName hana -hostNum 52b8fdfa2816

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

installPath='/home/epm/EPM2'
imageV='keyfunc.tencentcloudcr.com/prod/hana:v61'
CONS=true
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -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
if test -e $dataDir
then
    echo "data dir exist: $dataDir"
else
    echo "data dir does not exist, create it: $dataDir"
    mkdir $dataDir
fi

#os setup
#/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
echo "sed end"

echo 'load image'
docker load < hanaImage.tar
# hanaImageExist=`docker images | grep 'keyfunc.tencentcloudcr.com/prod/hana'`
# if [[ $hanaImageExist ]]; then
#   echo 'hanaImageExist'
# else
#   echo 'hanaImage not Exist, pulling from center'
#   docker login keyfunc.tencentcloudcr.com --username 100024241365 --password eyJhbGciOiJSUzI1NiIsImtpZCI6Ik5HS0E6WURTQzpNVlJSOlRVRUU6VTRVTDpOT0JZOkFWSDM6M0ZUWjpZSkMyOlc0Vlo6NlFYUDpLVk9EIn0.eyJvd25lclVpbiI6IjEwMDAyMzUyMDA0NiIsIm9wZXJhdG9yVWluIjoiMTAwMDI0MjQxMzY1IiwidG9rZW5JZCI6ImNjcWswZjM2Nmd2bzRpbjZmNmVnIiwiZXhwIjoxOTc5Nzk0MjM2LCJuYmYiOjE2NjQ0MzQyMzYsImlhdCI6MTY2NDQzNDIzNn0.Tg9V4OxsCvaa8TIeoGz2ZcDaIPwo-0BZlR--XUOY8OcL_Y3wZdwsJ0ZYMGfOswIIPOnemXq1583_-7tXp6w-_om1BAdUXcx5jypeTinVkHkA-xBSx2kHA8UVtrC5rVtoTbPz0SH3dOkW26ngznylM4Yxe0WKOraQxWv2yNBLcOVQAldPSd8I3WSn6PScyvDI1-t_Im2a605nGzLIqaSYj2VT5SA0FSQ5a7Xh4xHe_CP_IcTkzhWm4gVg5zobUDHi9CvyMp4M-fmLpHXYcxQWNdZBJhQla2VB_tBeVXY_14wpTX996Z52tMTiINvtRSf7CIAYcY7dqgdx-fYfLeX_aw
#   docker pull keyfunc.tencentcloudcr.com/prod/hana:v61
# fi
hanaImageId=`docker images | grep 'keyfunc.tencentcloudcr.com/prod/hana' | awk '{print $3}'`
docker tag $hanaImageId hana:v61
docker rmi $imageV
chmod -R 777 $dataDir

echo "{\"master_password\":\"$sysPass\"}" > $dataDir/hana_passwd.json

dockerid=`docker ps -a | tail -n+2 | grep hana | awk '{print $1}'`
echo "docker to rm $dockerid"
if [ $dockerid ];then 
  docker stop $dockerid
  docker rm $dockerid
  echo "docker del done"
fi
echo "starting hana"
docker run --restart unless-stopped --name $instName -d -h $hostNum -p $sysPort:39017 -p $dataPort:39041 -v $dataDir:/hana/mounts hana:v61 --agree-to-sap-license --passwords-url file:///hana/mounts/hana_passwd.json

until nc -z 127.0.0.1 30115
do
   sleep 1
done

echo 'hana started'
#install hdbsql
#wget https://fileserver-1309418224.cos.ap-shanghai.myqcloud.com/hdb_client_linux_x86_64.tgz
tar zxf hdb_client_linux_x86_64.tgz
HDB_CLIENT_LINUX_X86_64/hdbinst --path=$dataDir/hdbclient
ln -s $dataDir/hdbclient/hdbsql /usr/bin/hdbsql
#echo "export PATH=$PATH:/usr/sap/hdbclient" >> /root/.bash_profile

echo 'init db'
 ./system_init.sh -USER_PASSWORD EPM_user_2022 -SYS_PASSWORD EPM_user_2022 -SYS others -HANA_ADDR '127.0.0.1:30115' -CONS $CONS -USER_NAME EPM -GIT_DIR dbupdate
echo 'finish'
