#!/usr/bin/env bash
# filename: zookeeper_install.sh

zookeeper_ver="3.4.12"
zookeeper_url="http://wd:wd@soft.ddos.com/tarball/zookeeper/zookeeper-${zookeeper_ver}.tar.gz"
zookeeper_md5sum="f43cca610c2e041c71ec7687cddbd0c3"
owner=wd
zookeeper_src_dir=/opt/wd/src
zookeeper_app_dir=/opt/wd/app
zookeeper_etc_dir=/opt/wd/etc/zookeeper
zookeeper_data_dir=/data/dbs/zookeeper
zookeeper_log_dir=/data/logs/zookeeper
zookeeper_listen_port=2181
zookeeper_listen_addr=127.0.0.1

function install() {
    getent group  ${owner}>/dev/null 2>&1|| groupadd -r ${owner} -g 2000
    getent passwd ${owner}>/dev/null 2>&1 || useradd -r -g ${owner} -u 2000 -s /sbin/nologin ${owner}
    for dir in ${zookeeper_src_dir} \
               ${zookeeper_app_dir} \
               ${zookeeper_etc_dir} \
              ${zookeeper_data_dir} \
               ${zookeeper_log_dir}; do
        [[ -e ${dir} ]] || mkdir -pv ${dir}
    done

    chown -R ${owner} ${zookeeper_data_dir} ${zookeeper_log_dir}
    chmod -R g+s ${zookeeper_data_dir} ${zookeeper_log_dir}

    [[ -e /tmp/zookeeper-${zookeeper_ver}.tar.gz ]] || curl -L ${zookeeper_url} -o /tmp/zookeeper-${zookeeper_ver}.tar.gz >/dev/null 2>&1
    echo "${zookeeper_md5sum} /tmp/zookeeper-${zookeeper_ver}.tar.gz" | md5sum -c
    if [[ $? != '0' ]]; then
        echo "Error: Failed to check the file";
        rm -rf /tmp/zookeeper-${zookeeper_ver}.tar.gz 
        exit 67
    fi

    tar axvf /tmp/${zookeeper_url##*/} -C ${zookeeper_src_dir}
    cd ${zookeeper_app_dir} && [[ -L ${zookeeper_app_dir}/zookeeper ]] && rm -rf zookeeper
    ln -sv ../src/zookeeper-${zookeeper_ver} ./zookeeper

    cat >${zookeeper_etc_dir}/zoo.cfg<<EOF
    maxClientCnxns=0
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=${zookeeper_data_dir}
    dataLogDir=${zookeeper_log_dir}
    clientPort=${zookeeper_listen_port}
    clientPortAddress=${zookeeper_listen_addr}
EOF
    sed -i 's/^[ \t]*//g' ${zookeeper_etc_dir}/zoo.cfg

    cat >/usr/lib/systemd/system/zookeeper.service<<EOF
    [Unit]
    Description=Apache Zookeeper server
    Documentation=http://zookeeper.apache.org
    Requires=network.target remote-fs.target
    After=network.target remote-fs.target

    [Service]
    Type=forking
    SyslogIdentifier=zookeeper
    Environment="JAVA_HOME=${zookeeper_app_dir}/jdk"
    WorkingDirectory=${zookeeper_data_dir}
    User=${owner}
    Group=${owner}
    UMask=0027
    ExecStart=${zookeeper_app_dir}/zookeeper/bin/zkServer.sh start ${zookeeper_etc_dir}/zoo.cfg
    ExecStop=${zookeeper_app_dir}/zookeeper/bin/zkServer.sh stop ${zookeeper_etc_dir}/zoo.cfg
    ExecReload=${zookeeper_app_dir}/zookeeper/bin/zkServer.sh restart ${zookeeper_etc_dir}/zoo.cfg
    Restart=on-abort

    [Install]
    WantedBy=multi-user.target
EOF
    sed -i 's/^[ \t]*//g' /usr/lib/systemd/system/zookeeper.service

    systemctl daemon-reload && systemctl start zookeeper && systemctl enable zookeeper
}

function remove() {

    systemctl stop zookeeper
    rm -rf /usr/lib/systemd/system/zookeeper.service \
                        ${zookeeper_etc_dir}/zoo.cfg \
                                ${zookeeper_app_dir} \
     ${zookeeper_src_dir}/zookeeper-${zookeeper_ver}
}

case $1 in
    install|-i)
        install
        ;;
    remove|-r)
        remove
        ;;
    *)
        echo "Usage: $(basename $0 .sh)  install | remove"
esac
