#!D:/TOOLS/cygwin/usr/local/bin/ruby.exe -KE
# -*- Mode: Ruby; -*-
#!/usr/local/bin/ruby -Ke
# Time-stamp: <2001/06/09 (Sat) 02:04:31 m-hase@ceres.dti.ne.jp>

#  $diaryDIR = "/home/m-hase/diary"
$diaryDIR = "E:/home/diary"

class BuyList

  ###----------------------------------------
  def initialize(opts={})
    @DB  = {}

    @title   = "散財リストダイジェスト"
    @template= "<li> %book (%year/%month/%day)</li>"
    @regtag  = Regexp::new(/^SUB\s+.*(買い物|散財)/)
    @display = ["ALL"]
    @replace ={ "book"	   => "Book",
                "本"	   => "Book",
                "コミック" => "Comic",
                "その他"   => "ETC",
                "ゲーム"   => "Game",
                "SCD"	   => "CDS",
                "PC"	   => "Comp" }
    @sort    = "date"
    @sorttype= {"date"=>0,"year"=>1,"month"=>2,"day"=>3,"book"=>4,"isbn"=>5}

    @title   = opts["title"]   if opts.key?("title")
    @template= opts["template"]if opts.key?("template")
    @regtag  = Regexp::new(opts["regtag"]) if opts.key?("regtag")
    @display = opts["display"] if opts.key?("display")
    @replace = opts["replace"] if opts.key?("replace")
    @sort    = opts["sort"]    if opts.key?("sort")	
  end
  
  ###----------------------------------------
  def read(filename)
    return if FileTest.directory?( filename )
    if filename =~ /^(.*)\/d(\d{4})(\d{2})(\d{2})\.hnf$/ then
      dir,year,month,day = $1,$2,$3,$4
#       printf("dir=%s %04d/%02d/%02d\n", dir, year, month, day )
      status = FALSE
      append = FALSE
      ccat  = "Other"
      cbook = ""
      isbn  = ""
      File.open( filename ).each do |line|
	status = FALSE if (( /^(NEW|CAT|SUB)/ =~ line ) and ( status == TRUE ))
	status = TRUE if @regtag =~ line
	if status == TRUE then
	  if /^LI\s+\[(\w+)\]/ =~ line then
	    ccat, cbook  = $1, $'
	    ccat = @replace[ccat] if @replace.key?(ccat)
	    next if cbook =~ /^\s*$/
	    isbn =""
	    append = TRUE
	  end
	  if /^(ISBN|CD)\s+([\w\d\-]+)\s+(.+)/ =~ line then
	    cmd, isbn, cbook  = $1, $2, $3
	    append = TRUE
	  end
	  if append == TRUE then
	    date = year.to_i*10000 + month.to_i*100 + day.to_i
	    q = []
	    q = @DB[ccat] if @DB.key?(ccat)
	    q << [id, year, month, day, cbook, isbn]
	    @DB.store(ccat, q)
	    append = FALSE
	    ccat = "Other"
	  end
	end
      end 
    end
  end # read

  ###----------------------------------------
  def AsHTML(dcat=@display)
    print "Content-type: text/html\n\n"
    print "<html><head>\n"
    print "<meta http-equiv=\"content-type\" content=\"text/html; charset=EUC-JP\">"
    print "<title>",@title,"</title>\n"
    print "</head>\n"
    print "<body>\n<h1>",@title,"</H1>\n"
    print "<ul>\n"

    display=[]
    dcat.each do |cat|
      if cat =~ "ALL" then
	display = @DB.keys
      else         
        display.push(cat)
      end
    end

    display.sort.each do |cat|
      next unless @DB.key?(cat)
      q = @DB[cat]
      print "<li>#{cat}\n<ol>\n"
      stype = @sorttype[@sort]
      q.sort{|x,y| x[stype]<=>y[stype]}.each do |id, year, month, day, book, isbn|
#   	printf("<li> %s (%04d/%02d/%02d)</li>\n",book, year, month, day)
 	print Expand(@template, { "book"=> book, "isbn"=>isbn,
		       "year"=>year,"month"=>month,"day"=>day})
      end
      print "</ol>\n</li>\n"
    end
    print "</ul>\n</body></html>\n"
  end # AsHTML

  ### 
  def Expand(template=@template,params={})
    t = template.clone
    t.gsub!(/%%/, "\34");
    params.each do |k, v|
      reg = Regexp.new("%{?"+k+"}?")
      t.gsub!(reg, v)
    end
    t.gsub!(/\34/,"%");
    return t;
  end # Expand

end # BuyList




###########################################################################
# blist = BuyList::new()
blist = BuyList::new({ "title"   => "散財ダイジェストリスト",
		       "regtag"  => /^SUB\s+.*(買い物|散財)/,
#    		       "sort"	 => "date",
  		       "sort"	 => "book",
		       "template"=> "<li> (%year/%month/%day) %book</li>",
		       "display" => "ALL"})

Dir.foreach($diaryDIR) do |yearDir|
  next unless yearDir =~ /^(\d{4})$/
  dir = $diaryDIR + "/" + yearDir
  Dir.foreach(dir) do |file|
    next unless file =~ /^d(\d{4})(\d{2})(\d{2})\.hnf$/
    filename = dir + "/" + file
    blist.read(filename)
  end
end
blist.AsHTML()
# blist.AsHTML(["Comp","Book"])