require 'fileutils'
require 'lib/handle_js_files'

ICAL_SRC = File.join(GEM_ROOT, 'src', 'ical')
ICAL_SRC_VIEWS = File.join(ICAL_SRC, 'app', 'views', 'shared')
ICAL_SRC_CONFIG = File.join(ICAL_SRC, 'config', 'initializers')
ICAL_SRC_IMAGES = File.join(ICAL_SRC, 'images')

ICAL_DEST_TEMPLATES = File.join(GEM_ROOT, 'templates', 'ical')
ICAL_DEST_VIEWS = File.join(ICAL_DEST_TEMPLATES, 'app', 'views', 'shared')
ICAL_DEST_CONFIG = File.join(ICAL_DEST_TEMPLATES, 'config', 'initializers')
ICAL_DEST_STYLESHEETS = File.join(ICAL_DEST_TEMPLATES, 'jquery')
ICAL_DEST_IMAGES = File.join(ICAL_DEST_STYLESHEETS, 'ical')

ICAL_MESSAGE1 = "# Generated by compass-jquery-plugin/gem-tasks/ical.rake\n# Install with: compass install jquery/ical\n\n"
ICAL_MESSAGE2 = "// Generated by compass-jquery-plugin/gem-tasks/ical.rake\n\n"

all_scripts = [
    'js/anytime.js',
    'js/anytimetz.js',
    'js/fullcalendar.js',
    'js/gcal.js'
].collect { |filename| File.read(File.join(ICAL_SRC, filename)) }.join "\n\n"

all_stylesheets = [
    'anytime.css',
    'fullcalendar.css'
].collect { |filename| File.read(File.join(ICAL_SRC, 'css', filename)) }.join "\n\n"

print_stylesheets = [
    'fullcalendar.print.css'
].collect { |filename| File.read(File.join(ICAL_SRC, 'css', filename)) }.join "\n\n"

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

    FileUtils.remove_dir ICAL_DEST_TEMPLATES if File.exists? ICAL_DEST_TEMPLATES
    FileUtils.mkdir_p(ICAL_DEST_CONFIG)
    FileUtils.mkdir_p(ICAL_DEST_VIEWS)

    open File.join(ICAL_DEST_TEMPLATES, 'manifest.rb'), 'w' do |manifest|
      manifest.print ICAL_MESSAGE1

      Dir.foreach ICAL_SRC_VIEWS do |file|
        next unless /\.haml$/ =~ file
        html = File.read File.join(ICAL_SRC_VIEWS, file)
        open File.join(ICAL_DEST_VIEWS, file), 'w' do |f|
          f.print(html)
        end
        manifest.print "file 'app/views/shared/#{file}'\n"
      end

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

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

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

      FileUtils.mkdir_p File.join(ICAL_DEST_STYLESHEETS)
      open File.join(ICAL_DEST_STYLESHEETS, 'ical.scss'), 'w' do |f|
        sass = ICAL_MESSAGE2
        IO.popen("sass-convert -F css -T scss", 'r+') { |ff| ff.print(all_stylesheets); ff.close_write; sass += ff.read }
        f.print sass
      end
      manifest.print "stylesheet 'jquery/ical.scss'\n"

      open File.join(ICAL_DEST_STYLESHEETS, 'ical.print.scss'), 'w' do |f|
        sass = ICAL_MESSAGE2
        IO.popen("sass-convert -F css -T scss", 'r+') { |ff| ff.print(print_stylesheets); ff.close_write; sass += ff.read }
        f.print sass
      end
      manifest.print "stylesheet 'jquery/ical.print.scss'\n"

      # Images  

      # Copy the images directory
      FileUtils.mkdir_p File.join(ICAL_DEST_IMAGES)
      src_dir = ICAL_SRC_IMAGES
      dest_dir = ICAL_DEST_IMAGES

      Dir.foreach(src_dir) do |image|
        next unless /\.png$/ =~ image
        FileUtils.cp(File.join(src_dir, image), dest_dir)
        manifest.print "image 'jquery/ical/#{image}'\n"
      end
    end
  end
end