#!/bin/bash
# 文件名: BIND_INSTALL.sh
# 作者: shuhui

######################## Define Variables Start ########################
# 安装版本号
PKG_VER="9.10.3"

# 安装包下载地址
PKG_URL="http://down.vqiu.cn/package/tarball/dns/bind-${PKG_VER}.tar.gz"

# 安装路径[软链接,实际安装路径为PREFIX + 版本号]
PREFIX="/data/server/named"

# 服务运行用户名
OWNER="named"

# 缺省域名
DOMAIN="vqiu.cn"

# CPU 核数
CPU_COUNTS=$(nproc)

# 颜色变量
export black='\033[0m'
export boldblack='\033[1;0m'
export red='\033[31m'
export boldred='\033[1;31m'
export green='\033[32m'
export boldgreen='\033[1;32m'
export yellow='\033[33m'
export boldyellow='\033[1;33m'
export blue='\033[34m'
export boldblue='\033[1;34m'
export magenta='\033[35m'
export boldmagenta='\033[1;35m'
export cyan='\033[36m'
export boldcyan='\033[1;36m'
export white='\033[37m'
export boldwhite='\033[1;37m'

######################## Define Variables End ########################

function is_root()
{
	# 检查当前用户是否为 root
	if [ $(UID) != '0' ]
	then
		echo "Error: Please use root role to install me!"
		exit 67
	fi
}
function yum_depend()
{
	# 安装依赖包
	for depend_pkg in gcc openssl-devel automake
	do
		yum -y install ${depend_pkg}
	done
}


function Install_Bind()
{
	cd $(pwd)

	# 下载软件包
	if [ ! -e bind-${PKG_VER}.tar.gz ]
	then 
		wget -c ${PKG_URL}
	fi
	
	# 建立用户名
	id ${OWNER} >/dev/null 2 >&1
	if [[ $? -ne '0' ]]
	then
		/usr/sbin/groupadd ${OWNER}
		/usr/sbin/useradd -M -g ${OWNER} ${OWNER} -s /sbin/nologin 
	fi
	
	# 解压、配置、编译、安装
	tar zxvf bind-${PKG_VER}.tar.gz;cd bind-${PKG_VER}
	./configure \
	--prefix=${PREFIX}-${PKG_VER} \
	--disable-ipv6 \
	--disable-openssl-version-check \
	--enable-largefile \
	--enable-threads
	make -j ${CPU_COUNTS}
	make install 
	
	# 添加软链接
	ln -sv ${PREFIX}-${PKG_VER} ${PREFIX}

	# 添加到系统环境变量
	echo "export PATH=\$PATH:${PREFIX}/sbin:${PREFIX}/bin">/etc/profile.d/named.sh
	chmod +x /etc/profile.d/named.sh
	source /etc/profile.d/named.sh

}

function Conf(){

	# 创建日记、数据目录
	mkdir -pv ${PREFIX}-${PKG_VER}/{log,data}

	# 生成 rndc 文件
	${PREFIX}-${PKG_VER}/sbin/rndc-confgen  -u named -a -r /dev/urandom > ${PREFIX}-${PKG_VER}/etc/rndc.key

	# 下载配置文件
	cd ${PREFIX}-${PKG_VER}/etc
	wget -c http://down.vqiu.cn/conf/Linux_conf/named/primary/named.conf
	wget -c http://down.vqiu.cn/conf/Linux_conf/named/primary/log.conf
	
	cd ${PREFIX}-${PKG_VER}/data
	wget -c http://down.vqiu.cn/conf/Linux_conf/named/primary/named.ca
	wget -c http://down.vqiu.cn/conf/Linux_conf/named/primary/named.localhost
	wget -c http://down.vqiu.cn/conf/Linux_conf/named/primary/named.localhost.rev
	wget -c http://down.vqiu.cn/conf/Linux_conf/named/primary/named.test.com
	

	# 权限设置
	chown ${OWNER}.${OWNER} ${PREFIX}-${PKG_VER} -R
}

function Modify_Domain()
{
	# 修改缺省域名,如果缺省域名为test.com,则不执行
	if [[ ${DOMAIN} != "test.com" ]]
	then
		sed -i "s/test.com/${DOMAIN}/" ${PREFIX}/etc/named.conf
		\mv ${PREFIX}/data/named.test.com ${PREFIX}/data/named.${DOMAIN}
		sed -i "s/test.com/${DOMAIN}/" ${PREFIX}/data/named.${DOMAIN}
	fi
}

function Start_Service()
{
	${PREFIX}/sbin/named -u ${OWNER} -c ${PREFIX}/etc/named.conf &
	echo "${PREFIX}/sbin/named -u named -c ${PREFIX}/etc/named.conf > /dev/null 2>&1" >> /etc/rc.local
}

function MSG()
{
	sleep 2
	ss -unlp|grep 53 >/dev/null 2>&1
	if [[ $? -eq '0' ]]
	then
		echo -en $boldyellow "Good Luck，Bind-${PKG_VER} has been installd successfully.\n" $black
	else
		echo -en $boldred "Sorry，Bind-${PKG_VER} has been installd failed.\n" $black
	fi
}

is_root
yum_depend
Install_Bind
Conf
Modify_Domain
Start_Service
MSG
