#!perl -nlw
# tail -c +0 -f noprof | # perl cabal-description2.pl | perl classify-ghc-errors-with-description2.pl | perl colorize-classified.pl
use FileHandle;
BEGIN{
    STDOUT->autoflush(1);
}
print if / ERROR /;
print if / startpoint=ghc/;
print if /make_snapshot/;
unless($j){
    $j="$1      INLINE $4" if /^(\S+:).*(?:hsc?|\.x|ghc_\d+\.s|hs-boot):\d+(-\d+)?:\d+(-\d+)?: error: (.*)/;
    $j="$1      INLINE $2" if /^(\S+:).*:\d+:\d+: fatal error: (.*)/;
    # assembler
    $j="$1      INLINE $2" if /^(\S+:).*:\d+:\d+: error:  Error: (.*)/;
    $j="$1    $2" if /^(\S+:).*(ghc:\s*panic.*)/;
    $j="$1    $2" if /^(\S+:).*[hc]:\d+:\d+:.*(error: .*)/;
    $j="$1    cannot create, directory nonexistent: $2" if /^(\S+:)\s+(.*cannot create.*Directory nonexistent.*)/;
    $j=$_ if /could not execute/ or /: The program .* is required but / or /exec: permission denied/ or /runInteractiveProcess: exec: / or /Simplifier ticks exhausted/ or /:  : copyFile: does not exist \(No such file or directory\)/ or /configure: error:/ or /No libraries, executables, tests, or benchmarks are enabled for package/ or /Failing due to -Werror/ or /<no location info>: error: / or /:  ghc: unrecognised flag/ or /No executables and no library found\. Nothing to do\./ or /The package has an impossible version range for a dependency/ or /File in tar archive is not in the expected directory/ or /ghc: on the commandline: malformed integer argument in -Odph/ or /installed package info from too old version of Cabal \(key field does not match id field\)/ or /on the commandline: error: \[-Werror\]/ or /ConnectionFailure Network/ or /cabal: can\x27t find source/ or /cabal: filepath wildcard .* does not match any/ or /No executables and no library found\. Nothing to do\./ or /ld\.gold: error: / or /depends on.*which failed/ or /error: undefined reference to / or /Permission denied/ or /ghc: no input files/ or /awk: cannot open / or /python2: .* \[Errno \d/ or /cannot satisfy -package/ or /cabal: Multiple cabal files found/ or /resource busy / or /Is it installed\?/ or / is neither on / or m,can't load \.so/\.DLL for, or /depends on.*which failed/;
    if($n) {
	  unless(/^\S+:\s+warning:/ or /^\S+:\s*$/) {
	      $j=$_;
	  }  # weird C warnings in HsSyck
	  $n=0;
    }
    if(/((hs(c|-boot)?)|(\.c(c|mm|pp)?)|\.h|Lexer\.x|\.m|enumfun\.inc|ghc_\d+\.hspp):\d+(-\d+)?:\d+(-\d+)?: error:$/ or /:\d+: \(column \d+\) \[ERROR\]  >>>/ or /^[0-9.]+:\s+Error:/){
        # get next line
	$n=1
    }
    # this one trumps
    $j="$1    $2" if /^(\S+:)(.*Missing \(or bad\).*(librar|header file).*)/;
}else {
    if (/\(Use .* to permit this/ or /Enable .* to allow/ or /Use .* if you want to/ or /Use .* to accept this/ or /Try GeneralizedNewtypeDeriving for GHC/ or /You need .* to derive an instance for this class/ or /Use \S+ to allow /){
	s/^[0-9.]+:\s*//;
	$j.="  LANGUAGEPRAGMA $_";
        # Note this won't get UndecidableInstances because it is on the first line.
    }
}
if(/^[0-9.]+:      Description:/i){
      ($d=$_) =~ s/\s+/ /g;
}
if(/^[0-9.]+:      Synopsis:/i){
      ($s=$_) =~ s/\s+/ /g;
}
if(/ ROOT FAILURE /){
    #print"";
    print $s if$s;
    print $d if $d;
    if(defined($j)){
	print $j
    } else {
	print "UNCLASSIFIED"
    }
    print;
    undef $j;
    undef $s;
    undef $d;
}
if(/:  Completed\s+\S+-[0-9.]+$/ or /INFO previous dependency did not install/){  #was "Installed" in an older version of cabal
    #print;
    undef $j;
    undef $s;
    undef $d;
}
