module Docbook
  module Adapters
    module Epub
      module Epubber
    
        OUTPUT_DIR = ".epubtmp#{Time.now.to_f.to_s}"
        MIMETYPE = "application/epub+zip"
        META_DIR = "META-INF"
        OEBPS_DIR = "OEBPS"
      
        def collect_images
          FileUtils.cp_r "images", "#{OUTPUT_DIR}/#{OEBPS_DIR}/images"
        end
        
        def collect_cover
          if File.exist?("cover/cover.jpg")
            FileUtils.mkdir "#{OUTPUT_DIR}/#{OEBPS_DIR}/cover"
            FileUtils.cp "cover/cover.jpg", "#{OUTPUT_DIR}/#{OEBPS_DIR}/cover/cover.jpg"
          else
            OUTPUT.say "Could not find a cover. Ensure cover/cover.jpg exists"
          end
        end
        
        def build_epub
          FileUtils.rm self.output_path # Saxon creates this stupid empty file. Let's nuke it
          
          OUTPUT.say "Collecting resources..."
          FileUtils.mv META_DIR,  "#{OUTPUT_DIR}/#{META_DIR}"
          
          FileUtils.mv OEBPS_DIR,  "#{OUTPUT_DIR}/#{OEBPS_DIR}"
          FileUtils.cp "xsl/#{@xsl_extension}.css", "#{OUTPUT_DIR}/#{OEBPS_DIR}/style.css"
          
          collect_images
          collect_cover

          Dir.chdir(OUTPUT_DIR) do
            File.open("mimetype", "w") {|f| f.print MIMETYPE}
            
            zip_cmd = %Q(zip -X -r  "../#{self.output_path}" mimetype "#{META_DIR}" "#{OEBPS_DIR}")
            OUTPUT.say zip_cmd
            run_command zip_cmd
          end
          
          if self.validate
            cmd = "java -jar #{self.root}/jars/epubcheck/epubcheck.jar #{self.output_path}"
            OUTPUT.say "Validating generated epub"
            run_command cmd
          end
          FileUtils.rm_rf OUTPUT_DIR
        end
        
        
        def xml_parser_options
          opts = super 
          opts << " chunk.quietly=1" 
          opts << " base.dir=#{OEBPS_DIR}/"
          opts << " epub.oebps.dir=#{OEBPS_DIR}/"
          opts << " epub.metainf.dir=#{META_DIR}/"
          opts
        end
        
        
        
        def before_render
          
          FileUtils.mkdir OUTPUT_DIR
   
          xsl_path = @windows ? self.root : self.root.lchop
      
          xml = %Q{<?xml version='1.0'?>

          <!-- DO NOT CHANGE THIS FILE. IT IS AUTOGENERATED BY THE SHORT-ATTENTION-SPAN-DOCBOOK CHAIN -->

          <xsl:stylesheet 
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
             xmlns:fo="http://www.w3.org/1999/XSL/Format"
             xmlns:xslthl="http://xslthl.sf.net"
             xmlns:d="http://docbook.org/ns/docbook"
          >
            <!-- Import the original EPUB stylesheets -->
            <xsl:import href="file:///#{xsl_path}/xsl/epub/docbook.xsl"/>
            <xsl:import href="file:///#{xsl_path}/xsl/xhtml-1_1/highlight.xsl"/>
      
            <!-- FOP -->
   
          }
      
          xml << %Q{
            <xsl:param name="show.comments" select="1"></xsl:param>
            <xsl:param name="draft.watermark.image">http://docbook.sourceforge.net/release/images/draft.png</xsl:param>
            <xsl:param name="draft.mode">yes</xsl:param>
          } if @draft
      
          xml << "</xsl:stylesheet>"
      
          File.open("xsl/epub_base.xsl", "w") do |f|
            f << xml
          end
        end
    

      end
    end
  end
end