#!/usr/bin/env ruby
# copied from hobo and then modified. Some stuff is there because of that.

require 'fileutils'
require 'pathname'
require 'rbconfig'
require 'rvm'

if not RVM.current
  puts "ruby or rvm not found"
  exit 2
end

Signal.trap("INT") { puts; exit }

src = File.join(File.dirname(__FILE__), "..")
require "#{src}/lib/inline_forms/version.rb"

USAGE = "
USAGE: inline_forms <options> <app-name>

  'inline_forms MyApp' will create a rails app in directory MyApp.

Options:
  --help                      # show this message

VERSION: #{InlineForms::VERSION}
"


while true
  case arg_name = ARGV.shift
  when "--help"
    puts USAGE
    exit
  else
    app_name = arg_name
    break
  end
end

if !ARGV.empty? || app_name.nil? || !/^[a-zA-Z][0-9a-zA-Z_-]+/.match(app_name) || File.exists?(app_name)
  puts USAGE
  exit 1
end

puts "\nGenerating Rails app '#{app_name}'...\n"
RVM.run "rails new #{app_name}"

RVM.chdir(app_name) do
  puts "\nWorking directory is now #{`pwd`}"

  puts "\nInstalling Gemfile...\n"

  gemfile_sources = "
source 'http://rubygems.org'

  "
  gemfile_gems = "

gem 'test-unit'
gem 'rails'
gem 'rake'
gem 'jquery-rails'
gem 'capistrano'
gem 'will_paginate' #, :git => 'git://github.com/acesuares/will_paginate.git', :branch => 'rails3'
gem 'tabs_on_rails' # , :git => 'git://github.com/acesuares/tabs_on_rails.git', :branch => 'add_remote'
gem 'ckeditor', :git => 'git://github.com/acesuares/ckeditor.git', :branch => 'master'
gem 'carrierwave'
gem 'remotipart', '~> 1.0'
gem 'paper_trail'
gem 'devise'
gem 'cancan'
gem 'inline_forms'
gem 'mini_magick'
gem 'jquery_datepicker'
gem 'yaml_db'
gem 'rails-i18n'
gem 'unicorn'
gem 'rvm-capistrano'

  "
  gemfile_development_group ="

# Include everything needed to run rake, tests, features, etc.
group :development do
  gem 'sqlite3'
  gem 'rspec-rails'
  gem 'shoulda', '>= 0'
  gem 'bundler'
  gem 'jeweler'
#  gem 'rcov', '>= 0'
end

  "
  gemfile_production_group ="
# these are just for production
group :production do
  gem 'mysql2'
  gem 'therubyracer'
  gem 'uglifier'
end

  "

  File.open( 'Gemfile', 'w') {|f| f.write(gemfile_sources + gemfile_gems + gemfile_development_group + gemfile_production_group ) }

  puts "\nCreating and trusting rvmrc...\n"
  RVM.run "echo rvm use `rvm current`@#{app_name} --create > .rvmrc"
  RVM.run "rvm rvmrc trust .rvmrc"

end
RVM.chdir "#{app_name}" do

  puts "\nRunning bundle...\n"
  RVM.run "bundle install"

  puts "\nDatabase setup: creating config/database.yml\n"
  development_stanza = "
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000
  "
  production_stanza = "
production:
  adapter: mysql2
  database: #{app_name}_p
  username: #{app_name}
  password: #{app_name}444

  "
  File.open( 'config/database.yml', 'w') {|f| f.write(development_stanza + production_stanza) }

  puts "\nDevise install..."
  system('bundle exec rails g devise:install')
  puts "\nDevise User model install with added name field..."
  system('bundle exec rails g devise User name:string')
  puts "\nInstall ckeditor..."
  system('bundle exec rails g ckeditor:install')
  puts "Create config file in app/assets/javascripts/ckeditor/config.js"
  FileUtils.mkdir_p 'app/assets/javascripts/ckeditor'
  system("cp #{src}/lib/app/assets/javascripts/ckeditor/config.js app/assets/javascripts/ckeditor")
  puts "Add remotipart to app/assets/javascripts/application.js..."
  system('echo >> app/assets/javascripts/application.js')
  system('echo "//= require jquery.remotipart" >> app/assets/javascripts/application.js')
  system('echo >> app/assets/javascripts/application.js')
  puts "Paper_trail install..."
  system('bundle exec rails g paper_trail:install')


  puts "Migrating Devise and Versions"
  system('bundle exec rake db:migrate')

  puts "Creating header in app/views/inline_forms/_header.html.erb...\n"
  header_src = "
<div id='Header'>
  <div id='title'>
    #{app_name} v<%= inline_forms_version -%>
  </div>
  <% if cancan_enabled? -%>
  <div id='logout'>
    <%= link_to 'Afmelden: \#{current_user.name}', destroy_user_session_path, :method => :delete %>
  </div>
  <% end -%>
  <div style='clear: both;'></div>
</div>
  "
  FileUtils.mkdir_p 'app/views/inline_forms'
  File.open( 'app/views/inline_forms/_header.html.erb', 'w') {|f| f.write(header_src) }

  puts "Set application_name and _title in ApplicationHelper...\n"
  app_helper = "
module ApplicationHelper
  def application_name
    #{app_name}
  end
  def application_title
    #{app_name}
  end
end
  "
  File.open( 'app/helpers/application_helper.rb', 'w') {|f| f.write(app_helper) }


  puts "Injecting precompile assets stuff in production.rb...\n"
  precomp = "
  \n
  \n
  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  #{app_name}::Application.configure do
    config.assets.precompile += %w( inline_forms_application.js inline_forms_application.css devise.css )
  end
  \n"
  File.open( 'config/environments/production.rb', 'a') {|f| f.write(precomp) }

  puts "Capify...\n"
  system('capify .')
  system("cp #{src}/lib/generators/templates/deploy.rb config/deploy.rb")

  puts "Unicorn Config...\n"
  system("cp #{src}/lib/generators/templates/unicorn.rb config/unicorn.rb")

  puts "Initializing git...\n"
  system('git init')
  system('echo >> .gitignore')
  system('echo "nbproject" >> .gitignore')
  system('echo "public/uploads" >> .gitignore')
  system('echo  >> .gitignore')
  system('git add .')
  system('git commit -a -m " * Initial"')

  puts "\n\nDone! Now make your tables with 'rails g inline_forms ...\n"
  puts "\nDon't forget: edit .rvmrc, config/{routes.rb, deploy.rb}, .git/config, delete public/index.html\n\n"



end