#!/bin/bash

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

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

name=simbaftp
pass=simbaftp

apt-get install vsftpd ftp -y

userdel -rf ${name} 
useradd  -d /home/ftp/ -m ${name}

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

#echo ${pass} | passwd ${name} 
echo simbaftp:simbaftp | chpasswd

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

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

 
#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.conf /etc/vsftpd-bak.conf
cp ./sh/doc/vsftpd.conf /etc/vsftpd.conf
#cat /root/vsftpdconf > /etc/vsftpd.conf

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

mkdir -p /usr/share/nginx/files/init/doc/
ln -s /usr/share/nginx/files/init/doc/ /home/$IUSER/file
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
cat ./sh/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' >> /home/start.log
  echo "nginx启动----------------------------[完成]" >> /home/start.log
  echo "###########################################" >> /home/start.log
else
  echo -e "nginx启动                      \033[31m 失败 \033[0m"
  date '+时间 : %Y-%m-%d %H:%M:%S' >> /home/stop.log
  echo "nginx启动----------------------------[失败]" >> /home/stop.log
  echo "###########################################" >> /home/start.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

systemctl enable vsftpd 
systemctl restart vsftpd

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