#!/usr/bin/python3
import sys, re

black = '\033[0;30m' 
red = '\033[0;91m' 
red2 = '\033[0;31m' 
green = '\033[1;32m' 
yellow = '\033[1;33m' 
brown = '\033[0;33m'
blue = '\033[0;34m' 
light_blue = '\033[1;34m'
magenta = '\033[1;35m'
cyan = '\033[0;36m'
white = '\033[0;37m' 
purple = '\033[0;35m'
default = '\033[0m'

lines = {
    '==>' : green,
    'notamock:' : brown,
    'using insecure memory!' : brown,
    'no files found' : brown,
    'OK' : brown,
    'yes' : brown,
    'done' : brown,
    'RPM build errors:' : red,
    'errors' : red,
    'warnings' : yellow,
    'error:' : red,
    'error 1' : red,
    'error 127': red,
    'Error:' : red,
    'ERROR' : red,
    'Failed:' : red,
    'FAILED' : red,
    'failed:' : red,
    'File not found:' : red,
    'No such file or directory' : red,
    'command not found' : red,
    'nothing provides' : red,
    'warning:' : yellow, 
    'Warning:' : yellow, 
    'is needed by' : yellow,
    'Provides:' : yellow,
    'Requires:' : yellow,
    'Requires(post):' : yellow,
    'Requires(pre):' : yellow,
    'Obsoletes:' : yellow,
    'Macro expanded in comment on line' : yellow,
    'Requires(rpmlib):' : yellow,
    'OrderWith' : yellow,
    'Processing files' : yellow,
    'Checking for unpackaged file(s)' : yellow,
    'Wrote:' : green,
    'exit 0' : green,
    'Executing(%clean)' : magenta,
    'Executing(%build)' : magenta,
    'Executing(%prep)' : magenta,
    'Executing(%install)' : magenta,
    'Executing(%check)' : magenta,
    'Executing(%doc):' : magenta,
    'Executing(%license):' : magenta,
    'Executing(%generate_buildrequires):' : magenta,
    'Installing:' : light_blue,
    'Installing dependencies:' : light_blue,
    'Downloading Packages:' : light_blue,
    'Installed:' : light_blue,
    'Installed (but unpackaged) file(s) found:' : red,
    'returned non-zero exit status' : red,
    'Failed build dependencies' : red,
    'returned non-zero exit status' : red,
    'Failed build dependencies' : red,
    'Segmentation fault' : red,
    'A fatal error has been detected by the Java Runtime Environment' : red,
    'has incomplete type' : red,
    'conflicting types for' : red,
    'No rule to make target' : red,
    'failed to allocate' : red, 
    'Cannot allocate memory' : red,
    'cp: cannot stat' : red,
    "can't find file to patch at input line" : red,
    'No matching package to install' : red,
    'No match for argument:' : red, 
    'but none of the providers can be installed' : red,
    'unable to execute command:' : red,
    'is a protected member of' : red,
    'undefined reference to' : red,
    'no matching function for call to' : red,
    'Could not find a configuration file for package' : red,
    'variable has incomplete type' : red,
    'File must begin with' : red, 
    'Bad source' : red,
    'File not found' : red,
    'implicit declaration of function' : red,
    'file not found' : red, 
    'use of undeclared identifier' : red,
    'function cannot return function type' : red,
    'unknown type name' : red, 
    'incomplete definition of type' : red,
    'Man pages cannot be built' : red,
    'format string is not a string literal' : red,
    'Failed to find required' : red,
    'not found' : red,
    'CMake Error at' : red,
    "Couldn't find include" : red,
    'Installed (but unpackaged) file(s) found:' : red,
    'STAGE' : cyan 
}
try:
  with open('/usr/bin/notamock', 'r') as nm:
    for a in nm.readlines():
      if not 'echo_exit' in a:
        continue
      match = re.search('(^.*echo_exit\ )(".*")(.*)',  a)
      templ = str(match[2]).replace('$1', '').strip('"\' ')
      lines[templ] = red2
except:
  pass
   
PACKAGES = []

for line in sys.stdin:
  if line.strip():
    if line.split()[0] == 'Wrote:' or line.split()[0] == 'Записан:':
      PACKAGES.append(line.split()[1])
  for word, color in lines.items():
    line = line.replace(word, color + word + default)
  print(line, end=''  )

if PACKAGES:
  print(magenta + 'Packages:' + default)
  for a in PACKAGES:
    if not '.nosrc.rpm' in a: 
      print(a)
