<?php
namespace Deployer;

require 'recipe/laravel.php';

// Project name
set('application', 'lotok-api');

// Project repository
set('repository', 'git@bitbucket.org:empat_tech/lotok-api.git');

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

// Shared files/dirs between deploys
add('shared_files', []);
add('shared_dirs', []);

// Writable dirs by web server
add('writable_dirs', []);
set('allow_anonymous_stats', false);

set('composer_options', 'install --verbose --prefer-dist --no-progress --no-interaction --optimize-autoloader --no-suggest');

// Hosts

host('lotok')
    ->set('deploy_path', '/var/www/lotok-api/staging')
    ->set('branch', 'dev')
    ->set('crontab', true)
    ->set('rabbit', true)
    ->multiplexing(false)
    ->configFile('C:\Users\babik\.ssh\config');

host('lotok-staging')
    ->set('deploy_path', '/var/www/lotok-api/staging')
    ->set('branch', 'master')
    ->set('crontab', true)
    ->set('rabbit', true)
    ->multiplexing(false)
    ->configFile('C:\Users\babik\.ssh\config');

host('lotok-prod')
    ->set('deploy_path', '/var/www/lotok-api/production')
    ->set('branch', 'master')
    ->set('crontab', true)
    ->set('rabbit', true)
    ->multiplexing(false)
    ->configFile('C:\Users\babik\.ssh\config');

// Tasks

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

task('artisan:migrate:force', function() {
    run('{{bin/php}} {{release_path}}/artisan migrate --force');
});

desc('Restart php-fpm');
task('fpm:restart', function () {
    run('sudo systemctl restart php7.4-fpm.service');
});

desc('Stop horizon supervisor');
task('supervisor:stop', function () {
    $host = get('hostname');

    if($host === 'lotok-prod') {
        $output = run('sudo supervisorctl stop horizon_production');
    } else {
        $output = run('sudo supervisorctl stop horizon_staging');
    }

    if($output === 'horizon: ERROR (not running)') {
        writeln('<error>Supervisor already stopped</error>');
    } else {
        writeln('<info>Successful stopped supervisor</info>');
    }
});

desc('Start horizon supervisor');
task('supervisor:start', function () {
    $host = get('hostname');

    if($host === 'lotok-prod') {
        $output = run('sudo supervisorctl start horizon_production');
    } else {
        $output = run('sudo supervisorctl start horizon_staging');
    }

    if($output === 'horizon: ERROR (already started)') {
        writeln('<error>Supervisor already started</error>');
    } else {
        writeln('<info>Successful started supervisor</info>');
    }
});

desc('Stop crontab task');
task('crontab:stop', function () {
    try {
        $output = run('crontab -r');

        writeln('<info>Successful stopped cron</info>');
    } catch (\Deployer\Exception\RuntimeException $e) {
        writeln('<error>Crontab already stopped</error>');
    }
});

desc('Start crontab task');
task('crontab:start', function () {
    $crontab = get('crontab');

    if($crontab) {
        $output = run('crontab ~/crontask');

        if($output === '') {
            writeln('<info>Successful started cron</info>');
        }
    } else {
        writeln('<info>Crontab is disabled for this server</info>');
    }
});

desc('Stop RabbitMq listener');
task('rabbit:stop', function() {
    $host = get('hostname');

    if($host === 'lotok-prod') {
        $output = run('sudo supervisorctl stop rabbit_production');
    } else {
        $output = run('sudo supervisorctl stop rabbit_staging');
    }

    if($output === 'horizon: ERROR (not running)') {
        writeln('<error>RabbitMq listener already stopped</error>');
    } else {
        writeln('<info>Successful stopped RabbitMq listener</info>');
    }
});

desc('Start RabbitMq listener');
task('rabbit:start', function() {
    $host = get('hostname');
    $rabbit = get('rabbit');

    if($rabbit) {
        if($host === 'lotok-prod') {
            $output = run('sudo supervisorctl start rabbit_production');
        } else {
            $output = run('sudo supervisorctl start rabbit_staging');
        }

        if($output === 'horizon: ERROR (not running)') {
            writeln('<error>RabbitMq listener already started</error>');
        } else {
            writeln('<info>Successful started RabbitMq listener</info>');
        }
    } else {
        writeln('<info>Rabbit listener is disabled for this server</info>');
    }
});

desc('Create public directories in storage');
task('storage:create_directories', function () {
    $commonPath = get('deploy_path') . '/shared/storage/app/public';
    $barcodesPath = '/images/barcodes';

    $isBarcodesDirExists = run("if [ -d '{$commonPath}{$barcodesPath}' ]; then echo 'true'; else echo false; fi");

    if ($isBarcodesDirExists == 'false') {
        writeln("<info>Directory <fg=gray>{$barcodesPath}</> does not exist</info>");
        writeln("<info>Creating <fg=gray>{$barcodesPath}</> directory</info>");
        run("mkdir -p '{$commonPath}{$barcodesPath}'");

        $isBarcodesDirExists = run("if [ -d '{$commonPath}{$barcodesPath}' ]; then echo 'true'; else echo false; fi");

        if ($isBarcodesDirExists == 'true') {
            writeln("<info>Directory <fg=gray>{$barcodesPath}</> has been created</info>");
        } else {
            writeln("<error>Directory <fg=gray>{$barcodesPath}</> could not be created</error>");
        }
    }
});

before('deploy:prepare', 'crontab:stop');
after('deploy', 'fpm:restart');
after('fpm:restart', 'supervisor:stop');
after('fpm:restart', 'rabbit:stop');
after('fpm:restart', 'crontab:start');
after('supervisor:stop', 'supervisor:start');
after('rabbit:stop', 'rabbit:start');
before('deploy:symlink', 'artisan:migrate:force');
before('deploy:symlink', 'storage:create_directories');
