MOBILE_MESSAGE1 = "# Generated by compass-jquery-plugin/gem-tasks/mobile.rake\n# Install with: compass install jquery/mobile\n\n"
MOBILE_MESSAGE2 = "// Generated by compass-jquery-plugin/gem-tasks/mobile.rake\n"

class JqueryMobileTheme

  # Initialize with the base theme
  def initialize(base_theme_directory)
    @prefix = 'jquery.mobile'
    @theme_filename = "#{@prefix}.theme.css"
  end

  # Create a sass file of variables names and copy the images
  def convert_theme(name, dir, stylesheets)
    theme = File.read(File.join(dir, @theme_filename))

    # Fix stuff
    theme.gsub!(/\;filter:Alpha/, "; filter: Alpha")
    theme.gsub! /url\(images(.+?)\)/, "image_url(\"jquery/mobile/#{name}\\1\")"

    # Convert the stylesheet
    open File.join(MOBILE_DEST_THEMES, "#{name}.scss"), 'w' do |f|
      sass = MOBILE_MESSAGE2
      IO.popen("sass-convert -F css -T scss", 'r+') { |ff| ff.print(theme); ff.close_write; sass += ff.read }
      f.print sass
      f.print "\n@import \"jquery/mobile/_base\"\n"
    end
  end
end
