#!/bin/bash
###
###wdDNS install scripts
###
##
function err {
    echo 
    echo 
    uname -m
    [ -f /etc/redhat-release ] && cat /etc/redhat-release
    echo -e "\033[31m----Install Error: $1 -----------\033[0m"
    echo
    echo -e "\033[0m"
    echo
    exit
}
function finsh {
	if [ -f "/root/admin_pw.txt" ];then
	apw=`cat /root/admin_pw.txt`
	fi	
    echo
    echo
	echo
	echo -e "      \033[31mCongratulations ,wdDNS install is complete"
	echo -e "      wdDNS http://ip:8088"
	echo -e "      username:admin passwd:$apw"
	echo -e "      more infomation please visit http://www.wddns.net\033[0m"
	echo
}

function redis_ins {
yum install gcc readline-devel -y
cd $cud
wget -c http://dl.wdlinux.cn/files/redis/redis-5.0.9.tar.gz
tar zxvf redis-5.0.9.tar.gz
cd redis-5.0.9
cd deps
cd jemalloc/
./configure
make
make install
ldconfig
cd ..
cd hiredis/
make
make install
cd ..
cd lua/
make linux
make install
cd ../..
make
make install
echo y | ./utils/install_server.sh
cd $cud
}

function supervisor_ins {
cd $cud
wget -c http://dl.wdlinux.cn/files/other/supervisor-4.2.2.tar.gz
tar zxvf supervisor-4.2.2.tar.gz
cd supervisor-4.2.2
python setup.py install
echo_supervisord_conf > /etc/supervisord.conf
mkdir -p /etc/supervisord.conf.d
echo '[include]
files=/etc/supervisord.conf.d/*.conf' >> /etc/supervisord.conf
cd $cud
}

###
if [ -d /opt/wddns/dnsa ];then
	err "wdDNS is Installed"
fi

cud=$(pwd)
yum install -y epel-release wget tar make python-setuptools
yum install redis redis-server -y
if [ $? != 0 ];then
redis_ins
fi
systemctl start redis
systemctl enable redis.service

yum install -y bind-utils -y
[ $? != 0 ] && err "bind-utils"
yum install supervisor -y
#[ $? != 0 ] && err "supervisor"
if [ $? != 0 ];then
supervisor_ins
fi
supervisord -c /etc/supervisord.conf
wget -O /usr/lib/systemd/system/supervisord.service http://dl.wdlinux.cn/files/wddns/centos-systemd-etcs
systemctl daemon-reload
systemctl enable supervisord.service
yum install mariadb-server -y
[ $? != 0 ] && err "mariadb"
service mariadb start
systemctl enable mariadb.service
ps ax |grep -v "grep" | grep "systemd-resolved" && systemctl stop systemd-resolved
pw=$(expr substr "$(echo $RANDOM | md5sum)" 1 8)
mysqladmin -u root password $pw
mysql -uroot -p$pw -e \
        "use mysql;update user set password=password('$pw') where user='root';
        delete from user where user='';
        DROP DATABASE test;
        drop user ''@'%';flush privileges;"
echo $pw > /root/mysql_pw.txt
dpw=$(expr substr "$(echo $RANDOM | md5sum)" 1 8)
mysql -uroot -p$pw -e \
"create database dnsadb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
grant all on dnsadb.* to 'dnsau'@'localhost' identified by '$dpw';"
#
dl_url="http://dl.wdlinux.cn/"
mkdir -p /opt/wddns/{dnsa,dnsd}
cd /opt/wddns
bit=`getconf LONG_BIT`
#dnsd
filename=dnsd_linux_${bit}.tar.gz
mkdir /opt/wddns/dnsd/logs
wget -c ${dl_url}files/wddns/${filename}
[ $? != 0 ] && err "download"
tar zxvf ${filename}
rm -f ${filename}
if [ -d /etc/supervisord.conf.d ];then
mv dnsd/etc/dnsd_supervisor.conf /etc/supervisord.conf.d/dnsd.conf
else
mv dnsd/etc/dnsd_supervisor.conf /etc/supervisord.d/dnsd.ini
fi
if [ $bit == "32" ];then
	if [ -f /etc/supervisord.conf.d/dnsd.conf ];then
		sed -i 's/64/32/' /etc/supervisord.conf.d/dnsd.conf
	else
		sed -i 's/64/32/' /etc/supervisord.d/dnsd.ini
	fi
fi
chmod 755 dnsd/dnsd_linux_amd64
supervisorctl reload
if [ -f /usr/bin/firewall-cmd ];then
	firewall-cmd --permanent --zone=public --add-port=8002/tcp
	firewall-cmd --permanent --zone=public --add-port=53/tcp
	firewall-cmd --permanent --zone=public --add-port=53/udp
	firewall-cmd --reload
else
	iptables -I INPUT -p tcp --dport 8002 -j ACCEPT
	iptables -I INPUT -p tcp --dport 53 -j ACCEPT
	iptables -I INPUT -p udp --dport 53 -j ACCEPT
fi
#dnsa
mkdir -p /opt/wddns/dnsa/{logs,upload/img}
filename=dnsa_linux_${bit}.tar.gz
wget -c ${dl_url}files/wddns/${filename}
[ $? != 0 ] && err "download"
tar zxvf ${filename}
chmod 755 dnsa/dnsa_linux_amd64
rm -f ${filename}
if [ -d /etc/supervisord.conf.d ];then
mv dnsa/conf/dnsa_supervisor.conf /etc/supervisord.conf.d/dnsa.conf
else
mv dnsa/conf/dnsa_supervisor.conf /etc/supervisord.d/dnsa.ini
fi
if [ $bit == "32" ];then
	if [ -f /etc/supervisord.conf.d/dnsa.conf ];then
		sed -i 's/64/32/' /etc/supervisord.conf.d/dnsa.conf
	else
		sed -i 's/64/32/' /etc/supervisord.d/dnsa.ini
	fi
fi
echo "[mysql]
password=$dpw" >> dnsa/conf/app.conf
supervisorctl reload
sleep 3
supervisorctl restart dnsa
if [ -f /usr/bin/firewall-cmd ];then
	firewall-cmd --permanent --zone=public --add-port=8088/tcp >/dev/null 2>&1
	firewall-cmd --reload >/dev/null 2>&1
else
	iptables -I INPUT -p tcp --dport 8088 -j ACCEPT >/dev/null 2>&1
fi
clear
finsh
cd $cud
rm -f install_v32.sh

