namespace :magento do

namespace :cron do
    desc 'Remove cron'
    task :remove do
        on release_roles :all do
            info "Disabling cron"
            if (fetch(:handle_cron).to_s == "true")
                execute :php, '-f', "#{fetch(:deploy_root)}/magento2/bin/magento", 'cron:remove'
            else
                info "Cron not controlled by capistrano"
            end
        end
    end

    desc 'Install cron'
    task :install do
        on release_roles :all do
            info "Enabling cron"
            if (fetch(:handle_cron).to_s == "true")
                execute :php, '-f', "#{fetch(:deploy_root)}/magento2/bin/magento", 'cron:install', '-d'
            else
                info "Cron not controlled by capistrano"
            end
        end
    end
end

namespace :cache do
    desc 'Flush Magento cache storage'
    task :flush do
        on release_roles :all do
            within release_path do
                execute :php, '-f', 'magento/bin/magento', 'cache:flush'
            end
        end
    end

    desc 'Clean Magento cache by types'
    task :clean do
        on release_roles :all do
            within release_path do
                execute :php, '-f', 'magento/bin/magento', 'cache:clean'
            end
        end
    end

    desc 'Enable all Magento cache'
    task :enable do
        on release_roles :all do
            within release_path do
                if File.exist?("magento/bin/magento")
                    execute :php, '-f', 'magento/bin/magento', 'cache:enable', 'config layout block_html collections reflection db_ddl eav config_integration config_integration_api full_page translate config_webservice'
                end
            end
        end
    end

    desc 'Disable Magento cache'
    task :disable do
        on release_roles :all do
            within release_path do
                execute :php, '-f', 'magento/bin/magento', 'cache:disable'
            end
        end
    end

    desc 'Check Magento cache enabled status'
    task :status do
        on release_roles :all do
            within release_path do
                execute :php, '-f', 'magento/bin/magento', 'cache:status'
            end
        end
    end
end

namespace :composer do
    desc 'Run composer install'
    task :install do
        on release_roles :all do
            within release_path + 'magento/' do
                execute :composer, 'install --prefer-dist --no-interaction --no-dev 2>&1'
                execute :composer, "#{fetch(:composer_requirements)}"

                # Dir should be here if properly setup, but check for it anyways just in case
                if test "[ -d #{release_path}/update ]"
                    execute :composer, 'install --prefer-dist --no-interaction --no-dev -d ./update 2>&1'
                else
                    puts "\e[0;31m    Warning: ./update dir does not exist in repository!\n\e[0m\n"
                end
            end
        end
    end
end

namespace :modules do
    desc 'Make sure modules are enabled'
    task :enable_all do
        on release_roles :staging do
            within release_path + 'magento/' do
                if test("[ -f #{release_path}/magento/app/etc/config.php ]")
                    # Exists, carry on
                else
                    execute :php, '-f', 'bin/magento', "module:enable", "--all"
                end
            end
        end
    end

    desc 'Disable a module'
    task 'disable', :module_name do |t, args|
        on release_roles :staging do
            within release_path + 'magento/' do
                execute :php, '-f', 'bin/magento', "module:disable", args[:module_name]
            end
        end
    end

    desc 'Enable a module'
    task 'enable', :module_name do |t, args|
        on release_roles :staging do
            within release_path + 'magento/' do
                execute :php, '-f', 'bin/magento', "module:enable", args[:module_name], '-f'
                invoke 'magento:setup:upgrade'
                invoke 'magento:setup:static-content:deploy'
            end
        end
    end
end

namespace :setup do

    namespace :config do
        desc 'Set a config item'
        task :set do
            on roles(:all) do
                within release_path + 'magento/' do
                    ask(:parameter, '')
                    ask(:value, '')
                    execute :php, '-f', 'magento/bin/magento', "setup:config:set", "--#{fetch(:parameter)}=#{fetch(:value)}", "--no-interaction"
                end
            end
        end
    end

    desc 'Composer install the deployed version of the composer file'
    task :deployment do
        on roles(:all) do
            within release_path + 'magento/' do
                invoke 'composer:install'
            end
        end
    end

    desc 'Run the Magento upgrade process'
    task :upgrade do
        on release_roles :all do
            within release_path do
                execute :php, '-f', 'magento/bin/magento', 'setup:upgrade', '-n'
            end
        end
    end

    desc 'Sets proper permissions on application'
    task :permissions do
        on release_roles(:staging, :production) do
            within release_path do
                execute :find, release_path, '-type d -print0 | sudo xargs --no-run-if-empty --null --max-procs=0 chmod 770'
                execute :find, release_path, '-type f -print0 | sudo xargs --no-run-if-empty --null --max-procs=0 chmod 660'
                execute :chmod, '-R g+s', release_path
                execute :chmod, '+x', "#{release_path}/magento/bin/magento"
            end
        end

        on release_roles(:testing) do
            within release_path do
                execute :find, release_path, '-type d -print0 | sudo xargs --no-run-if-empty --null --max-procs=0 chmod 777'
                execute :find, release_path, '-type f -print0 | sudo xargs --no-run-if-empty --null --max-procs=0 chmod 666'
                execute :chmod, '-R g+s', release_path
                execute :chmod, '+x', "#{release_path}/magento/bin/magento"
            end
        end
    end

    desc 'Sets proper permissions on application'
    task :ownership do
        on roles(:testing, :production) do
            within release_path do
                execute :sudo, 'chown', "#{fetch(:username)}:#{fetch(:group)}", '-R', "#{release_path}"
                execute :sudo, 'chown', "#{fetch(:username)}:#{fetch(:group)}", '-R', "#{deploy_to}/shared"
            end
        end
    end

    namespace :di do
        desc 'Runs dependency injection compilation routine'
        task :compile do
            on release_roles :all do
                within release_path do
                    execute :php, '-f', 'magento/bin/magento', 'setup:di:compile'
                end
            end
        end
    end

    namespace 'static-content' do
        desc 'Deploys static view files'
        task :deploy do
            on release_roles :all do
                within release_path do
                    execute :php, '-f', 'magento/bin/magento', 'setup:static-content:deploy -f en_US en_GB -j6'
                end
            end
        end
    end
end

# @TODO these below functions target the current directory which may not exist on first deployment
namespace :maintenance do
    desc 'Enable maintenance mode'
    task :enable do
        on release_roles :all do
            execute :php, '-f', "#{fetch(:deploy_root)}/magento2/bin/magento", 'maintenance:enable'
        end
    end

    desc 'Disable maintenance mode'
    task :disable do
        on release_roles :all do
            execute :php, '-f', "#{fetch(:deploy_root)}/magento2/bin/magento", 'maintenance:disable'
        end
    end

    desc 'Displays maintenance mode status'
    task :status do
        on release_roles :all do
            within current_path do
                execute :php, '-f', 'magento/bin/magento', 'maintenance:status'
            end
        end
    end

    desc 'Sets maintenance mode exempt IPs'
    task 'allow-ips', :ip do |t, args|
        on release_roles :all do
            within current_path do
                execute :php, '-f', 'magento/bin/magento', 'maintenance:allow-ips', args[:ip]
            end
        end
    end
end

namespace :indexer do
    desc 'Reindex data by all indexers'
    task :reindex do
        on release_roles :all do
            within release_path do
                execute :php, '-f', 'magento/bin/magento', 'indexer:reindex'
            end
        end
    end
end

namespace :patches do
    desc 'Run the ece-patch tool if the .magento.env.yaml file exists'
    task :apply do
        on release_roles :all do
            within release_path do
                if test("[ -f #{release_path}/magento/.magento.env.yaml ]")
                    info 'Applying patches'
                    execute :php, '-f', 'magento/vendor/bin/ece-patches', 'apply --no-interaction'
                else
                    info 'No patch file found, skipping.'
                end
            end
        end
    end

    desc 'Revert patches if the .magento.env.yaml files exists'
    task :revert do
        on release_roles :all do
            within release_path do
                if test("[ -f #{release_path}/magento/.magento.env.yaml ]")
                    info 'Reverting patches'
                    execute :php, '-f', 'magento/vendor/bin/ece-patches', 'revert --no-interaction'
                else
                    info 'No patch file found, skipping.'
                end
            end
        end
    end
end

end