#!/usr/bin/env python2.7
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import sys
from subprocess import call

BOLD = '\033[1m'
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[33m'
RESET = '\033[0m'

TARGETS = [
    'itest_trusty',
    'itest_xenial',
    'itest_jessie',
    'itest_stretch',
    'itest_tox',
]


def log(line, color=''):
    print(BOLD + color + line + RESET)


if __name__ == '__main__':
    num_workers = int(os.environ['CIRCLE_NODE_TOTAL'])
    node_index = int(os.environ['CIRCLE_NODE_INDEX'])
    assert node_index < num_workers

    my_targets = TARGETS[node_index::num_workers]
    status = 0

    for target in my_targets:
        log('Executing target {}...'.format(target), color=YELLOW)
        target_status = call(('make', target))

        if target_status == 0:
            log(
                'Target {} completed successfully'.format(target),
                color=GREEN,
            )
        else:
            status = target_status
            log(
                'Error! Target {} failed with exit status {}'.format(
                    target,
                    target_status,
                ),
                color=RED,
            )

    sys.exit(status)
