require 'fileutils'
require 'lib/handle_js_files'

# Compass generator for RIBBON_14
RIBBON_SRC = File.join(GEM_ROOT, 'src', 'ribbon')
RIBBON_SRC_CONFIG = File.join(RIBBON_SRC, 'config', 'initializers')
RIBBON_SRC_IMAGES = File.join(RIBBON_SRC, 'images')
RIBBON_SRC_THEMES = File.join(RIBBON_SRC, 'themes')

RIBBON_DEST_TEMPLATES = File.join(GEM_ROOT, 'templates', 'ribbon')
RIBBON_DEST_CONFIG = File.join(RIBBON_DEST_TEMPLATES, 'config', 'initializers')
RIBBON_DEST_THEMES = File.join(RIBBON_DEST_TEMPLATES, 'jquery', 'ribbon')
RIBBON_DEST_IMAGES = File.join(RIBBON_DEST_THEMES)

RIBBON_MESSAGE1 = "# Generated by compass-jquery-plugin/gem-tasks/ribbon.rake\n# Install with: compass install jquery/ribbon\n\n"
RIBBON_MESSAGE2 = "// Generated by compass-jquery-plugin/gem-tasks/ribbon.rake\n\n"

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

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

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

    open File.join(RIBBON_DEST_TEMPLATES, 'manifest.rb'), 'w' do |manifest|
      manifest.print RIBBON_MESSAGE1

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

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

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

      # jQuery Ribbon Themes

      FileUtils.mkdir_p(RIBBON_DEST_THEMES)

      Dir.foreach RIBBON_SRC_THEMES do |theme|
        next if /^\./ =~ theme

        # Convert the stylesheet

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

        # Copy the theme images directory
        src_dir = File.join(RIBBON_SRC_THEMES, theme, 'images')
        dest_dir = File.join(RIBBON_DEST_IMAGES, "#{theme}")
        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/ribbon/#{theme}/#{image}'\n"
        end
      end
    end
  end
end