#!/bin/bash
set +e
#echo "[`date +'%F %H:%M:%S'`] [INFO] useage : sh install_mdd.sh "
pd=$PWD
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

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

# 调整启动参数，适配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

#redo=$pd/olapserver/redo
#redoRe=$pd/olapserver/redoRe

## 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"
ExecStart=$pd/olapserver/bin/start.sh start
ExecStop=$pd/olapserver/bin/start.sh stop

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mdd
systemctl enable mdd

## echo install resault ##
portOK=`netstat -ntpl|grep "\<8080\>" || true`
if [[ -z $portOK ]];then
    echo "mdd install failed!"
else
    echo "mdd install susses!"
fi