#!/bin/bash
set +e
#echo "[`date +'%F %H:%M:%S'`] [INFO] useage : sh install_mdd.sh [setup_mode] [local_ip] [mdd_vip] [only_host] [mdd_password]"

if [ $# -ne 5 ]; then
  echo "$(basename $0): Missing script argument"
  echo "$(basename $0) [setup_mode] [local_ip] [mdd_vip] [only_host] [mdd_password]"
  exit 1
fi

pd=$PWD
setup_mode=$1
IP=$2
VIP=$3
only_host=$4
password=$5
password=${password:-Cosmic@2024}
source /etc/profile
set -e
#export JAVA_HOME=$JAVA_HOME
#export PATH=$PATH:$JAVA_HOME/bin

## install mdd ##
if [ -z "$JAVA_HOME" ]; then
  echo "jdk not be installed,please install jdk first!"
  exit 1
fi

redo=$pd/olapserver/redoDir
backup=$pd/olapserver/backup

mkdir -p olapserver && unzip -o bos-olap-setup.zip -d olapserver
mkdir -p $redo && mkdir -p $backup
chmod -R 755 $pd/
chmod +x $pd/olapserver/bin/start.sh

# 当mdd单独在一台主机不喝其他组件共用的时候 修改内核参数 vm.overcommit_memory = 0
if [ ${only_host} -eq 1 ]; then
  if [ $(grep "^vm.overcommit_memory" /etc/sysctl.conf | wc -l) != 0 ]; then
    # 能匹配到key但是匹配不到value 注释掉 加新的参数
    if [ $(grep "^vm.overcommit_memory = 0" /etc/sysctl.conf | wc -l) == 0 ]; then
      sed -i "/^vm.overcommit_memory/s/^/#/" /etc/sysctl.conf
      echo "vm.overcommit_memory = 0" >> /etc/sysctl.conf
      sysctl -p || echo
    fi
  else
    echo "vm.overcommit_memory = 0" >> /etc/sysctl.conf
    sysctl -p || echo
  fi
fi

# 调整启动参数，适配arm64操作系统
arch=$(arch)
if [[ ${arch} = "aarch64" ]]; then
  sed -i 's/nohup java -Duser/nohup java -DEnableMddOtherCpu=true -Duser/g' $pd/olapserver/bin/start.sh
fi

if [ $setup_mode -eq "1" ]; then
  cp -rf replication_master.xml $pd/olapserver/conf/replication.xml
  sed -i s/mdd_vip/$VIP/g $pd/olapserver/conf/replication.xml
  sed -i s/host_ip/$IP/g $pd/olapserver/conf/replication.xml
  sed -i "s:redo_dir:$redo:g" $pd/olapserver/conf/replication.xml
  sed -i "s:backup_dir:$backup:g" $pd/olapserver/conf/replication.xml

else
  cp -rf replication_slave.xml $pd/olapserver/conf/replication.xml
  sed -i s/mdd_vip/$VIP/g $pd/olapserver/conf/replication.xml
  sed -i s/host_ip/$IP/g $pd/olapserver/conf/replication.xml
  sed -i "s:redo_dir:$redo:g" $pd/olapserver/conf/replication.xml
  sed -i "s:backup_dir:$backup:g" $pd/olapserver/conf/replication.xml
fi

# 设置密码
$pd/olapserver/bin/resetAdminPW.sh $password

## register systemd ##
cat > /usr/lib/systemd/system/mdd.service << EOF
[Unit]
Description=mdd
After=syslog.target network.target

[Service]
Type=forking
Environment="JAVA_HOME=$JAVA_HOME"
Environment="PATH=$PATH:$JAVA_HOME/bin"
PIDFile=$pd/olapserver/bin/command.pid
ExecStart=$pd/olapserver/bin/start.sh start
ExecStop=$pd/olapserver/bin/start.sh stop
LimitNOFILE=1048576
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start mdd
systemctl enable mdd

## echo install result ##
count=0
max_count=5
while [ $count -le $max_count ]; do
  portOK=$(ss -tunlp | grep ':8080 ' || true)
  if [[ -z $portOK ]]; then
    sleep 5
    count=$((count + 1))
  else
    break
  fi
done
if [[ -z $portOK ]]; then
  echo "mdd install failed!"
  exit 2
else
  echo "mdd install susses!"
fi
