#!/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 fatal_error(msg)
  p "Fatal Error: "+msg
  exit
end

def parse_date(line)
  if /\A(\d\d(\d\d)-(\d\d)-(\d\d)T(\d\d)):\d\d:\d\d\Z/ =~ line 
    timestr =  $2 + $3 + $4 + $5
    return timestr
  end
  fatal_error("what?"+line)
end

def parse_group(path)
  if /\A\/([^\/]+)\/(.*)\z/ =~ path
    group = $1
    page = $2
    page = "FrontPage.html" if page == ""

    if /\A\./ =~ group
      group = "www"
      page = $2
    end

    return group, page
  end
    
  if /\A\/(.*)\z/ =~ path
    group = "www"
    page = $1
    page = "FrontPage.html" if page == ""

    return group, page
  end

  fatal_error("what?"+path)
end

def parse(line)
  line.gsub!(/, /, ",")
  date, host, user, method, path, status, byte = line.split
  timestr = parse_date(date)

  method = method.sub(/\A"/, "")
  path = path.sub(/"$/, "")
  group, page = parse_group(path)

  return date, user, method, group, page
end

def main
  open("qwik1.log", "wb") {|out|
    open("qwik0.log") {|f|
      while line = f.gets

	# remove public access
	next if / - "/ =~ line

	line = line.gsub(/, /, ",") # for unknown host

	date, user, method, group, page = parse(line)

	next if group == "www"
	next if group == "ring.sfc.keio.ac.jp" # remove ring related

	if group.include?(".") # error
	  #qp group
	  next
	end

	# care about session id access. (very few)
	if /\A(.+)\?sid=\d+\z/ =~ page
	  page = $1
	end

	# remove html
	if /\.html\z/ =~ page
	  #qp method
	  if method == "POST"
	    #qp line
	    page = page.sub(/\.html\z/, ".save")
	  else
	    #	  next
	  end
	end

	# remove login related
	next if /\?user\=/ =~ page && /\&pass\=/ =~ page
	next if page == ".basicauth"
	next if page == ".login"
	next if /\A\.logout/ =~ page
	next if /\A\.getpass/ =~ page
	next if /\ALogOut\.html/ =~ page

	# remove command related
	next if /\A\.proxy/ =~ page
	next if /\ASiteSearch\.html/ =~ page
	next if /\AMemberAdd\.html/ =~ page
	next if /\ANewPage\.html\?t=/ =~ page
	next if /\.new\z/ =~ page
	next if /\.zip\z/ =~ page

	# edit related
	next if /\.edit\z/ =~ page
	next if /\.html\?c=edit\z/ =~ page

	# remove theme related
	next if /\A\.attach/ =~ page
	next if /\.backup/ =~ page
	next if /\.rss\z/ =~ page
	next if /\.attach\z/ =~ page
	next if page == "favicon.ico"
	next if /\.gif\z/ =~ page
	next if /\.jpg\z/ =~ page
	next if /\.css\z/ =~ page

	/\A([^\.]+)\..+\z/ =~ page
	basename = $1
	if basename == ""
	  fatal_error("what?"+line)
	end

	cmd = ""
	cmd = "html" if /\.html\z/ =~ page
	cmd = "save" if /\.save\z/ =~ page
	cmd = "wema" if /\.wema\z/ =~ page
	cmd = "hcomment" if /\.hcomment\z/ =~ page
	cmd = "comment" if /\.html\?message=/ =~ page
	fatal_error("what?"+line) if cmd == ""
	out.print "#{date} #{user} #{group} #{basename} #{cmd}\n"

=begin
	   # remove all, for test
	   next if /\.save\z/ =~ page
	   next if /\.wema\z/ =~ page
	   next if /\.hcomment\z/ =~ page
	   next if /\.html\?message=/ =~ page
	   out.print "#{date} #{user} #{method} #{group} #{page}\n"
=end

      end
    }
  }
end
main
