require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
#require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
require 'mina/rvm'    # for rvm support. (http://rvm.io)
require 'mina_sidekiq/tasks'
require 'mina/whenever'

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)

set :domain, 'codecuriosity.org'
set :deploy_to, '/home/deploy/projects/codecuriosity'
#set :repository, 'git@github.com:joshsoftware/code-curiosity.git'
set :repository, 'https://github.com/joshsoftware/code-curiosity.git'
set :user, 'deploy'
# set :ssh_options, '-A'
set :rail_env, 'production'
set :keep_releases, 5
set :branch, 'master'

# For system-wide RVM install.
set :rvm_path, '/usr/local/rvm/bin/rvm'

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, [
  'config/mongoid.yml', 'config/secrets.yml', 'log',
  'config/git.yml', '.env.production', 'repositories',
  'cache', 'config/backup.yml', 'config/badge.yml', 'config/points.yml',
  'app/models/repo_budget.rb', 'app/models/commit_score.rb'
]

# Optional settings:
#   set :user, 'foobar'    # Username in the server to SSH to.
#   set :port, '30000'     # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  invoke :'rvm:use[ruby-2.7.1]'
end

# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/cache"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/cache"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]

  queue! %[touch "#{deploy_to}/#{shared_path}/config/mongoid.yml"]
  queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
  queue! %[touch "#{deploy_to}/#{shared_path}/.env.production"]

  queue! %[mkdir -p "#{deploy_to}/shared/pids/"]
  queue! %[mkdir -p "#{deploy_to}/shared/repositories/"]
  queue  %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/mongoid.yml' and 'secrets.yml'."]
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  to :before_hook do
    # Put things to run locally before ssh
  end
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:assets_precompile'
    unless ENV['only'] == 'web'
      invoke :'sidekiq:quiet'
    end
    invoke :'deploy:cleanup'

    to :launch do
      queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
      queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"

      unless ENV['only'] == 'web'
        invoke :'sidekiq:restart'
        invoke :'whenever:clear'
        invoke :'whenever:write'
      end
      #queue 'sudo monit restart cksidekiq'
    end
  end
end

desc "db backup"
task :db_backup => :environment do
  home = "/home/#{user}/db_dump"
  name = "mongodump-#{Time.now.strftime('%F')}"

  queue "mongodump --db codecuriosity_#{rail_env} --out #{home}/#{name}"
  queue "cd #{home}; tar -cvf #{name}.tar.gz #{name}/"
  queue "rm -rf #{home}/#{name}"
  queue "echo 'path : #{home}/#{name}.tar.gz'"
end

# For help in making your deploy script, see the Mina documentation:
#
#  - http://nadarei.co/mina
#  - http://nadarei.co/mina/tasks
#  - http://nadarei.co/mina/settings
#  - http://nadarei.co/mina/helpers
