#!/usr/bin/env python

import fileinput
import os.path
import re
import sys

USAGE = 'USAGE: rosgcov_summarize <package_dir> <rosgcov_file>'

if len(sys.argv) != 3:
    print(USAGE)
    sys.exit(-1)

pkg = sys.argv[1]
fname = sys.argv[2]

if not os.path.exists(fname):
    print('[rosgcov] %s : %.2f%% (no coverage results)' % (os.path.split(pkg)[1], 0.0))
    sys.exit(0)

re_hit = re.compile('^ *[0-9]*:.*')
re_miss = re.compile('^ *#####:.*')

re_branch_hit = re.compile('^branch *[0-9] *taken [0-9]*.*')
re_branch_miss = re.compile('^branch *[0-9] *never executed.*')

files = []
finput = fileinput.input(fname)
for l in finput:
    ls = l.strip().split(' ')
    f = os.path.join(ls[0], os.path.split(ls[1])[1])
    files.append(f.strip())

total = 0
hits = 0
misses = 0
branch_total = 0
branch_hits = 0
branch_misses = 0
print('-------------------------------------------------------')
print('Coverage summary: ')
print('-------------------------------------------------------')
for f in files:
    prefix = os.path.commonprefix([pkg, f])
    display_name = f[len(prefix):]
    if display_name[0] == '/':
        display_name = display_name[1:]
    print('  ' + display_name + ': ')
    gcov_fname = f + '.gcov'
    if not os.path.exists(gcov_fname):
        print('WARNING: no coverage results for %s' % (display_name))
        continue
    gcovf = fileinput.input(gcov_fname)
    local_total = 0
    local_hits = 0
    local_misses = 0
    local_branch_total = 0
    local_branch_hits = 0
    local_branch_misses = 0
    for s in gcovf:
        if re_hit.match(s):
            local_hits += 1
            local_total += 1
        elif re_miss.match(s):
            local_misses += 1
            local_total += 1
        if re_branch_hit.match(s):
            local_branch_hits += 1
        elif re_branch_miss.match(s):
            local_branch_misses += 1
            local_branch_total += 1

    print('    line:   %.2f%% (%d / %d)' % ((100.0 * local_hits / max(local_total, 1)), local_hits, local_total))
    hits += local_hits
    misses += local_misses
    total += local_total

    print('    branch: %.2f%% (%d / %d)' % ((100.0 * local_branch_hits / max(local_branch_total, 1)), local_branch_hits, local_branch_total))
    branch_hits += local_branch_hits
    branch_misses += local_branch_misses
    branch_total += local_branch_total

print('-------------------------------------------------------')
print('[rosgcov] %s : %.2f%% (%d / %d)' % (os.path.split(pkg)[1], (100.0 * hits / max(total, 1)), hits, total))
print('[rosgcov] %s : branch %.2f%% (%d / %d)' % (os.path.split(pkg)[1], (100.0 * branch_hits / max(branch_total, 1)), branch_hits, branch_total))
print('-------------------------------------------------------')
