#!/usr/bin/perl

# Copyright (C) 2013 Red Hat, Inc.
#
# This file is part of csmock.
#
# csmock is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# csmock is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with csmock.  If not, see <http://www.gnu.org/licenses/>.

use warnings;
use strict;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $opt_s;
getopts('s:')
    or die "$0: invalid options";

sub fatal {
    my ($msg) = @_;
    print STDERR "$0: $msg\n";
    exit(1);
}

if ($#ARGV != 0) {
    print STDERR "Usage: $0 [-s SRC_PATH_PREFIX] COV_DIR\n";
    exit(1);
}

my $dir = $ARGV[0];
my $srcdir = $opt_s || '/builddir/build/BUILD';
$srcdir .= '/'
    unless $srcdir =~ m,/$,;

fatal("directory not found: $dir")
    unless -d $dir;
fatal("directory not found: $dir/c/emit")
    unless -d "$dir/c/emit" or -d "$dir/emit";
print STDERR "warning: directory not found: $srcdir\n"
    unless -d $srcdir;

open(INPUT, '-|', 'cov-format-errors', '--emacs-style', '--dir', "$dir")
    or die;

while (<INPUT>) {
    s/([0-9>]):\n/$1: /;
    s/^Error: /\nError: /;
    s/^path://;
    s/^\Q$srcdir\E//;
    print $_;
}

close(INPUT)
    or die;
