module JekyllAndHyde
  class New < JekyllAndHyde::GroupTask
    JEKYLL_AND_HYDE_TEMPLATE_GIT_REPO = 'git://github.com/jingweno/jekyll_and_hyde_template.git'
    SLIPPY_GIT_REPO = 'git://github.com/jingweno/slippy.git'

    argument :app_path, :type => :string, :required => true, :desc => "The app path to generate the skeletal installation"
    class_option :github, :type => :boolean, :desc => "Create branching information for GitHub Project Pages, details in http://pages.github.com/."
    class_option :template, :type => :string, :default => JEKYLL_AND_HYDE_TEMPLATE_GIT_REPO, :desc => "The Git repository of the generated Jekyll template to download."
    desc "Generate a skeletal jekyll_and_hyde installation in [APP_PATH]."

    def make_app_path_dir
      empty_directory app_path
    end

    # TODO: pass in the url as parameter
    def git_clone_template
      inside_app_path do
        run_command "git clone #{options[:template]} ."
        remove_file('.git')
      end
    end

    def git_init
      inside_app_path do
        run_command "git init"
      end
    end

    def add_slippy_git_submodule
      inside_app_path do
        run_command "git submodule add #{SLIPPY_GIT_REPO}"
      end
    end

    def git_add_all
      inside_app_path do
        run_command "git add ."
      end
    end

    def create_git_hub_page
      if options.github?
        inside_app_path do
          # Commit all changes
          run_command 'git commit -am "Template files generated by jekyll_and_hyde."'
          invoke_github_task
        end
      end
    end

    private

    def inside_app_path(config = {}, &block)
      inside app_path, config, &block
    end

    def invoke_github_task
      klass = JekyllAndHyde::Util.find_class_and_task_by_namespace("github").first
      klass.start([], :shell => self.shell)
    end
  end
end