import os
import subprocess

# مسیر پوشه پروژه
project_dir = "/home/followe1/public_html/tubeify.ir"
python_path = "/home/followe1/virtualenv/public_html/tubeify.ir/3.11/bin/python"
uvicorn_path = "/home/followe1/virtualenv/public_html/tubeify.ir/3.11/bin/uvicorn"

# ۱. چک کردن و روشن کردن ربات (bot.py)
result_bot = subprocess.run(["pgrep", "-f", "bot.py"], stdout=subprocess.PIPE)
if not result_bot.stdout:
    os.chdir(project_dir)
    subprocess.Popen(
        f"nohup {python_path} bot.py > bot_log.txt 2>&1 &",
        shell=True
    )

# ۲. چک کردن و روشن کردن وب‌سایت (main.py با uvicorn)
result_web = subprocess.run(["pgrep", "-f", "main:app"], stdout=subprocess.PIPE)
if not result_web.stdout:
    os.chdir(project_dir)
    subprocess.Popen(
        f"nohup {uvicorn_path} main:app --host 127.0.0.1 --port 8000 > web_log.txt 2>&1 &",
        shell=True
    )