#!/usr/bin/env bash
# ========================================== #
# install zookeeper on server machine        #
# Version: for CentOS 6.x                    #
# ========================================== #

VERSION=3.3.6
ZkVersion=zookeeper-${VERSION}
ZkVersionFile=${ZkVersion}".tar.gz"
ZkVersionUrl="http://47.52.95.108/confsys/${ZkVersionFile}"
JDKRpmURL="http://120.26.139.115/common/jdk-8u45-linux-x64.rpm"
Common_tools="http://47.52.95.108/tools/common_tools.sh"

ZkDir=/usr/local/zookeeper
ZkDataDir=/data/zookeeper-data
ZkLogDir=/data/zookeeper-logs

_SYS_CPU_NUMS_=$(cat /proc/cpuinfo | grep -c "model name")

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to run"
    exit 1
fi

if [ ! -f 'tools/common_tools.sh' ] ;then
    wget -P "tools" ${Common_tools}
    if [ ! -s 'tools/common_tools.sh' ];then
        exit 1;
    fi
    chmod +x tools/common_tools.sh
fi

if [  -s 'tools/common_tools.sh' ];then
    . tools/common_tools.sh
fi

Install_Java(){
#    if [[ "$(command -v java)" = "" ]];then
        Echo_Blue "[+] Installing java..."
        cd /usr/local/src
        [[ ! -f jdk-8u45-linux-x64.rpm ]] && wget -c ${JDKRpmURL}
        rpm -ivh --prefix /usr/java jdk-8u45-linux-x64.rpm
        export JAVA_HOME=/usr/java/jdk1.8.0_45
        export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
        export PATH=$PATH:$JAVA_HOME/bin
        export JAVA_HOME CLASSPATH PATH
#        yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel --skip-broken
#    else
#        Echo_Yellow "java aleardy Installed..."
#    fi
}

Setting_Zookeeper(){
    cd ${ZkDir}
    [[ ! -d ${ZkDir}/conf ]] && mkdir -p ${ZkDir}/conf 2>&1 >/dev/null
    [[ ! -d ${ZkLogDir} ]] && mkdir -p ${ZkLogDir} 2>&1 >/dev/null

    mv ${ZkDir}/conf/zoo_sample.cfg ${ZkDir}/conf/zoo.cfg
    sed -i '/^dataDir/c \dataDir=\/data\/zookeeper-data' ${ZkDir}/conf/zoo.cfg
    echo 'dataLogDir=/data/zookeeper-logs' >> ${ZkDir}/conf/zoo.cfg
    sed -i "/ZOO_LOG_DIR=/c  ZOO_LOG_DIR=\'\/data\/zookeeper-logs\'" ${ZkDir}/bin/zkEnv.sh

    export ZOOKEEPER_HOME=${ZkDir}
    PATH=$ZOOKEEPER_HOME/bin:$PATH
    export PATH
    cat > /etc/init.d/zookeeper <<ZOO
#/bin/sh
#chkconfig: 2345 20 80
# description:  zookeeper1
case \$1 in
          start) bin/sh ${ZkDir}/bin/zkServer.sh start   ;;
          stop) bin/sh ${ZkDir}/bin/zkServer.sh stop;;
          status) bin/sh ${ZkDir}/bin/zkServer.sh status;;
          restart) bin/sh ${ZkDir}/bin/zkServer.sh restart;;
              *)  echo "require start|stop|status|restart"  ;;
esac
ZOO

sed -ri 's@^(_ZOO_DAEMON_OUT=).*@\1"/dev/null"@g' ${ZkDir}/bin/zkServer.sh

chmod 755 /etc/init.d/zookeeper 2>&1 > /dev/null
chkconfig --add zookeeper 2>&1 > /dev/null
chkconfig zookeeper on 2>&1 > /dev/null

service zookeeper start

}

Install_Zookeeper(){
#    if [[ ! -d ${ZkDir} ]];then
        Echo_Blue "[+] install ${ZkVersion}"
        cd /usr/local/src
        if [[ ! -f ${ZkVersionFile} ]];then
           wget -c ${ZkVersionUrl}
        fi
        [[ "$(command -v java)" = "" ]] && yum install java -y
        [[ -d ${ZkVersion} ]] && rm -rf ${ZkVersion} 2>&1 > /dev/null
        [[ ! -d ${ZkDataDir} ]] && mkdir -p ${ZkDataDir} 2>&1 >/dev/null
        [[ ! -f ${ZkVersionFile}  ]] && Echo_Red "${ZkVersionFile} source file not found ..." && exit 1
        tar -xzf ${ZkVersionFile}
        mv ${ZkVersion} ${ZkDir} 2>&1 >/dev/null
        cd ${ZkDir}/src/c
        ./configure --prefix=${ZkDir}
        make -j ${_SYS_CPU_NUMS_} && make install
        Setting_Zookeeper
#    else
#        Echo_Yellow "${ZkVersion} installed already..."
#    fi
}

Install_Zookeeper

