#!/bin/bash
# 文件名: openldap_install.sh
# 描述：OpenLDAP 源安装自动安装脚本
# 作者：shuhui

### 定义变量 开始

bdb_ver=4.6.21                                   # Berkeley DB 版本
openldap_ver=2.4.36                              # OpenLDAP 版本
cpu_core=$(grep processor /proc/cpuinfo|wc -l)   # CPU 核心[用于多]

### 定义变量 结束

if [[ $UID -ne 0 ]]; then
    echo "Error: Please use root role run this script."
    exit 1
fi


function bdb_install() {
    # 安装依赖包
    yum -y libtool-ltdl libtool-ltdl-devel gcc openssl openssl-devel
    
    if [[ ! -e db-${bdb_ver}.tar.gz ]]; then
        curl -L https://down.vqiu.cn/package/tarball/openldap/db-${bdb_ver}.tar.gz -O
    fi
    tar axvf db-${bdb_ver}.tar.gz
    cd db-${bdb_ver}/build_unix/
    ../dist/configure --prefix=/usr/local/bdb-${dbd_ver}
    make -j ${cpu_core} && make install
    
    # 创建软链接
    ln -svf /usr/local/bdb-${dbd_ver} /usr/local/bdb
    
    # 添加库文件
    cat >> /etc/ld.so.conf.d/bdb.conf <<EOF
/usr/local/dbd/
EOF
    
    # 刷新系统库
    /sbin/ldconfig -v

    # 链接头文件
    ln -svf /usr/local/bdb/include /usr/include/bdb
}

function openldap_install() {
    if [[ ! -e openldap-${openldap_ver}.tgz ]]; then
        curl -L https://down.vqiu.cn/package/tarball/openldap/openldap-${openldap_ver}.tgz -O
    fi
    tar xvf openldap-${openldap_ver}.tgz 
    cd openldap-${openldap_ver}
    ./configure --prefix=/usr/local/openldap-${openldap_ver}
    make -j ${cpu_core} && make install
     
    # 创建软链接
    ln -svf /usr/local/openldap-${openldap_ver} /usr/local/openldap
    
    # 添加库文件
    cat >> /etc/ld.so.conf.d/openldap.conf <<EOF
/usr/local/openldap
EOF
    
    # 刷新系统库
    /sbin/ldconfig -v

    # 链接头文件
    ln -svf /usr/local/ldap/include /usr/include/ldap
}


bdb_install
openldap_install

