#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Step 2: Restart Firebird service (FirebirdServerCGM)
Forces all connections to close before database restore.
"""

import subprocess
import time

SERVICE_NAME = "FirebirdServerCGM"

def run_cmd(cmd):
    """Run a command and return (success, output)."""
    try:
        result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
        return result.returncode == 0, result.stdout.strip() + result.stderr.strip()
    except Exception as e:
        return False, str(e)

print(f"🔧 Restarting service: {SERVICE_NAME}")

# --- Stop service
ok, out = run_cmd(f'net stop "{SERVICE_NAME}"')
if ok:
    print(f"🛑 Service {SERVICE_NAME} stopped.")
else:
    print(f"⚠️ Failed to stop service (it may already be stopped):\n{out}")

# --- Small delay
time.sleep(5)

# --- Start service again
ok, out = run_cmd(f'net start "{SERVICE_NAME}"')
if ok:
    print(f"▶️ Service {SERVICE_NAME} started.")
else:
    print(f"❌ Failed to start service:\n{out}")

print("✅ Firebird restart sequence complete.")
