#!/bin/bash

# 定义颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
RESET='\033[0m'

current_time=$(date +"%Y%m%d%H%M%S")
current_path=$(pwd)
target_path="$current_path/cert"

# 检测参数个数
if [ "$#" -ne 3 ]; then
    echo -e "${RED}错误：需要提供${YELLOW}域名、网站根目录、中间件类型${RED}三个参数${RESET}"
    echo -e "${RED}用法: ${YELLOW}bash $0 <域名> <网站根目录> <nginx/apache>${RESET}"
    echo -e ${RESET}
    exit 1
fi


# 获取传递的参数
domain="$1"
root_directory="$2"
web_server="$3"

# 检测域名是否正确
# 在这里添加你的域名验证逻辑
# 示例：检测域名是否包含有效的字符
echo -e "${GREEN}验证域名有效性...${RESET}"
if [[ ! "$domain" =~ ^[a-zA-Z0-9.-]+$ ]]; then
    echo -e "${RED}错误：无效的域名${RESET}"
    exit 1
fi


# 验证域名是否能够ping通
if ping -c 1 "$domain" &> /dev/null; then
    echo -e "${GREEN}域名 $domain 检测通过${RESET}"
else
    echo -e "${RED}错误：域名 $domain 无法ping通,请检查域名解析。${RESET}"
    exit 1
fi

echo -e "${GREEN}检测80端口是否正常...${RESET}"
if curl --output /dev/null --silent --head --fail "$domain:80"; then
    echo -e "${GREEN}域名 $domain 的80端口正常${RESET}。"
else
    echo -e "${RED}域名 $domain 的80端口不正常或不可访问,请关闭443访问并检查站点服务状态。${RESET}"
    exit 1
fi

echo -e "${GREEN}验证网站目录....${RESET}"
if [ ! -d "$root_directory" ]; then
    echo -e "${RED}错误：指定的网站根目录不存在，请检查站点根目录。${RESET}"
    exit 1
fi
echo -e "${GREEN}网站目录验证通过${RESET}"
# 如果脚本执行到这里，说明参数正确，可以继续处理其他逻辑
echo -e "${GREEN}域名: $domain${RESET}"
echo -e "${GREEN}网站根目录: $root_directory${RESET}"
cd ./acme.sh

echo -e "${GREEN}开始申请证书...${RESET}"
./acme.sh --issue -d $domain -w $root_directory

current_path=$(pwd)
target_path="$current_path/cert"
if [ -d "$target_path" ]; then
    echo -e "${GREEN}目录已经存在: ${YELLOW}$target_path${RESET}"
else
    # 如果目录不存在，则新建目录
    mkdir -p "$target_path"
    echo -e "${GREEN}新建目录: ${YELLOW}$target_path${RESET}"
fi

if [ "$web_server" == "nginx" ]; then
    ./acme.sh --install-cert -d $domain \
    --cert-file		$target_path/$domain\_$current_time\_nginx_cert.pem  \
    --key-file		$target_path/$domain\_$current_time\_nginx_key.pem  \
    --fullchain-file	$target_path/$domain\_$current_time\_nginx_fullchain.pem
    echo -e "${GREEN}证书签发成功,证书文件路径如下：${RESET}"
    echo -e "${GREEN}Key: ${YELLOW}$target_path/"$domain"_"$current_time"_nginx_key.pem${RESET}"
    echo -e "${GREEN}Cert: ${YELLOW}$target_path/"$domain"_"$current_time"_nginx_cert.pem${RESET}"
    echo -e "${GREEN}Fullchain: ${YELLOW}$target_path/"$domain"_"$current_time"_nginx_fullchain.pem${RESET}"
fi

if [ "$web_server" == "apache" ]; then
    ./acme.sh --install-cert -d $domain \
    --cert-file      $target_path/$domain\_$current_time\_apache_cert.pem  \
    --key-file       $target_path/$domain\_$current_time\_apache_key.pem  \
    --fullchain-file $target_path/$domain\_$current_time\_apache_fullchain.pem
    echo -e "${GREEN}证书签发成功,证书文件路径如下：${GREEN}"
    echo -e "${GREEN}Key: ${YELLOW}$target_path/"$domain"_"$current_time"_apache_cert.pem${RESET}"
    echo -e "${GREEN}Cert: ${YELLOW}$target_path/"$domain"_"$current_time"_apache_key.pem${RESET}"
    echo -e "${GREEN}Fullchain: ${YELLOW}$target_path/"$domain"_"$current_time"_apache_fullchain.pem${RESET}"
fi
