import paramiko
import sys
import os

HOST = "135.125.102.180"
USER = "ubuntu"
PASS = "BotPascal2026!"
LOCAL_FILE = "public/index.php"
REMOTE_FILE = "/var/www/html/repondeur_mail_grok/public/index.php"

def main():
    try:
        print(f"Connecting to {HOST}...")
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, username=USER, password=PASS)
        
        # Using /tmp strategy for safety (permissions)
        tmp_remote = "/tmp/index.php"
        
        sftp = ssh.open_sftp()
        print(f"Uploading -> {tmp_remote}")
        sftp.put(os.path.abspath(LOCAL_FILE), tmp_remote)
        sftp.close()
        
        print("Moving to target...")
        ssh.exec_command(f"sudo mv {tmp_remote} {REMOTE_FILE}")
        ssh.exec_command(f"sudo chown www-data:www-data {REMOTE_FILE}")
        
        ssh.close()
        print("Update Complete.")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()
