import os
import sys
import subprocess

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
REQ_FILE = os.path.join(BASE_DIR, "requirements.txt")

print("Python executable:", sys.executable)
print("Working dir:", BASE_DIR)
print("Requirements file:", REQ_FILE)

if not os.path.exists(REQ_FILE):
    raise FileNotFoundError(f"requirements.txt not found at {REQ_FILE}")

# Upgrade pip first (often required on shared hosts)
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"]) 

# Install dependencies from requirements.txt
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", REQ_FILE])

# Touch restart file for Passenger
TMP_DIR = os.path.join(BASE_DIR, "tmp")
os.makedirs(TMP_DIR, exist_ok=True)
with open(os.path.join(TMP_DIR, "restart.txt"), "a", encoding="utf-8"):
    pass

print("Dependencies installed and restart triggered.")
