# -*- encoding : utf-8 -*-
set :application, 'pic_vert_app' 
	set :domain, 'www2.lepicvert.asso.fr'




	require "rvm/capistrano"

	set :rvm_ruby_string, 'ruby-1.9.3-p0'
	set :rvm_type, :user  

	set :user, 'web'



	set :deploy_to, "/home/#{user}/#{application}/" 
	set :deploy_via, :remote_cache

	set :normalize_asset_timestamps, false


	set :rails_env, "production"



	role :app, domain
	role :web, domain
	role :db, domain, :primary => true

	set :scm, :git
	set :repository,  "git@box1.quartz-agence.com:#{application}.git"
	set :branch, "master"

	default_run_options[:pty] = true 


	
	before "deploy:symlink", "deploy:bundle_gems"
	after "deploy:bundle_gems", "deploy:make_simlinks"
	after "deploy:make_simlinks", "assets:precompile"

	
	
#	after "deploy", "deploy:restart"
	after "deploy:restart", "deploy:cleanup"
	
	
	#after "deploy", "deploy:bundle_gems"
	#after "deploy:bundle_gems", "deploy:make_simlinks"
	#after "deploy:make_simlinks", "assets:precompile"
	#after "assets:precompile", "deploy:restart"
	#after "deploy:restart", "deploy:cleanup"
	

	namespace :assets do
	  task :precompile, :roles => :web do
		run "rm -rf #{release_path}/public/assets"
		run "ln -s #{deploy_to}shared/assets #{release_path}/public/assets"
		
		
	    run "cd #{release_path} && bundle exec rake assets:precompile --trace"
	  end

	  task :cleanup, :roles => :web do
	    run "cd #{current_path} && RAILS_ENV=production bundle exec rake assets:clean"
	  end
	end

	



	namespace :deploy do
		task :bundle_gems do 
			run "cd #{release_path} && bundle install"
		end

		task :make_simlinks do 
			run "rm -rf #{release_path}/private_medias"
			run "ln -s #{deploy_to}shared/storage/private #{release_path}/private_medias"
			run "rm -rf #{release_path}/public/public_medias"
			run "ln -s #{deploy_to}shared/storage/public #{release_path}/public/public_medias"
		end

		task :start do 
			sudo "service #{application} start"
		end
		task :stop do 
			run "service #{application} stop"	
		end
		task :restart, :roles => :app, :except => { :no_release => true } do
			sudo "service #{application} restart"
		end
		task :migrate do 
			run "cd #{deploy_to}current && bundle exec rake db:migrate RAILS_ENV=#{rails_env}"

		end
		task :migrate_redo do 
			run "cd #{deploy_to}current && bundle exec rake db:migrate:redo RAILS_ENV=#{rails_env}"

		end
    
		task :nginx do 
			sudo "service nginx reload"
		end
    
		
		task :seeds do 
			run "cd #{deploy_to}current && rake db:seed RAILS_ENV=#{rails_env}"

		end
    
    
	end
	
