#!/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

import time

from plano import *

amqp_servers = (
    "activemq",
    "artemis",
    "qdrouterd",
    "qpidd",
)

amqp_clients = (
    "qpid-jms",
    "qpid-messaging-cpp",
    "qpid-messaging-python",
    "qpid-proton-cpp",
    "qpid-proton-python",
    "rhea",
    "vertx-proton",
)

core_clients = (
    "activemq-artemis-jms",
)

core_servers = (
    "artemis",
)

openwire_clients = (
    "activemq-jms",
)

openwire_servers = (
    "activemq",
    "artemis",
)

def run_test(server, client, output_dir, args):
    if server == "activemq" and not is_dir(join(home_dir(), "activemq")):
        return

    if server == "artemis" and not is_dir(join(home_dir(), "artemis")):
        return

    notice("Testing server '{}' with client '{}'", server, client)

    test_dir = join(output_dir, "{}--{}".format(server, client))
    data_dir = join(test_dir, "data")
    output_file = join(test_dir, "output.txt")
    result_file = join(test_dir, "result.txt")
    command = "scripts/test-{} --impl {} {} --output {} &> {}"

    make_dir(test_dir)

    try:
        call(command, server, client, args, data_dir, output_file)
        write(result_file, "SUCCESS")
    except CalledProcessError as e:
        error(e)
        write(result_file, "FAILURE: {}".format(e))

def main():
    if "--impl" in ARGS:
        exit("The --impl argument is not allowed here")

    if "--output" in ARGS:
        exit("The --output argument is not allowed here")

    script = file_name(ARGS[0])
    timestamp = time.strftime('%Y-%m-%d', time.localtime())
    output_dir = "{}-{}".format(script, timestamp)

    remove(output_dir)
    make_dir(output_dir)

    args = " ".join(ARGS[1:])

    for server in amqp_servers:
        for client in amqp_clients:
            run_test(server, client, output_dir, args)

    for server in core_servers:
        for client in core_clients:
            run_test(server, client, output_dir, args)

    for server in openwire_servers:
        for client in openwire_clients:
            run_test(server, client, output_dir, args)

    print(output_dir)

    for name in (list_dir(output_dir)):
        print("  {}".format(name))

try:
    main()
except KeyboardInterrupt:
    pass
