#!/usr/bin/env python3
"""
Synex ServerHub - Main executable
"""

import sys
import os
from pathlib import Path

# Support both installed and development environments
if os.path.exists('/usr/share/serverhub/core'):
    core_path = '/usr/share/serverhub/core'
else:
    script_dir = Path(__file__).parent.resolve()
    core_path = str(script_dir.parent / 'share' / 'serverhub' / 'core')

sys.path.insert(0, core_path)

from serverhub import ServerHub
from lib.cli import CLIHandler

def main():
    """Entry point"""
    try:
        cli = CLIHandler()
        args = cli.parse_args()

        if args is None:
            # No arguments provided, launch interactive TUI
            app = ServerHub(cli_mode=False)
            app.run()
        else:
            # Execute command from CLI
            app = ServerHub(cli_mode=True)
            exit_code = cli.execute(args, app)
            sys.exit(exit_code)

    except KeyboardInterrupt:
        print("\n\nOperation cancelled by user.")
        sys.exit(130)
    except SystemExit:
        raise
    except Exception as e:
        print(f"\nFatal error: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()
