#!/bin/bash
# wp.webciniz.com Otomatik Sunucu Kurulum Scripti (Ubuntu / Debian)

echo "=== Paketler Guncelleniyor ==="
sudo apt-get update
sudo apt-get install -y python3 python3-pip python3-venv wget curl nginx certbot python3-certbot-nginx

echo "=== Google Chrome Kuruluyor ==="
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/share/keyrings/google-chrome.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt-get update
sudo apt-get install -y google-chrome-stable

echo "=== Python Ortami ve Bagimliliklar Kuruluyor ==="
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

CURRENT_DIR=$(pwd)

echo "=== Systemd Servisi Olusturuluyor (7/24 Arka Planda Calisma) ==="
sudo bash -c "cat > /etc/systemd/system/wp_lead.service <<EOF
[Unit]
Description=WP Webciniz Lead Finder
After=network.target

[Service]
User=$USER
WorkingDirectory=$CURRENT_DIR
ExecStart=$CURRENT_DIR/venv/bin/streamlit run app.py --server.port=8501 --server.address=127.0.0.1 --server.headless=true --browser.gatherUsageStats=false
Restart=always

[Install]
WantedBy=multi-user.target
EOF"

sudo systemctl daemon-reload
sudo systemctl enable wp_lead
sudo systemctl restart wp_lead

echo "=== Nginx wp.webciniz.com Ayarlaniyor ==="
sudo cp nginx_wp_webciniz.conf /etc/nginx/sites-available/wp.webciniz.com
sudo ln -s /etc/nginx/sites-available/wp.webciniz.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

echo ""
echo "=================================================================="
echo "Kurulum tamamlandi! Servis 7/24 calisiyor."
echo "Ucretsiz SSL (HTTPS) kurmak icin su komutu calistirin:"
echo "sudo certbot --nginx -d wp.webciniz.com"
echo "=================================================================="
