#!/bin/bash

# ==========================================
# 定义颜色
# ==========================================
GREEN="\033[32m"
CYAN="\033[36m"
YELLOW="\033[33m"
RED="\033[31m"
BOLD="\033[1m"
RESET="\033[0m"

# ==========================================
# 交互式输入部分 (修复了 curl 管道无法输入的问题)
# ==========================================
echo -e "${GREEN}#################################################${RESET}"
echo -e "${GREEN}       Acme.sh 证书申请小工具 (在线交互版)${RESET}"
echo -e "${GREEN}#################################################${RESET}"
echo ""

# 1. 询问域名 (注意 < /dev/tty)
read -p "1. 请输入域名 (例: test.inklazy.com): " DOMAIN < /dev/tty
if [ -z "$DOMAIN" ]; then echo -e "${RED}❌ 域名不能为空${RESET}"; exit 1; fi

# 2. 询问 CF Token (注意 < /dev/tty)
read -p "2. 请输入 CF_Token: " INPUT_TOKEN < /dev/tty
if [ -z "$INPUT_TOKEN" ]; then echo -e "${RED}❌ Token 不能为空${RESET}"; exit 1; fi

# 3. 询问 CF Account ID (注意 < /dev/tty)
read -p "3. 请输入 CF_Account_ID: " INPUT_ACCOUNT_ID < /dev/tty
if [ -z "$INPUT_ACCOUNT_ID" ]; then echo -e "${RED}❌ Account ID 不能为空${RESET}"; exit 1; fi

# 导出环境变量
export CF_Token="$INPUT_TOKEN"
export CF_Account_ID="$INPUT_ACCOUNT_ID"

echo ""
echo ">>> 信息已确认，正在准备环境..."

# ==========================================
# 环境准备与申请
# ==========================================

# 4. 安装依赖
if [ -x "$(command -v apt-get)" ]; then
    dpkg -s socat >/dev/null 2>&1 || (apt-get update && apt-get install -y socat)
elif [ -x "$(command -v yum)" ]; then
    rpm -q socat >/dev/null 2>&1 || (yum install -y socat)
fi

# 5. 安装 acme.sh
if [ ! -f "$HOME/.acme.sh/acme.sh" ]; then
    echo ">>> 正在安装 acme.sh..."
    wget -qO- https://get.acme.sh | bash
fi

ACME_SH="$HOME/.acme.sh/acme.sh"

# 6. 设置默认 CA
"$ACME_SH" --set-default-ca --server letsencrypt >/dev/null 2>&1

# 7. 执行申请
echo ">>> 正在向 Cloudflare 验证并申请证书..."
"$ACME_SH" --issue --dns dns_cf -d "$DOMAIN"

# ==========================================
# 结果输出 (工业风)
# ==========================================
RET_CODE=$?

if [ $RET_CODE -eq 0 ]; then
    # 自动修正路径 (检测是否为 ECC 证书目录)
    if [ -d "$HOME/.acme.sh/${DOMAIN}_ecc" ]; then
        CERT_PATH="$HOME/.acme.sh/${DOMAIN}_ecc"
    else
        CERT_PATH="$HOME/.acme.sh/${DOMAIN}"
    fi

    echo ""
    echo -e "${GREEN}${BOLD}SUCCESS: 证书申请完成${RESET}"
    echo -e "${GREEN}====================================================${RESET}"
    echo ""
    echo -e "  域名:     ${CYAN}${DOMAIN}${RESET}"
    echo -e "  申请时间: $(date "+%Y-%m-%d %H:%M:%S")"
    echo ""
    echo -e "  [文件路径]"
    echo -e "  --------------------------------------------------"
    echo -e "  证书公钥: ${CYAN}${CERT_PATH}/fullchain.cer${RESET}"
    echo -e "  证书私钥: ${CYAN}${CERT_PATH}/${DOMAIN}.key${RESET}"
    echo -e "  --------------------------------------------------"
    echo ""
    echo -e "${YELLOW}  [注意]${RESET}"
    echo -e "  1. acme.sh 已配置自动续期，无需人工干预。"
    echo -e "  2. 请复制上方 ${CYAN}青色路径${RESET} 到您的配置文件中。"
    echo ""
    echo -e "${GREEN}====================================================${RESET}"
    echo ""
else
    echo ""
    echo -e "${RED}${BOLD}ERROR: 证书申请失败${RESET}"
    echo -e "${RED}====================================================${RESET}"
    echo "  请检查以下可能的问题："
    echo "  1. Cloudflare Token 是否正确？"
    echo "  2. Account ID 是否正确？"
    echo "  3. 域名解析是否已生效？"
    echo -e "${RED}====================================================${RESET}"
    echo ""
fi