# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH.unshift("../../compat") unless $LOAD_PATH.include?("../../compat")
begin
  require "zip/zip"
  $have_zip = true
rescue LoadError
  $have_zip = false
end

module Qwik
  class Action
    def plg_zip
      [:span, {:class=>"attribute"},
	[:a, {:href=>@site.sitename+".zip"}, _("site archive")]]
    end

    def ext_zip
      c_require_member

      raise "unknown basename" unless @req.base == @req.sitename
      gen = ZipGenerator.new(@config, @site, self)
      path = gen.generate
      c_simple_send(path, "application/zip")
    end
  end

  class ZipGenerator
    def initialize(config, site, action)
      @config, @site = config, site
      @action = action
    end

    def generate
      zip_file = @site.sitename+".zip"
      site_path = get_sitepath
      zip_path = site_path + zip_file
      Zip::ZipOutputStream.open(zip_path.to_s) {|zos|
	@site.each_all {|page|
	  add_page(zos, site_path, page)
	}
	add_theme(zos)
      }
      zip_path
    end

    private

    def get_sitepath
      return (@site.attach.path("")+"..").cleanpath
    end

    def add_page(zos, site_path, page)
      base = @site.sitename+"/"+page.key
      add_txt(zos, base, page)

      html_path = site_path+(page.key+".html")
#     @action.generate_html(page.key) if !html_path.exist?
      @action.view_page_cache_generate(page.key) if !html_path.exist?
      return unless html_path.exist? # what?
      file = base+".html"
      str = html_path.read
      add_entry(zos, file, str)

      html_path = site_path+(page.key+"-presen.html")
      gen = PresenPageGenerator.new(@config, @action)
      w = gen.generate_html(@site, page.key)
      str = w.format_xml
      file = base+"-presen.html"
      add_entry(zos, file, str)
    end

    def add_txt(zos, base, page)
      file = base+".txt"
      str = page.load
      add_entry(zos, file, str)
    end

    def add_entry(zos, filename, content)
      e = Zip::ZipEntry.new("", filename)
      zos.put_next_entry(e)
      zos.write(content)
    end

    def add_theme(zos)
      ar = []
      # FIXME: collect file list from the directory.
      ar << "css/base.css"
      ar << "css/wema.css"
      ar << "js/base.js"
      ar << "js/debugwindow.js"
      ar << "js/niftypp.js"
      ar << "js/wema.js"
      ar << "i/external.png"
      ar << "i/new.png"

      t = @site.theme
      list = @action.theme_files(t)
      list.each {|f|
	ar << "#{t}/#{f}"
      }

      ar << "s5/i18n/slides.css"
      ar << "s5/i18n/s5-core.css"
      ar << "s5/i18n/framing.css"
      ar << "s5/i18n/pretty.css"
      ar << "s5/i18n/bg-shade.png"
      ar << "s5/i18n/bg-slide.jpg"
      ar << "s5/default/outline.css"
      ar << "s5/default/print.css"
      ar << "s5/default/opera.css"
      ar << "s5/default/slides.js"

      theme_dir = @config.theme_dir
      ar.each {|b|
	file  = @site.sitename+"/.theme/"+b
	local = theme_dir+"/"+b
	str = local.path.read
	#qp file, local
	add_entry(zos, file, str)
      }
    end
  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "qwik/test-common"
  $debug = true
end

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

    def test_plg_zip
      assert_wiki("<p><a href=\"test.zip\">test.zip</a></p>", "[[test.zip]]")
      assert_wiki("<span class=\"attribute\"><a href=\"test.zip\">site archive</a></span>", "{{zip}}")
    end
  end
end
