module Fullstack
  module Cms
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
        
      class_option :host, :default => "remotehost"
      class_option :user, :default => "ror"
      class_option :title, :default => "title"
      class_option :slogan, :default => "slogan"
      class_option :localize, :type => :boolean, :default => true
      
      def install_fullstack_admin
          generate "fullstack:admin:install  --host='#{host}' --user='#{user}'  --title='#{title}' --slogan='#{slogan}'"
      end
      
      def install_libraries
        
        generate "ars_permalink:install"
        generate "has_attached:install"  
        generate "acts_as_taggable_on:migration"
                
      end
      
      def templates_tree
        remove_file "public"
        remove_file "app/assets/javascripts/application.js"
        remove_file "app/assets/javascripts/application.css"
        remove_file "app/views/layouts/application.html.erb"
        remove_file "public/404.html"
        remove_file "public/robots.txt"
        remove_file "config/styles.yml"
        
        directory "rails", Rails.root
      end
      
      def assets
        append_to_file "config/assets.yml" do
<<-eos

- site/site.css
- site/site.js          
eos
          
        end
      end
      
      def routes_rb
        

        if !localize
          route '''
    constraints(:host => /^#{Regexp.escape(Settings.app.domain)}/) do
       root :to => redirect("http://www.#{Settings.app.domain}")
       match "/*path", :to => redirect {|params, request| 
         query_part = request.query_string.present? ? "?#{request.query_string}" : ""  
         "http://www.#{Settings.app.domain}/#{params[:path]}#{query_part}"
       }
    end

    # namespace :site, :path => "/" do
    #   mount_controller "site" 
    # end

          '''
        else
          route '''
    constraints(:host => /^#{Regexp.escape(Settings.app.domain)}/) do
      root :to => redirect("http://www.#{Settings.app.domain}/#{I18n.default_locale}/")
      match \'/*path\', :to => redirect {|params| "http://www.#{Settings.app.domain}/#{params[:path]}"}
    end
  
    # root :to => redirect("/#{I18n.default_locale}/")
    # namespace :site, :path => "/" do
    #  mount_controller "site"
    # end


          '''
        end

      end
      
      def set_localization
        if !localize
          inject_into_file "config/fullstack.rb", :after => "Fullstack::Cms.configure do |config|" do
%q{
  
  config.localized = false

}            
          end 
        else  
          inject_into_file "config/fullstack.rb", :after => "Fullstack::Cms.configure do |config|" do
%q{
  
  # config.localized = false

}            
          end 

        end
      end
      
      def home_page_helper
        append_to_file "app/helpers/application_helper.rb", :after => "ApplicationHelper" do
%q{
 
  def home_page?
    self.class == Site::SiteController && action_name == "home"
  end
         
}
        end
        
      end
      
            
      def install_migrations
        generate "migration:from link menu page_part page redirect setting attachment photo text_page_part text_with_title_page_part"
      end
      
      
      
      protected
      
      def migration_timestamp
        Time.now.strftime("%Y%m%d%H%M%S")
      end

      def host
        options[:host]
      end
      
      def domain
        host
      end
      
      def localize
        options[:localize]
      end
    
      def user
         options[:user]
      end
    
      def app
        Rails.application.class.to_s.split("::").first.underscore      
      end
    
      def email
        "info@#{host}"
      end
    
      def title
        options[:title]      
      end
    
      def slogan
        options[:slogan]
      end
      
    end
  end
end