#!/usr/bin/env ruby
require 'rubygems'
require 'rake'

def cwd
  File.expand_path(File.join(File.dirname(__FILE__), '..'))
end

def expand(*paths)
  File.expand_path(File.join(*paths))
end

def rakefile_path
  expand(cwd, 'templates/Rakefile')
end

if ARGV[0] == 'init'
  require 'ftools'
  File.makedirs('spec/javascripts')
  File.makedirs('spec/helpers')

  dest_root = File.expand_path(Dir.pwd)
  dest_spec = expand(dest_root, 'spec')
  dest_spec_javascripts = expand(dest_root, 'spec/javascripts')
  dest_spec_helpers = expand(dest_root, 'spec/helpers')

  unless File.exist?(expand(dest_spec_helpers, 'spec_helper.js'))
    File.copy(expand(cwd, 'templates/spec_helper.js'), dest_spec_helpers)
    end
  unless File.exist?(expand(dest_spec_helpers, 'jasmine_helper.rb'))
    File.copy(expand(cwd, 'templates/jasmine_helper.rb'), dest_spec_helpers)
  end

  File.copy(expand(cwd, 'templates/example_spec.js'), dest_spec_javascripts)

  if File.exist?(expand(dest_root, 'Rakefile'))
    existing_rakefile = expand(dest_root, 'Rakefile')
    load existing_rakefile
    unless Rake::Task.task_defined?('jasmine')
      open(existing_rakefile, 'a') do |f|
        f.write(File.read(rakefile_path))
      end
    end
  else
    File.copy(rakefile_path, dest_root)
  end
end

