###############################################################################
# Copyright 2012 - 2013 American Pharma Technologies LLC. All rights reserved.
#
# Capistrano deployment recipe for pharmawatch front-end
#
# DB PROCESSOR ENGINE
###############################################################################

# set the deployment sources
DEPLOYMENT = %w(local working hotfix main)
DEST_SERVER = %w(apdb2 aparch apdbst apdbmr csdb csarch)

# forward declare crontabs
set :crontabs, []

#==============================================================================
# set the Shared code path
#
# This loads the users path from their local capistrano/user_shared_code_path.rb
# file. This file is not included in source control so it can be different for
# each user. 
#==============================================================================
task :set_shared_code_path do
  load "capistrano/user_shared_code_path"
  branch = Pathname.new(Dir.getwd).basename.to_s
  share = "#{shared_code_root}#{branch}/capistrano/"
  avail = File.directory? share
  if avail==false
    abort "ERROR: the shared code directory is not available at: #{share}."
  end
  set :shared_code_path, share
  puts "STEP 1: Set shared code path to: #{shared_code_path}"
end
before DEPLOYMENT, 'set_shared_code_path'

#==============================================================================
# load shared recipes
#==============================================================================
task :load_shared_code do
  load "#{shared_code_path}deploy/recipes/base"
  load "#{shared_code_path}deploy/recipes/configure"
  load "#{shared_code_path}deploy/recipes/database"
  load "#{shared_code_path}deploy/recipes/nginx"
  load "#{shared_code_path}deploy/recipes/apache"
  load "#{shared_code_path}deploy/recipes/cron"
  puts "STEP 2: Loaded shared code"
end
after 'set_shared_code_path', 'load_shared_code'

#==============================================================================
# load the specific branch setup file. this loads the variables
# to drive the push
#==============================================================================
DEPLOYMENT.each do |name|
  desc "Set the source to `#{name}` for deployment."
  task(name) do
    set :deployment, name.to_sym
    load "#{shared_code_path}deploy/all"
    load "#{shared_code_path}deploy/#{deployment}"
    load "capistrano/deploy/#{deployment}"
    puts "STEP 3: Loaded deployment: #{name}"
  end
end

#==============================================================================
# load the specific branch setup file. this loads the variables
# to drive the push
#==============================================================================
DEST_SERVER.each do |name|
  desc "Set the destination server to `#{name}`."
  task(name) do
    set :dest_server, name.to_sym
    load "#{shared_code_path}deploy/servers/#{dest_server}"
    puts "STEP 4: Loaded Destination Server: #{name}"
  end
  #after name, 'test_deployment'
end
before 'deploy', 'test_deployment'
before 'deploy:rollback', 'test_deployment'
before 'status', 'test_deployment'
before 'deploy:setup', 'test_deployment'
before 'cron:ci_setup', 'test_deployment'
before 'cron:ci_create_crontabs', 'test_deployment'
before 'cron:ci_set_log_perms', 'test_deployment'

#==============================================================================
# error checking: make sure one of the branch setup files is requested
# before proceeding
#==============================================================================
#on :start, :except => [DEPLOYMENT, DEST_SERVER] do
task :test_deployment do
  if !exists?(:deployment)
    abort "ERROR: no deployment specified, please choose one of #{DEPLOYMENT.join(", ")}"
  else
    puts 'STEP 6: Verified deployment loaded'
    if !exists?(:dest_server)
      abort "ERROR: no destination server specified, please choose one of #{DEST_SERVER.join(", ")}"
    else
      puts 'STEP 7: Verified Destination Server loaded'
      puts 'STEP 8: Review recipe configuration'
      post_warning(recipe_info, true)
      puts 'STEP 9: Run...'
    end
  end
end
before 'test_deployment', 'set_vars'


#==============================================================================
# error checking: make sure one of the branch setup files is requested
# before proceeding
#==============================================================================
#on :start, :except => [DEPLOYMENT, DEST_SERVER] do
#  if !exists?(:dest_server)
#    abort "ERROR: no destination server specified, please choose one of #{DEST_SERVER.join(", ")}"
#  end
#  puts 'STEP 6: Verified Destination Server loaded'
#  puts 'STEP 7: Review recipe configuration'
#  post_warning(recipe_info, true)
#  puts 'STEP 8: Run...'
#end
#==============================================================================
# Conditionals for decisions made--runs before deployment and sets
# many variables needed for the process
#==============================================================================
task :set_vars do

  # determine the source for code pulled from repository
  if branch = variables[:branch] || variables[:b]
    set :tag_or_branch, "branches/#{branch}"
  elsif tag = variables[:tag] || variables[:t]
    set :tag_or_branch, "tags/#{tag}"
  else
    set :tag_or_branch, "#{source_branch}"
  end
  set :application, "DB Processor"
  set :repository,  "svn+ssh://#{user}@svn.#{svn_domain}/svn/dbprocessor/#{tag_or_branch}"

  set :fqdn,  "dbprocessor.#{subdomain}#{domain}"
  set :deploy_to, "#{deploy_path}#{fqdn}"

  set :scm, :subversion
  set :deploy_via, :export

  set :shared_children, %w{}

  default_run_options[:pty] = true
  ssh_options[:forward_agent] = true

  role :web, "#{fetch(:dest_ip_address, fqdn)}"                          # Your HTTP server, Apache/etc
  role :app, "#{fetch(:dest_ip_address, fqdn)}"                          # This may be the same as your `Web` server
  role :db,  "#{fetch(:dest_ip_address, fqdn)}",   :primary => true

  puts 'STEP 5: Set deployment variables'
end

#==============================================================================
# Deployment namespace: all deploy tasks (which talk to remote server) fit
# within this space
#==============================================================================
namespace :deploy do
  desc "Symlink shared configs on each release."
  task :symlink_shared do
    run "#{try_sudo} ln -nfs #{shared_path}/config/index.php #{release_path}/public/index.php"
    run "#{try_sudo} ln -nfs #{shared_path}/config/database.php #{release_path}/dbprocessor/config/database.php"
    run "#{try_sudo} ln -nfs #{shared_path}/config/config.php #{release_path}/dbprocessor/config/config.php"
    #run "#{try_sudo} ln -nfs #{shared_path}/videos #{release_path}/public/videos"
  end
  after 'deploy:update_code', 'deploy:symlink_shared'
end

#==============================================================================
# clean up after a deploying a new website so that there are never more
# than five copies of the website present on the server in the releases folder
#==============================================================================
after 'deploy', 'deploy:cleanup'

#==============================================================================
# show a list of tasks that are in the shared code. these are not visible 
# using the cap -T ouput
#==============================================================================
desc "Show a list of shared code tasks"
task :list_shared_tasks do
  puts 'DISPLAY SHARED CODE TASKS'
end

#==============================================================================
# show a list of tasks that are in the shared code. these are not visible
# using the cap -T ouput
#==============================================================================
desc "Shortcut: Show a list of shared code tasks"
task :LST do
end
after 'LST', 'list_shared_tasks'

#==============================================================================
# show a list of tasks that are in the shared code. these are not visible
# using the cap -T ouput
#==============================================================================
desc "Shortcut: Show a list of shared code tasks"
task :lst do
end
after 'lst', 'list_shared_tasks'

#namespace :rvm do
#  task :trust_rvmrc do
#    run "rvm rvmrc trust #{release_path}"
#  end
#end
#after 'deploy', 'rvm:trust_rvmrc'

