<?php

namespace Deployer;

require 'recipe/drupal8.php';

set('project_name', 'finantsinspketsioon');

// Project repository
set('repository', 'git@bitbucket.org:adm-nova/finantsinspektsioon.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);

// Shared files/dirs between deploys
set('shared_files', [
  'web/sites/default/settings.local.php',
]);
// List of shared dirs.
set('shared_dirs', [
  'web/sites/default/files',
  'config/excluded',
  'web/temp_web_content',
  'private',
  'resources',
  'node_modules',
  'web/failid',
  'web/koond2',
  'web/schemas',
  'web/source_data'
]);
// List of files to copy between release.
set('copy_dirs', [
  'web/sites/default/files',
  'config/excluded',
  'web/temp_web_content',
  'private',
  'resources',
  'node_modules',
  'web/failid',
  'web/koond2',
  'web/schemas',
  'web/source_data'
]);

// Hosts
host('YOUR-HOST-NICKNAME')
  ->stage('prod')
  ->forwardAgent(false)
  ->set('http_user', 'apache')
  ->set('backup_path', '/var/www/html/drupal_backups')
  ->set('deploy_path', '/var/www/html/drupal_releases');

// Tasks
task('upload', function () {
  writeln('Uploading vendor');
  run('mkdir {{release_path}}/vendor');
  upload(__DIR__ . "/vendor", '{{release_path}}');
  writeln('Uploaded vendor');

  writeln('Uploading core');
  run('mkdir {{release_path}}/web/core');
  upload(__DIR__ . "/web/core", '{{release_path}}/web');
  writeln('Uploaded core');

  writeln('Uploading contrib');
  run('mkdir {{release_path}}/web/modules/contrib');
  upload(__DIR__ . "/web/modules/contrib", '{{release_path}}/web/modules');
  writeln('Uploaded contrib');
});

task('enable_drush', function() {
  run('{{release_path}}/vendor/drush/drush/drush init');
});

task('create_backup', function() {
   $date = date('Y-m-d-H-i');
    cd('{{deploy_path}}/current');
    run("vendor/drush/drush/drush --root=web sql-dump --gzip > {{backup_path}}/{{project_name}}-$date.sql.gz");
    writeln("Created backup on $date");
})->onStage('prod');

task('change_old_release_file_permissions', function() {
  $releases = get('releases_list');
  $keep = get('keep_releases');
  $sudo = get('cleanup_use_sudo') ? 'sudo' : '';
  $runOpts = [];
  if ($sudo) {
    $runOpts['tty'] = get('cleanup_tty', false);
  }
  if ($keep === -1) {
    // Keep unlimited releases.
    return;
  }
  while ($keep > 0) {
    array_shift($releases);
    --$keep;
  }
  foreach ($releases as $release) {
    // These are old releases, not available for public
    // and are removed in the cleanup task.
    run("chmod -R 777 {{deploy_path}}/releases/$release/web/sites/default", $runOpts);
  }
});

task('change_ownership', function() {
  run('chown YOUR-USER:apache -R {{deploy_path}}');
  writeln('Ownership changed');
})->onStage('prod');


task('release', [
  'deploy:prepare',
  'deploy:release',
  'deploy:update_code',
  'upload',
  'deploy:shared',
  'deploy:writable',
  'enable_drush',
  'deploy:symlink',
  'deploy:unlock',
]);

task('deploy', [
  'create_backup',
  'release',
  'change_old_release_file_permissions',
  'change_ownership',
  'cleanup',
  'success',
]);

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

