import Foundation import UltraMeshCore do { signal(SIGPIPE, SIG_IGN) let arguments = Array(CommandLine.arguments.dropFirst()) var stateURL = FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent("Library/Application Support/UltraMesh") var socketPath: String? var index = 0 while index < arguments.count { guard index + 1 < arguments.count else { throw UMNError.message("missing value for \(arguments[index])") } if arguments[index] == "--state-dir" { stateURL = URL(fileURLWithPath: arguments[index + 1]) } else if arguments[index] == "--socket" { socketPath = arguments[index + 1] } else { throw UMNError.message("unknown option \(arguments[index])") } index += 2 } let daemon = try MeshDaemon(baseURL: stateURL, socketPath: socketPath) try daemon.start() signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN) let source = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main) let term = DispatchSource.makeSignalSource(signal: SIGTERM, queue: .main) source.setEventHandler { daemon.stop(); exit(0) } term.setEventHandler { daemon.stop(); exit(0) } source.resume(); term.resume() dispatchMain() } catch { FileHandle.standardError.write(Data("umnd: \(error.localizedDescription)\n".utf8)) exit(1) }