#!/usr/bin/env ruby

def del_whitespace(error_id)
  error_id.gsub(/ /, '')
end

rpm_name = ''
in_remove = false
in_print_rpm_location = false
in_print_src_rpm_location = false
in_print_rpm_evr = false
in_print_rpm_license = false

while (line = STDIN.gets)
  case line.chomp!

  # Cannot download Packages/kernel-devel-4.19.90-2012.4.0.0053.oe1.aarch64.rpm: All mirrors were tried
  when /Cannot download Packages\/(.*):/
    puts "cannot_download.element: #{del_whitespace($1)}"
    puts "cannot_download_#{del_whitespace($1)}: 1"

  # - nothing provides perl(Globus::GRAM::Error) needed by globus-gram-job-manager-condor-3.0-9.noarch
  when /nothing provides (.*) needed by (.*)/
    puts "lack_of_dependency.element: #{del_whitespace($2)} #{del_whitespace($1)}"
    puts "#{del_whitespace($2)}_lack_of_dependency_#{del_whitespace($1)}: 1"

  # file /usr/share/man/man1/nm-online.1.gz from install of NetworkManager-1:1.18.8-2.aarch64 conflicts with file from package NetworkManager-help-1:1.26.2-4.oe1.noarch
  when /file (.*) from install of (.*) conflicts.*from package (.*)/
    puts "conflicting_rpm.element: #{del_whitespace($2)} #{del_whitespace($3)} #{del_whitespace($1)}"
    puts "#{del_whitespace($1)}_from_#{del_whitespace($2)}_conflicts_with_#{del_whitespace($3)}: 1"

  # file /usr/libexec/caribou conflicts between attempted installs of caribou-0.4.21-1.aarch64 and caribou0-0.4.21-12.1.aarch64
  when /file (.*) conflicts.*of (.*) and (.*)/
    puts "conflicting_rpm.element: #{del_whitespace($2)} #{del_whitespace($3)} #{del_whitespace($1)}"
    puts "#{del_whitespace($1)}_conflicts_between_#{del_whitespace($2)}_#{del_whitespace($3)}: 1"

  # package qt-devel-1:4.8.7-48.oe1.aarch64 requires libQtCore.so.4()(64bit), but none of the providers can be installed
  when /package (.*) requires (.*), but.*installed$/
    puts "lack_of_dependency.element: #{del_whitespace($1)} #{del_whitespace($2)}"
    puts "#{del_whitespace($1)}_lack_of_dependency_#{del_whitespace($2)}: 1"

  # cannot install both libselinux-2.5-15.aarch64 and libselinux-3.1-1.oe1.aarch64
  when /cannot install both (.*) and (.*)/
    puts "conflicting_rpm.element: #{del_whitespace($1)} #{del_whitespace($2)}"
    puts "#{del_whitespace($1)}_conflicts_with_#{del_whitespace($2)}: 1"

  # installed package openEuler-release-20.03LTS_SP1-38.oe1.aarch64 obsoletes yum-utils provided by yum-utils-1.1.31-54.noarch
  # package openEuler-release-20.03LTS_SP1-38.oe1.aarch64 obsoletes yum-utils provided by yum-utils-1.1.31-54.noarch
  when /package (.*) obsoletes (.*) provided by (.*)/
    puts "obsoletes.element: #{del_whitespace($1)} #{del_whitespace($3)} #{del_whitespace($2)}"
    puts "#{del_whitespace($1)}_obsoletes_#{del_whitespace($2)}_provided_by_#{del_whitespace($3)}: 1"

  # start test rpm, rpm name: $i, rpm full name: $rpm_full_name
  when /start test rpm, rpm name: (.*), rpm full name: (.*)/
    puts "#{del_whitespace($1)}_rpm_full_name.#{del_whitespace($2)}: 1"

  # rpm src name, rpm name: $i, rpm src name: $rpm_src_name
  when /rpm src name, rpm name: (.*), rpm src name: (.*)/
    puts "#{del_whitespace($1)}_rpm_src_name.#{del_whitespace($2)}: 1"

  # rpm install result, rpm name: docker-engine, state: pass
  when /rpm install result, rpm name: (.*), state: (.*)/
    puts "#{del_whitespace($1)}_install.#{del_whitespace($2)}: 1"

  # query group result, rpm name: docker-engine, group: Tools/Docker
  when /query group result, rpm name: (.*), group: (.*)/
    puts "#{del_whitespace($1)}_group_.#{del_whitespace($2)}: 1"

  # query libs result, rpm name: docker-engine, libs: true
  when /query libs result, rpm name: (.*), libs: (.*)/
    puts "#{del_whitespace($1)}_libs_.#{del_whitespace($2)}: 1"

  # run cmd result rpm name: docker-engine, cmd: /usr/bin/containerd, state: pass
  when /run cmd help result, rpm name: (.*), cmd: (.*), state: (.*)/
    puts "#{del_whitespace($1)}_cmd_#{del_whitespace($2)}.#{del_whitespace($3)}: 1"

  # run service action result, rpm name: docker-engine, service: docker.service, action: start, state: pass
  when /run service action result, rpm name: (.*), service: (.*), action: (.*), state: (.*)/
    puts "#{del_whitespace($1)}_service_#{del_whitespace($2)}_#{del_whitespace($3)}.#{del_whitespace($4)}: 1"

  # rpm uninstall result, rpm name: docker-engine, state: pass
  when /rpm uninstall result, rpm name: (.*), state: (.*)/
    puts "#{del_whitespace($1)}_uninstall.#{del_whitespace($2)}: 1"

  when /^start remove test: (.*)$/
    in_remove = true
    rpm_name = $1
    next
  when "remove test finished: #{rpm_name}"
    in_remove = false
    rpm_name = ''

  when /^Starting print rpm: ([^\s]*) location$/
    in_print_rpm_location = true
    rpm_name = $1
    next
  when "print rpm: #{rpm_name} location end"
    in_print_rpm_location = false
    rpm_name = ''

  when /^Starting print rpm: ([^\s]*) evr$/
    in_print_rpm_evr = true
    rpm_name = $1
    next
  when "print rpm: #{rpm_name} evr end"
    in_print_rpm_evr = false
    rpm_name = ''

  when /^Starting print rpm: (.*)'s src.rpm location$/
    in_print_src_rpm_location = true
    rpm_name = $1
    next
  when "print rpm: #{rpm_name}'s src.rpm location end"
    in_print_src_rpm_location = false
    rpm_name = ''

  when /^Starting print rpm: (.*) license$/
    in_print_rpm_license = true
    rpm_name = $1
    next

  when "print rpm: #{rpm_name} license end"
    in_print_rpm_license = false
    rpm_name = ''
  end

  if in_remove
    # Problem: The operation would result in removing the following protected packages: dnf, systemd
    next unless line =~ /protected packages: (.*)/

    $1.split(', ').each do |rpm|
      puts "protected_packages.element: #{del_whitespace(rpm_name)} #{del_whitespace(rpm)}"
      puts "remove_#{del_whitespace(rpm_name)}_but_protected_packages_#{del_whitespace(rpm)}: 1"
    end
  end

  if in_print_rpm_location
    puts "#{rpm_name}_location.element: #{line}"
  end

  if in_print_src_rpm_location
    puts "#{rpm_name}_src_rpm_location.element: #{line}"
  end

  if in_print_rpm_evr
    puts "#{rpm_name}_evr.element: #{line}"
  end

  if in_print_rpm_license
    puts "#{rpm_name}_license.element: #{line}"
  end

end
