# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"

set :repo_url, 'git@github.com:joeycrowle/zissou.bedrock.git'
set :stages, %w[dev]
set :default_stage, "dev"
set :ssh_options, {:forward_agent => true}
# Hardcodes branch to always be master
# This could be overridden in a stage config file
set :branch, :master
# Use :debug for more verbose output when troubleshooting
set :log_level, :info
# Apache users with .htaccess files:
# it needs to be added to linked_files so it persists across deploys:
# set :linked_files, fetch(:linked_files, []).push('.env', 'web/.htaccess')
set :linked_files, fetch(:linked_files, []).push('.env', 'web/.htaccess')
set :linked_dirs, fetch(:linked_dirs, []).push('web/app/uploads')
namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
       execute :service, :apache, :reload
    end
  end
end
# The above restart task is not run by default
# Uncomment the following line to run it on deploys if needed
# after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
  desc 'Update WordPress template root paths to point to the new release'
  task :update_option_paths do
    on roles(:app) do
      within fetch(:release_path) do
        if test :wp, :core, 'is-installed'
          [:stylesheet_root, :template_root].each do |option|
            # Only change the value if it's an absolute path
            # i.e. The relative path "/themes" must remain unchanged
            # Also, the option might not be set, in which case we leave it like that
            value = capture :wp, :option, :get, option, raise_on_non_zero_exit: false
            if value != '' && value != '/themes'
              execute :wp, :option, :set, option, fetch(:release_path).join('web/wp/wp-content/themes')
            end
          end
        end
      end
    end
  end
end
namespace :deploy do
  desc 'Composer Install'
  task :install do
    on roles (:app) do

      #execute "cd #{release_path} && curl -s https://getcomposer.org/installer | php -d allow_url_fopen=1 -d suhosin.executor.include.whitelist=phar"
      execute "cd #{release_path} && php -d memory_limit=512M -d allow_url_fopen=1 -d suhosin.executor.include.whitelist=phar composer.phar install --ignore-platform-reqs"
      execute "cd #{release_path}/web/app/themes/ && git clone git@github.com:joeycrowle/zissou.sage.git zissou"
      system("/Applications/AMPPS/www/zissou/config/deploy/dist.sh")
    end
  end
end
# The above update_option_paths task is not run by default
# Note that you need to have WP-CLI installed on your server
# Uncomment the following line to run it on deploys if needed
after 'deploy:publishing', 'deploy:install'
