#!/usr/bin/python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import with_statement

from plano import *

config_template = """
router {
    mode: standalone
    id: test-qdrouterd-1
}

listener {
    host: ::
    port: @port@
    linkCapacity: 1000
    authenticatePeer: off
    saslMechanisms: ANONYMOUS
}
"""

def main():
    port = random_port()

    config = config_template.replace("@port@", str(port))
    config_file = write_temp("qdrouterd-config", config)
    server = start_process_2("qdrouterd --config {}", config_file)

    command = "quiver //localhost:{}/q0 {}"
    args = " ".join(ARGS[1:])
    exit_code = 0

    try:
        call(command, port, args)
    except CalledProcessError as e:
        error(e)
        exit_code = e.returncode
    finally:
        stop_process(server)
        exit(exit_code)

try:
    main()
except KeyboardInterrupt:
    pass
