#!/usr/bin/ruby 


=begin
  $Id: sakura.cgi,v 1.2 2003/11/25 10:22:17 moleskin Exp $


  Copyright (c) 2003  moleskin <moleskin@as.email.ne.jp>. 
  All rights reserved.
  
    Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
     derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=end

require "find"
require "cgi"


CONTENT_TEMPLATE= <<EOF
<h3>$title</h3>
<p><b>$date</b></p>
<p>category:<a href="?category=$category" >$category</a></p>
$body
<hr />
EOF

HEADER_TEMPLATE= <<EOF
<html>
<head>
<title>$blog_title</title>
</head>
<body><h1>$blog_title</h1>
<p><a href="http://sourceforge.jp/"><img src="http://sourceforge.jp/sflogo.php?group_id=867" width="96" height="31" border="0" alt="SourceForge.jp"></a></p>
<hr />
EOF


FOOTER_TEMPLATE= <<EOF
<p>mail:$mailaddr</p>
</body></html>
EOF

CONTENT_TYPE = "Content-Type: text/html; charset=$encoding \n\n"



class Template
  attr_accessor :value
  def initialize
    @value = Hash.new("")
    @template_string = Hash.new("")
    @buffer = STDOUT
  end
  def replace(string)
    return string.gsub(/\$[a-z_]+/){
      |m | @value[m]
    }
  end
  def add(name,value)
    @template_string[name] = value
  end
  def exec(name)
    if @template_string.key? name then
      replace(@template_string[name])
    end
  end
end


class Sakura 
  attr_accessor :datadir ,:content_pattern,:encoding
  attr_accessor :category,:today
  attr_accessor :template,:cgi
  def initialize(&block)
    @category = ""
    instance_eval &block
  end
  def get_content(dir,pattern)
    begin
      list = []
      Find.find(dir) do |path|
        Find.prune if [".",".."].include? path
        if File.basename(path) =~ pattern then
          list << path
        end
      end
      return list
    resque
      print "get_content error\n"
    end
  end
  DATE = 0
  PATH = 1
  def get_contentlist(el)
    begin
      list = []
      el.each do |f |
        if FileTest.exist?(f) then
          list << [File.mtime(f),f]
        end
      end
      return list
    resque
      print "get_content list error\n"
    end
  end
  def make_content(one_entry)
    title = ""
    body = ""
    File.open(one_entry[PATH]) do |f|
      if line = f.gets then
        title << line.chomp
      end
      while line = f.gets 
        body << line
      end
    end
    template.value["$title"] = title
    template.value["$date"] = one_entry[DATE]
    template.value["$body"] = body
    template.value["$category"] = File.dirname(one_entry[PATH])[@datadir.length,999]
    return template.exec("content")
  end
  def makemain()
    begin
      entrylist = get_content(@datadir + @category,@content_pattern)
      list = get_contentlist(entrylist)
      list.sort!
      list.reverse!
      list.each do |entry|
        print make_content(entry)
      end
    rescue
      print  "<p>error #$! #$@</p>\n"
    end
  end
  def makeheader
    print template.exec("content_type")
    print template.exec("header")
  end
  def makefooter
    print template.exec("footer")
  end
  def set_globalvariable(cgi)
    @cgi = cgi
    if cgi.has_key? "category" then
      @category = cgi["category"].join()
    else
      @category = ""
    end
  end 
  def main(cgi)
    set_globalvariable(cgi)
    makeheader()
    makemain()
    makefooter()
  end
end

sakura = Sakura.new do
  self.datadir = "/var/autofs/misc/groups/s/sa/sakura/htdocs/sakura"
  self.content_pattern = /^.+.txt$/
  self.template = Template.new()
  self.encoding = "EUC-JP"
  self.template.value["$blog_title"] = "Sakura: Simple Blog Tool "
  self.template.value["$encoding"] = self.encoding
  self.template.value["$mailaddr"] = "moleskin@as.email.ne.jp"
  self.template.add("content_type",CONTENT_TYPE)
  self.template.add("content",CONTENT_TEMPLATE)
  self.template.add("header",HEADER_TEMPLATE)
  self.template.add("footer",FOOTER_TEMPLATE)
end

cgi = CGI.new
sakura.main(cgi)