#!/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 "time"
require "qwik/qp"

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

def nuparse_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 nuparse(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

# copy from time.rb
MonthValue = {
  'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
  'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
}

def parse(line)
  line.chomp!

  #localhost - - [02/Oct/2004:09:28:54 JST] "GET /pb/9.html HTTP/1.1" 200 7573 "-" "Wget/1.9.1"
  d, d, d, date, d, method, path, d, d, d, d, d = line.split

  date = date.sub(/\A\[/, "")
  if /\A((\d\d)\/(...)\/\d\d(\d\d):(\d\d):\d\d:\d\d)\Z/ =~ date
    mon = sprintf("%02d", MonthValue[$3.upcase])
    timestr =  $4 + mon + $2 + $5
  end

  user = "a@e.jp"
  method = method.sub(/\A"/, "")
  return timestr, user, method, path
end

def main
  open("access1.log", "wb") {|out|
    open("access0.log") {|f|
      while line = f.gets
	timestr, user, method, path = parse(line)

	# reject virus
	next if /\.\./ =~ path
	next if /\A\/scripts/ =~ path
	next if /\A\/c\/winnt/ =~ path
	next if /\A\/d\/winnt/ =~ path
	next if /\A\/tld\/pxy/ =~ path
	next if /\A\/happy\/misc/ =~ path
	next if /\A\/cgi-bin/ =~ path
	next if /\A\/psr\// =~ path
	next if /\A\/MSADC/ =~ path
	next if /\A\/_vti_bin/ =~ path
	next if /\A\/MSOffice/ =~ path
	next if /robots\.txt/ =~ path

	group, page = parse_group(path)

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

#	qp timestr, user, method, group, page
	#exit

	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 /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 /\A_new\.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 /\.rss\// =~ page
	next if /\.attach\z/ =~ page
	next if page == "favicon.ico"
	next if /\.gif\z/ =~ page
	next if /\.jpg\z/i =~ page
	next if /\.css\z/ =~ page
	next if /\.png\z/ =~ page
	next if /\.bmp\z/ =~ page
	next if /\.rdf\z/ =~ page
	next if /index.cgi/ =~ page

	# remove old style
	next if /\.html\?c=newentry/ =~ page
	next if /\?cmd=delete/ =~ page
	next if /\?c=\z/ =~ page
	next if /\.htm\z/ =~ page
	next if /\.tml\z/ =~ page
	next if /\.htm\// =~ page
	next if /\.html\// =~ page
	next if /\.htmlML/ =~ page
	next if /qwik\.jp\z/ =~ page

	/\A([^\.]+)\..+\z/ =~ page
	basename = $1
	if basename.nil? || basename == ""
	  basename = page
	  page = basename+".html"
#	  qp page
	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
	if cmd == ""
	  qp "what?"+line
	else
	  out.print "#{timestr} #{user} #{group} #{basename} #{cmd}\n"
	end

=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
