#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

base = File.dirname(__FILE__)+"/.." # /usr/local/qwik/bin -> /usr/local/qwik
$LOAD_PATH.unshift(File.expand_path(base+"/lib"))
$LOAD_PATH.unshift(File.expand_path(base+"/compat"))

require "qwik/qp"

def get_group(line)
  if /\A.+\[(.+)\]: .+$/ =~ line
    return $1
  end
  return nil
end

def fatal_error(msg)
  p "Fatal Error: "+msg
  exit
end

open("quickml1.log", "wb") {|out|
  open("quickml0.log") {|f|

    bad_log_group = nil

    while line = f.gets

      if bad_log_group
	group = get_group(line)
	#if group == bad_log_group
	if /: Inactive$/ =~ line ||
	    /: ML Closed$/ =~ line
	  #p "this is bad "+line
	  next
	end
	bad_log_gruop = nil
      end

      if /New ML by $/ =~ line
	bad_log_group = get_group(line)
	fatal_error(line) if bad_log_group.nil?
	next
      end
      bad_log_group = nil

      next if /: Command: / =~ line
      next if /: MAIL FROM:/ =~ line
      next if /: RCPT TO:/ =~ line
      next if /: From:/ =~ line
      next if /: Cc: / =~ line
      next if /: bare From: / =~ line
      next if /: bare Cc: / =~ line

      next if /: Sweeper started$/ =~ line
      next if /: Sweeper runs$/ =~ line
      next if /: Sweeper finished$/ =~ line
      next if /: Sweeper shutdown$/ =~ line

      next if /: Server started at / =~ line
      next if /: Server exited / =~ line

      next if /: Closed: localhost$/ =~ line
      next if /: Connect: localhost$/ =~ line
      next if /: Session finished: / =~ line
      next if /: Connect: / =~ line
      next if /: Closed: / =~ line
      next if /: Unacceptable RCPT TO:/ =~ line
      next if /: Excluded: / =~ line

      next if %r+: /qwik/lib/qwik/+ =~ line
      next if %r+: /usr/local/qwik+ =~ line
      next if %r+: /usr/local/qwik+ =~ line

      next if /: Invalid From Address: / =~ line
      next if /: Unknown Sweep Error: / =~ line
      next if /: Unknown Session Error: / =~ line

      next if /: ErrorMail: / =~ line
      next if /: AddError: / =~ line
      next if /: Send confirmation: / =~ line
      next if /: Forward Address: / =~ line
      next if /\A	/ =~ line
      next if /: rejected$/ =~ line

      out.print
    end
  }
}
