import sys
Param = sys.argv[1:]

if not Param:
    from hooks import webservice

    if webservice.start():
        import hooks.monitor
        hooks.monitor.StartUp()

elif len(Param) == 1 or Param[1]:
    import socket
    Argv = ';'.join(["service"] + Param)
    DataSend, XbmcMonitor, sock = f"EVENT {Argv}".encode('utf-8'), None, socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    sock.settimeout(None)

    for _ in range(60):  # 60 seconds timeout
        try:
            sock.connect(('127.0.0.1', 57342))
            sock.send(DataSend)
            sock.recv(1024)
            sock.close()
            break
        except:
            if not XbmcMonitor:
                import xbmc
                XbmcMonitor = xbmc.Monitor()

            if XbmcMonitor.waitForAbort(0.1):
                break
