#
# Copyright (C) 2002-2004 Satoru Takabayashi <satoru@namazu.org> 
# Copyright (C) 2003-2005 Kouichirou Eto
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.
#

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/util-kconv"
require "qwik/config"
require "qwik/memory"
require "qwik/farm"

module QuickML
  class QwikSite
    def initialize(sitename, test=false)
      if test
	@config = $test_config
	@memory = $test_memory
      else
	@config = Qwik::Config.new(test)
	@memory = Qwik::ServerMemory.new(@config)
      end

      @sitename = sitename
      @site = @memory.farm.get_site(@sitename)
      @key = nil
    end
    attr_reader :key

    def valid_as_pagekey?(t) # same as in act-newpage.rb
      /^[A-Za-z_0-9]+$/ =~ t
    end

    def post(mail)
      # get subject
      subject = mail.get_clean_subject

      page = @site.get_by_title(subject)
      if page.nil?
	if valid_as_pagekey?(subject)
	  page = @site.create(subject) # CREATE
	else
	  page = @site.create_new # CREATE
	end
	page.store("* "+subject+"\n")
      end
      @key = page.key # @key will be used at qwik_url

      # get body
      message = make_message(mail)
      page.add(message)
    end

    #private

    def make_message(mail)
      from = mail.mail_from
      body = ""
      mail.each_part {|mail|
	if mail.plain_text_body?
	  #body << mail.body.chomp.chomp+"\n" # This is too ad hoc.
	  body << mail.decoded_body.chomp.chomp+"\n" # This is too ad hoc.
	else
	  filename = mail.filename
	  content = mail.decoded_body
	  if filename && content
	    msg = attach(filename, content)
	    body << msg
	  end
	end
      }

      t = Time.now
      t = Time.at(0) if $quickml_debug
      time = t.to_i.to_s
      message = "{{mail(#{from},#{time})\n#{body}}}\n".jistosjis
      message
    end

    def file_with_num(filename, num)
      #filename+"."+num.to_s
      num.to_s+"-"+filename
    end

    def attach(filename, content)
      key = @key
      key = "FrontPage" if key.nil?

      num = 0
     #if @site.attach.exist?(filename)
      if @site.files(key).exist?(filename)
	num = 1
       #while @site.attach.exist?(file_with_num(filename, num))
	while @site.files(key).exist?(file_with_num(filename, num))
	  num += 1
	end
	#return "Site attach failed because the same filename is already used.\n"
      end

      if num != 0
	filename = file_with_num(filename, num)
      end

    # @site.attach.put(filename, content)
      @site.files(key).put(filename, content)
     #"\n{{ref(#{filename})}}\n\n"
      "\n{{file(#{filename})}}\n\n"
    end
  end
end

if $0 == __FILE__
  require "qwik/test-common"
  $debug = true
end

if $debug
  class TC_MLSite < Test::Unit::TestCase
    include TestSession

    def test_all
      site = QuickML::QwikSite.new("test", true)

      assert_equal(nil, site.key)

      # test valid as pagekey
      assert_equal(0, site.valid_as_pagekey?("t"))
      assert_equal(nil, site.valid_as_pagekey?("t t"))

      # test file with num
      assert_equal("1-t", site.file_with_num("t", 1))

      # test attach
      assert_equal(nil, site.key)
      assert_equal("\n{{file(t.txt)}}\n\n", site.attach("t.txt", "test."))
      assert_equal(true, @site.files("FrontPage").exist?("t.txt"))
      assert_equal("\n{{file(1-t.txt)}}\n\n", site.attach("t.txt", "test."))
      assert_equal(true, @site.files("FrontPage").exist?("1-t.txt"))
    end
  end
end
