#!/usr/bin/env python3
"""Prepare local mods, rsync to server, generate config, and commit."""

import os
import subprocess
import sys

BASE = os.path.dirname(os.path.abspath(__file__))
PREPARE_LOCAL = os.path.join(BASE, "prepare_local")
AUTOGEN = os.path.join(BASE, "scripts", "autogen_config.py")
LOCALMODS_DIR = os.path.join(BASE, "LocalMods")

CONFIG_PATH = os.path.join(BASE, "config_player.xml")
RSYNC_SSH = "ssh -p 22"


def log_ok(msg):
    print(f"\033[92m✓\033[0m {msg}")

def log_info(msg):
    print(f"• {msg}")

def log_err(msg):
    print(f"\033[91m✗\033[0m {msg}", file=sys.stderr)


def main():
    msg = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else "Update LocalMods"

    print()
    print("╔══════════════════════════════════════════╗")
    print("║  Prep: mods → config → rsync → commit    ║")
    print("╚══════════════════════════════════════════╝")
    print()

    log_info("Step 1/5: Copy mods from workshop...")
    subprocess.run([sys.executable, PREPARE_LOCAL], check=True)
    log_ok("LocalMods ready")
    print()

    log_info("Step 2/5: Generating config_player.xml...")
    subprocess.run([sys.executable, AUTOGEN], check=True)
    log_ok("config_player.xml generated")
    print()

    log_info("Step 3/4: Rsyncing LocalMods + config to server...")
    subprocess.run([
        "rsync", "-avz",
        "-e", RSYNC_SSH,
        LOCALMODS_DIR + "/", CONFIG_PATH,
        "baro@barotrauma:/opt/barotrauma/"
    ], check=True)
    log_ok("Synced to server")
    print()

    log_ok("Done. Push manually when ready.")


if __name__ == "__main__":
    main()
