#!/bin/bash

systemctl status vsftpd 
if [ $? -eq 0 ];then
  echo "<<============= FTP已部署 ============>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<============= FTP已部署 ============>>" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi

echo "<<=========== FTP部署[开始] ==========>>"
date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
echo "<<=========== FTP部署[开始] ==========>>" >> ./init.log
echo "##########" >> ./init.log

name=simbaftp
pass=simbaftp

yum -y install vsftpd ftp 
if [ $? -eq 0 ] ; then
  echo -e "ftp下载                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "ftp下载------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "ftp下载                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "ftp下载------------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1;
fi

userdel -rf ${name} 
useradd ${name} -d /home/ftp/ -s /sbin/nologin

if [ $? -eq 0 ];then
  echo -e "ftp用户创建                    \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "ftp用户创建----------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "ftp用户创建                    \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "ftp用户创建----------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1
fi

echo ${pass} | passwd ${name} --stdin 

if [ $? -eq 0 ];then
  echo -e "用户密码设置                   \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "用户密码设置---------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "用户密码设置                   \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "用户密码设置---------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1
fi

mkdir -p /home/ftp/{simba,pub}
chmod 777 /home/ftp/pub
chown ${name}:ftp -R /home/ftp

systemctl stop firewalld.service
systemctl disable firewalld.service 
systemctl start vsftpd 

setsebool -P tftp_home_dir on
setsebool -P ftpd_full_access on
if [ $? -eq 0 ];then
  echo -e "setsebool                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "setsebool------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "setsebool                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "setsebool-------------=----------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1
fi

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd-bak.conf
cat ./doc/vsftpdconf > /etc/vsftpd/vsftpd.conf

yum -y remove httpd 
yum -y install nginx 
if [ $? -eq 0 ];then
  echo -e "安装nginx                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "安装nginx------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "安装nginx                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "安装nginx------------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1
fi

mkdir -p /usr/share/nginx/files/init/doc/
ln -s /usr/share/nginx/files/init/doc/ /home/simba/file
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
cat ./doc/nginx.conf > /etc/nginx/nginx.conf
# systemctl enable nginx 
# systemctl start nginx 
semanage port -a -t http_port_t -p tcp 7374
touch /run/nginx.pid
nginx -c /etc/nginx/nginx.conf

if [ $? -eq 0 ];then
  echo -e "nginx启动                      \033[32m 完成 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "nginx启动------------------------------[完成]" >> ./init.log
  echo "##########" >> ./init.log
else
  echo -e "nginx启动                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo    "nginx启动------------------------------[失败]" >> ./init.log
  echo "##########" >> ./init.log
  exit 1
fi

echo "nginx -c /etc/nginx/nginx.conf" >> /etc/rc.local
echo "nginx -s reload" >> /etc/rc.local

# firewall-cmd --zone=public --add-port=7374/tcp --permanent
# firewall-cmd --reload
sed -i 's/tcp_wrappers=YES/#tcp_wrappers=YES/g' /etc/vsftpd/vsftpd.conf
sed -i 's/ftp\/:\/sbin\/nologin/ftp\/:\/bin\/bash/g' /etc/passwd

systemctl enable vsftpd 
systemctl restart vsftpd

if [ $? -eq 0 ];then
  echo "<<=========== FTP部署[完成] ==========>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<=========== FTP部署[完成] ==========>>" >> ./init.log
  echo "##########" >> ./init.log
else
  echo "<<=========== FTP部署[失败] ==========>>"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> ./init.log
  echo "<<=========== FTP部署[失败] ==========>>" >> ./init.log
  echo "##########" >> ./init.log
  exit 1
fi
