require 'fileutils'
require 'lib/handle_js_files'

TINY_MCE_SRC = File.join(GEM_ROOT, 'src', 'tiny_mce')
TINY_MCE_SRC_CONFIG = File.join(TINY_MCE_SRC, 'config', 'initializers')
TINY_MCE_SRC_SCRIPTS = File.join(TINY_MCE_SRC, 'js/')

TINY_MCE_DEST_TEMPLATES = File.join(GEM_ROOT, 'templates', 'tiny_mce')
TINY_MCE_DEST_CONFIG = File.join(TINY_MCE_DEST_TEMPLATES, 'config', 'initializers')
TINY_MCE_DEST_SCRIPTS = File.join(TINY_MCE_DEST_TEMPLATES, 'tiny_mce')

TINY_MCE_MESSAGE1 = "# Generated by compass-jquery-plugin/gem-tasks/tiny_mce.rake\n# Install with: compass install jquery/tinymce\n\n"
TINY_MCE_MESSAGE2 = "// Generated by compass-jquery-plugin/gem-tasks/tiny_mce.rake\n\n"

namespace :build do
  desc 'Build the stylesheets and templates for tiny_mce.'
  task :tiny_mce do

    FileUtils.remove_dir TINY_MCE_DEST_TEMPLATES if File.exists? TINY_MCE_DEST_TEMPLATES
    FileUtils.mkdir_p(File.join(TINY_MCE_DEST_TEMPLATES, 'config', 'initializers'))

    open File.join(TINY_MCE_DEST_TEMPLATES, 'manifest.rb'), 'w' do |manifest|
      manifest.print TINY_MCE_MESSAGE1

      open File.join(TINY_MCE_DEST_CONFIG, 'tiny_mce.rb'), 'w' do |f|
        f.print(File.read(File.join(TINY_MCE_SRC_CONFIG, 'tiny_mce.rb')))
      end
      manifest.print "file 'config/initializers/tiny_mce.rb'\n"

      handleTinyMCEDir(manifest, TINY_MCE_SRC_SCRIPTS, TINY_MCE_DEST_SCRIPTS)
    end
  end
end