#!/usr/bin/ruby

require 'fileutils'

def usage
	return %q{Usage: svnrails app uri_to://subversion/server [options]

Options:
    -n, --repository_name=repos      Use a different repository than the default (default to app, whatever you name the application)
    -j, --just-app                   Puts the rails app in the root of the repository and does not generate "trunk", "branches", etc.

Description:
    The 'svnrails' command creates a new Rails application with a default
    directory structure and configuration at the path you specify and makes
    it "versioned" with subversion on the server you specify.

Example:
    svnrails weblog http://svn.my-domain.com/

    This generates a skeletal Rails installation in the repository 'weblog'
    at http://svn.my-domain.com/ and checks out a working copy in ./weblog. 
    This will work with any type of Subversion repository (ie: file:///, 
    http://, svn://)}
end

def create_temp_structure(app_name, app_only)
  if !app_only
  	Dir.mkdir(app_name)
	  Dir.chdir(app_name)
	  Dir.mkdir('trunk')
	  Dir.mkdir('branches')
	  Dir.mkdir('tags')
	  Dir.chdir('trunk')
	  `rails #{app_name}`
	  Dir.chdir('../..')
	else
		`rails #{app_name}`
  end
end

# Check if arguments were given, if not, display usage
if ARGV.size < 2
	puts usage; exit
end

#Get values of arguments
app_path   = ARGV.first
svn_uri    = ARGV[1].chomp("/")
just_app   = ARGV.any? { |option| %w(-j --just-app).include?(option) }
repos_name = (ARGV.select {|o| o =~ /(-n|--repos-name)=.*/}.first || "").split("=")[1] || app_path

# Create the temporary directory structure to import into the repository
puts 'Creating temporary structure . . .'
create_temp_structure(app_path, just_app)

# Import the whole thing into the repository
puts "Importing structure into #{svn_uri} . . ."
`svn import #{app_path} #{svn_uri}/#{repos_name} -m 'initial structure for a rails app'`

# Delete the temporary structure
puts 'Deleting temporary structure . . .'
FileUtils.rmtree app_path

# Check out the project
puts 'Checking out the project . . .'
`svn checkout #{svn_uri}/#{repos_name}#{"/trunk/" + repos_name unless just_app} #{app_path}`

# Ignore various files (mostly) as per http://wiki.rubyonrails.org/rails/pages/HowtoUseRailsWithSubversion
Dir.chdir(app_path)
puts 'Removing and ignoring log files from repository'
`svn remove log/*`
`svn commit -m 'Removing log files from repository'`
`svn propset svn:ignore "*.log" log/`
`svn update log/`
`svn commit -m 'Ignoring all files in /log/ ending in .log'`

puts 'Ignoring all files in tmp/ from repository'
## Un-comment for rails < 1.1
#`svn propset svn:ignore "*" tmp/sessions tmp/cache tmp/sockets`
#`svn commit -m 'Ignoring all files in tmp/*'`
## Comment for rails >= 1.1
`svn remove tmp/*`
`svn propset svn:ignore "*" tmp/`
`svn commit -m "Ignoring all files in tmp/*"`
##

puts 'Backing up and ignoring database.yml'
`svn move config/database.yml config/database.yml.example`
`svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'`
`svn propset svn:ignore "database.yml" config/`
`svn update config/`
`svn commit -m 'Ignoring database.yml'`

puts 'Done.'

#puts 'app_path   - ' + app_path
#puts 'svn_uri    - ' + svn_uri
#puts 'just_app   - ' + (just_app.to_s || "false")
#puts 'repos_name - ' + (repos_name || "blank")

