#!@PYTHONEXE@

import os
import sys
import shutil
import re
import subprocess
import time

# FIXME: There's too much repetition, move generally used parts into reusable modules.
if os.name == "nt":
    tmp = os.getenv("TEMP")
else:
    tmp = "/tmp"

if os.name == 'nt':
    st = 'gnunet-statistics.exe'
    arm = './gnunet-arm.exe'
else:
    st = 'gnunet-statistics'
    arm = './gnunet-arm'

run_arm = [arm, '-c', 'test_arm_api_data.conf', '--no-stdout', '--no-stderr']
debug = os.getenv('DEBUG')
if debug:
    run_arm += [debug.split(' ')]


def cleanup():
    shutil.rmtree(os.path.join(tmp, "test-gnunetd-arm"), True)


def sub_run(args, want_stdo=True, want_stde=False, nofail=False):
    if want_stdo:
        stdo = subprocess.PIPE
    else:
        stdo = None
    if want_stde:
        stde = subprocess.PIPE
    else:
        stde = None
    p = subprocess.Popen(args, stdout=stdo, stderr=stde)
    stdo, stde = p.communicate()
    if not nofail:
        if p.returncode != 0:
            sys.exit(p.returncode)
    return (p.returncode, stdo, stde)


def fail(result):
    print(result)
    r_arm(['-e'], want_stdo=False)
    sys.exit(1)


def end_arm_failure(command, rc, stdo, stde, normal):
    if normal:
        if rc != 0:
            fail(
                "FAIL: error running {}\nCommand output was:\n{}\n{}".format(
                    command, stdo, stde
                )
            )
    else:
        if rc == 0:
            fail(
                "FAIL: expected error while running {}\nCommand output was:\n{}\n{}"
                .format(command, stdo, stde)
            )


def print_only_failure(command, rc, stdo, stde, normal):
    if normal:
        if rc != 0:
            print(
                "FAIL: error running {}\nCommand output was:\n{}\n{}".format(
                    command, stdo, stde
                )
            )
            sys.exit(1)
    else:
        if rc == 0:
            print(
                "FAIL: expected error while running {}\nCommand output was:\n{}\n{}"
                .format(command, stdo, stde)
            )
            sys.exit(1)


def r_something(to_run, extra_args, failure=None, normal=True, **kw):
    rc, stdo, stde = sub_run(
        to_run + extra_args, nofail=True, want_stde=True, **kw
    )
    if failure is not None:
        failure(to_run + extra_args, rc, stdo, stde, normal)
    return (rc, stdo, stde)


def r_arm(extra_args, **kw):
    return r_something(run_arm, extra_args, **kw)


cleanup()

print("TEST: Bad argument checking...", end='')
r_arm(['-x'], normal=False, failure=print_only_failure)
print("PASS")

print("TEST: Start ARM...", end='')
r_arm(['-s'], failure=print_only_failure)
time.sleep(1)
print("PASS")

print("TEST: Start another service...", end='')
r_arm(['-i', 'resolver'], failure=end_arm_failure)
time.sleep(1)
print("PASS")

print("TEST: Stop a service...", end='')
r_arm(['-k', 'resolver'], failure=end_arm_failure)
time.sleep(1)
print("PASS")

print("TEST: Stop ARM...", end='')
r_arm(['-e'], failure=print_only_failure)
time.sleep(1)
print("PASS")

cleanup()
