#!/bin/bash
# ============================================================
# Velo Hosting — SSL Kurulum Scripti (Let's Encrypt / Certbot)
# cPanel/WHM sunucusunda çalıştırın:  bash ssl_setup.sh
# ============================================================

DOMAIN="barindir.net.tr"
EMAIL="destek@barindir.net.tr"
WEBROOT="/home/barindir/public_html/public"

echo "========================================"
echo " Velo Hosting SSL Kurulum Başlıyor..."
echo " Domain : $DOMAIN"
echo " E-posta: $EMAIL"
echo "========================================"

# 1. Certbot yüklü mü?
if ! command -v certbot &> /dev/null; then
    echo "[1/4] Certbot kuruluyor..."
    if command -v apt-get &> /dev/null; then
        apt-get update -qq && apt-get install -y certbot python3-certbot-apache
    elif command -v yum &> /dev/null; then
        yum install -y certbot python3-certbot-apache
    elif command -v snap &> /dev/null; then
        snap install --classic certbot
        ln -sf /snap/bin/certbot /usr/bin/certbot
    else
        echo "HATA: Paket yöneticisi bulunamadı. Manuel kurulum gerekli."
        echo "Detay: https://certbot.eff.org/instructions"
        exit 1
    fi
else
    echo "[1/4] Certbot zaten yüklü: $(certbot --version 2>&1)"
fi

# 2. cPanel ortamında AutoSSL'yi dene
if command -v /usr/local/cpanel/bin/autossl_check &> /dev/null; then
    echo "[2/4] cPanel AutoSSL tespit edildi — Let's Encrypt sertifikası isteniyor..."
    /usr/local/cpanel/bin/autossl_check --user=$(stat -c '%U' $WEBROOT 2>/dev/null || echo "root") 2>&1
    echo "cPanel AutoSSL tamamlandı."
else
    # 3. Standalone certbot
    echo "[2/4] Sertifika alınıyor..."
    certbot certonly \
        --webroot \
        --webroot-path="$WEBROOT" \
        --domain "$DOMAIN" \
        --domain "www.$DOMAIN" \
        --email "$EMAIL" \
        --agree-tos \
        --non-interactive \
        --keep-until-expiring 2>&1

    if [ $? -ne 0 ]; then
        echo "Webroot başarısız, standalone deneniyor (Apache durdurulacak)..."
        systemctl stop apache2 httpd 2>/dev/null || true
        certbot certonly \
            --standalone \
            --domain "$DOMAIN" \
            --domain "www.$DOMAIN" \
            --email "$EMAIL" \
            --agree-tos \
            --non-interactive \
            --keep-until-expiring 2>&1
        systemctl start apache2 httpd 2>/dev/null || true
    fi
fi

# 3. Sertifika var mı kontrol
CERT_PATH="/etc/letsencrypt/live/$DOMAIN/fullchain.pem"
if [ -f "$CERT_PATH" ]; then
    echo ""
    echo "[3/4] ✅ Sertifika başarıyla alındı!"
    echo "  Fullchain : /etc/letsencrypt/live/$DOMAIN/fullchain.pem"
    echo "  Privkey   : /etc/letsencrypt/live/$DOMAIN/privkey.pem"
    echo "  Bitiş     : $(openssl x509 -noout -enddate -in $CERT_PATH)"
else
    echo "[3/4] ⚠️  Sertifika dosyası bulunamadı. Manuel kontrol gerekli."
fi

# 4. Otomatik yenileme (cron)
echo "[4/4] Otomatik yenileme cron ayarlanıyor..."
CRON_CMD="0 3 * * * certbot renew --quiet --post-hook 'systemctl reload apache2 httpd 2>/dev/null || true'"
(crontab -l 2>/dev/null | grep -v "certbot renew"; echo "$CRON_CMD") | crontab -
echo "  Cron eklendi: $CRON_CMD"

echo ""
echo "========================================"
echo " SSL Kurulum Tamamlandı!"
echo "========================================"
echo ""
echo "SONRAKI ADIMLAR:"
echo "  1. .htaccess dosyasındaki HTTPS yönlendirmesi aktif (zaten yapılandırıldı)"
echo "  2. APP_URL=https://$DOMAIN olduğundan emin olun (.env)"
echo "  3. Sertifika 90 günde bir otomatik yenilenir (cron aktif)"
echo "  4. Brower'da https://$DOMAIN adresini test edin"
