require 'fileutils'
require 'lib/handle_js_files'

# Compass generator for DYNATREE_14
DYNATREE_SRC = File.join(GEM_ROOT, 'src', 'dynatree')
DYNATREE_SRC_CONFIG = File.join(DYNATREE_SRC, 'config', 'initializers')
DYNATREE_SRC_SKINS = File.join(DYNATREE_SRC, 'skins')

DYNATREE_DEST_TEMPLATES = File.join(GEM_ROOT, 'templates', 'dynatree')
DYNATREE_DEST_CONFIG = File.join(DYNATREE_DEST_TEMPLATES, 'config', 'initializers')
DYNATREE_DEST_SKINS = File.join(DYNATREE_DEST_TEMPLATES, 'jquery', 'dynatree')
DYNATREE_DEST_IMAGES = File.join(DYNATREE_DEST_SKINS)

DYNATREE_MESSAGE1 = "# Generated by compass-jquery-plugin/gem-tasks/dynatree.rake\n# Install with: compass install jquery/dynatree\n\n"
DYNATREE_MESSAGE2 = "// Generated by compass-jquery-plugin/gem-tasks/dynatree.rake\n\n"

all_scripts = [
    'js/jquery.dynatree.js'
].collect { |filename| File.read(File.join(DYNATREE_SRC, filename)) }.join "\n\n"

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

    FileUtils.remove_dir DYNATREE_DEST_TEMPLATES if File.exists? DYNATREE_DEST_TEMPLATES
    FileUtils.mkdir_p(DYNATREE_DEST_CONFIG)

    open File.join(DYNATREE_DEST_TEMPLATES, 'manifest.rb'), 'w' do |manifest|
      manifest.print DYNATREE_MESSAGE1

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

      open File.join(DYNATREE_DEST_TEMPLATES, 'jquery.dynatree.js'), 'w' do |f|
        f.print concat_files(all_scripts)
      end
      manifest.print "javascript 'jquery.dynatree.js'\n"

      open File.join(DYNATREE_DEST_TEMPLATES, 'jquery.dynatree.min.js'), 'w' do |f|
        f.print compress_js(all_scripts, "google")
      end
      manifest.print "javascript 'jquery.dynatree.min.js'\n"

      # jQuery DynaTree Skins

      FileUtils.mkdir_p(DYNATREE_DEST_SKINS)

      Dir.foreach DYNATREE_SRC_SKINS do |skin|
        next if /^\./ =~ skin

        # Convert the stylesheet

        Dir.foreach File.join(DYNATREE_SRC_SKINS, "#{skin}") do |file|
          next unless /\.css$/ =~ file
          css = File.read File.join(DYNATREE_SRC_SKINS, "#{skin}", file)
          sass = ''
          IO.popen("sass-convert -F css -T scss", 'r+') { |f| f.print(css); f.close_write; sass = f.read }
          open File.join(DYNATREE_DEST_SKINS, "#{skin}.scss"), 'w' do |f|
            f.write DYNATREE_MESSAGE2 + sass
          end
          manifest.print "stylesheet 'jquery/dynatree/#{skin}.scss'\n"
        end

        # Copy the skin images directory
        src_dir = File.join(DYNATREE_SRC_SKINS, skin, 'images')
        dest_dir = File.join(DYNATREE_DEST_IMAGES, "#{skin}")
        FileUtils.mkdir_p dest_dir

        Dir.foreach(src_dir) do |image|
          next if /^\./ =~ image
          FileUtils.cp(File.join(src_dir, image), dest_dir)
          manifest.print "image 'jquery/dynatree/#{skin}/#{image}'\n"
        end
      end
    end
  end
end