import paramiko
import sys

HOST = "192.168.1.76"
USER = "root"
PASSWORD = "7309208104"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(HOST, username=USER, password=PASSWORD, look_for_keys=False, allow_agent=False)
print(f"Připojeno na {HOST}")

def run(cmd, show=True):
    stdin, stdout, stderr = client.exec_command(cmd, get_pty=False)
    out = stdout.read().decode().strip()
    err = stderr.read().decode().strip()
    if show:
        if out:
            print(out)
        if err:
            print(f"[stderr] {err}", file=sys.stderr)
    return out, err

# 1. Ověř Python
print("\n--- Python ---")
run("python3 --version")
run("pip3 --version || pip --version")

# 2. Co už je nainstalováno
print("\n--- Nainstalované balíčky (blake3, psycopg2) ---")
out, _ = run("pip3 list 2>/dev/null || pip list 2>/dev/null")
for line in out.splitlines():
    if any(pkg in line.lower() for pkg in ("blake3", "psycopg2")):
        print(f"  FOUND: {line}")

# 3. Instalace chybějících
print("\n--- Instalace blake3 ---")
run("pip3 install blake3 || pip install blake3")

print("\n--- Instalace psycopg2-binary ---")
run("pip3 install psycopg2-binary || pip install psycopg2-binary")

# 4. Ověření
print("\n--- Ověření importů ---")
run("python3 -c \"import blake3; print('blake3 OK:', blake3.__version__)\"")
run("python3 -c \"import psycopg2; print('psycopg2 OK:', psycopg2.__version__)\"")

client.close()
print("\nHotovo.")
