# frozen_string_literal: true

namespace :load do
  task :defaults do
    set :drupal_root, 'web'
  end
end

namespace :composer do
  task :install do
    on roles(:app) do
      within release_path do
        stage = fetch(:stage).to_s
        devel_enabled = %w[demo dev].include?(stage) ? '--dev' : '--no-dev'
        execute :composer, 'install', '--no-progress', '--no-interaction', '--prefer-dist', devel_enabled
      end
    end
  end
end

namespace :drupal do
  task :set_paths do
    SSHKit.config.command_map[:drupal] = "#{fetch(:php_path, '/usr/bin/php')} -d memory_limit=-1 #{shared_path}/vendor/bin/drupal"
    SSHKit.config.command_map[:drush] = "#{fetch(:php_path, '/usr/bin/php')} -d memory_limit=-1 #{shared_path}/vendor/bin/drush"
    SSHKit.config.command_map[:composer] = "#{fetch(:php_path, '/usr/bin/php')} -d memory_limit=-1 -d allow_url_fopen=1 #{release_path}/composer.phar"
  end

  namespace :site do
    task :backup do
      # TODO: Rewrite me when https://www.drupal.org/project/backup_migrate/issues/2968766 closed
      on roles(:app) do
        within release_path.join(fetch(:drupal_root)) do
          execute :drush, 'eval', %{'backup_migrate_perform_backup("default_db", "private_files", []);'}
        end
      end
    end

    task :setup do
      invoke 'drupal:set_paths'

      on roles(:app) do
        within release_path.join(fetch(:drupal_root)) do
          execute :drush, 'site:install', "--root=#{current_path.join(fetch(:drupal_root))}", '--locale=hu', '--account-name=sevadmin', '--account-pass=egyesek11', '--account-mail=hirek@gysev.hu', '--site-mail=hirek@gysev.hu', '-y', 'macroweb_drupal_skeleton'
          execute :drupal, 'config:override', '-y', 'system.site', '--key=uuid', '--value=d6536f41-f103-4891-8a3e-f62b8fddac6c'
          execute :drupal, 'config:override', '-y', 'language.entity.hu', '--key=uuid', '--value=30305211-be40-46d2-a057-e3e522580ee8'
          execute :drush, 'config:import', "--root=#{release_path.join(fetch(:drupal_root))}", '-y'
          # execute :drush,  'locale:import:all', "--root=#{release_path.join(fetch(:drupal_root))}", "profiles/custom/macroweb_drupal_skeleton/translations", "--override=not-customized", "--type=not-customized"
        end
      end
    end

    task :offline do
      on roles(:app) do
        if test("[[ -d #{current_path} -a -f #{shared_path.join('vendor/bin/drush')} ]]")
          within current_path.join(fetch(:drupal_root)) do
            execute :drush, 'sset system.maintenance_mode', 'TRUE'
          end
        end
      end
    end

    task :online do
      on roles(:app) do
        within release_path.join(fetch(:drupal_root)) do
          execute :drush, 'sset system.maintenance_mode', 'FALSE'
        end
      end
    end
  end

  task :update do
    on roles(:app) do
      within release_path.join(fetch(:drupal_root)) do
        execute :drush, 'updatedb', "--root=#{release_path.join(fetch(:drupal_root))}", '-y'
        execute :chmod, '-R', 'go-w', 'libraries', 'core', 'profiles', 'modules', 'themes'
        # The update sometimes trying to re-enable the site, keep off
        execute :drush, 'sset system.maintenance_mode', 'TRUE'
        execute :drush, 'cache:rebuild'
        execute :drush, 'config:import', "--root=#{release_path.join(fetch(:drupal_root))}", '-y'
        execute :drush,  'locale:check'
        execute :drush,  'locale:update'
        execute :drush,  'locale:import:all', "--root=#{release_path.join(fetch(:drupal_root))}", release_path.join('web/profiles/custom/macroweb_drupal_skeleton/translations'), '--override=all', '--type=customized'
        # %w[hu de].each do |lang|
        #   execute :drush, 'locale:import', "--root=#{release_path.join(fetch(:drupal_root))}", lang, release_path.join('web/profiles/custom/macroweb_drupal_skeleton/translations/#{lang}.po'), "--override=not-customized", "--type=not-customized"
        # end
        execute :drush, 'cache:rebuild'
        execute :drush, 'config:import', "--root=#{release_path.join(fetch(:drupal_root))}", '-y'
        execute :drush, 'sset system.maintenance_mode', 'TRUE'

        confsplit_profile = 'config_' + fetch(:stage).to_s[0..3]

        execute :drush, 'config-split:import', confsplit_profile, '-y'
        # Case: https://atrium.macroweb.hu/gysev/node/40584#comment-202516
        #execute :drush, 'colorbox-dompurify'
        execute :drush, 'sset system.maintenance_mode', 'FALSE'
      end
    end
  end

  namespace :cache do
    task :clear do
      on roles(:app) do
        within release_path.join(fetch(:drupal_root)) do
          execute :drush, 'cache:rebuild'
        end
      end
    end
  end

  namespace :symlink do
    task :htdocs do
      on roles(:app) do
        execute :rm, '-f', deploy_path.join('htdocs')
        execute :ln, '-sf', release_path, deploy_path.join('htdocs')
      end
    end

    task :htaccess do
      on roles(:app) do
        execute :rm, '-f', release_path.join('web/.htaccess')
        execute :ln, '-sf', release_path.join('htaccess.txt'), release_path.join('web/.htaccess')
      end
    end
  end

  namespace :cleanup do
    task :unharden do
      on roles(:app) do
        real_current = capture(:readlink, current_path)
        dirs = capture(:ls, '-xd', releases_path.join('*/web/sites/default')).split.find_all { |dir| !dir.match(real_current) }
        dirs.each do |default_dir|
          execute :chmod, 'u+w', default_dir
        end
      end
    end

    task :harden do
      on roles(:app) do
        real_current = capture(:readlink, current_path)
        dirs = capture(:ls, '-xd', releases_path.join('*/web/sites/default')).split.find_all { |dir| !dir.match(real_current) }
        dirs.each do |default_dir|
          execute :chmod, 'u-w', default_dir
        end
      end
    end
  end
end

after 'deploy:updating', 'drupal:set_paths'
before 'deploy:updated', 'drupal:site:offline'
before 'deploy:updated', 'composer:install'
# before 'deploy:updated', 'drupal:site:backup'
after 'deploy:updated', 'drupal:update'
after 'deploy:published', 'drupal:site:online'
after 'deploy:published', 'drupal:cache:clear'
before 'deploy:cleanup', 'drupal:cleanup:unharden'
after 'deploy:cleanup', 'drupal:cleanup:harden'
