/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Config file for defining DSL2 per module options and publishing paths
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Available keys to override module options:
        ext.args   = Additional arguments appended to command in module.
        ext.args2  = Second set of arguments appended to command in module (multi-tool modules).
        ext.args3  = Third set of arguments appended to command in module (multi-tool modules).
        ext.prefix = File name prefix for output files.
----------------------------------------------------------------------------------------
*/

process {

    publishDir = [
        path:    { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
        mode:    params.publish_dir_mode,
        saveAs:  { filename -> filename.equals('versions.yml') ? null : filename },
        enabled: params.publish_dir_enabled
    ]

    withName: "FORCE_GENERATE_ANALYSIS_MASK" {
        publishDir = [
            path:    { "${params.outdir}/preparation/" },
            mode:    params.publish_dir_mode,
            pattern: '**.tif',
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "FORCE_GENERATE_TILE_ALLOW_LIST" {
        publishDir = [
            path:    { "${params.outdir}/preparation/" },
            pattern: '*.txt',
            mode:    params.publish_dir_mode,
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "FORCE_PREPROCESS" {
        publishDir    = [
            [
                path:    { "${params.outdir}/preprocess/${task.tag}/logs" },
                mode:    params.publish_dir_mode,
                pattern: '**.log',
                enabled: params.publish_dir_enabled
            ],
            [
                path:    { "${params.outdir}/preprocess/${task.tag}" },
                mode:    params.publish_dir_mode,
                pattern: 'level2_ard/**/*',
                saveAs:  { params.save_ard ? it : null },
                enabled: params.publish_dir_enabled
            ]
        ]
    }


    withName: "HIGHER_LEVEL_CONFIG" {
        publishDir    = [
            path:    { "${params.outdir}/higher-level/${task.tag}/param_files" },
            mode:    params.publish_dir_mode,
            pattern: '*.prm',
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "FORCE_HIGHER_LEVEL" {
        publishDir = [
            path:    { "${params.outdir}/higher-level/${task.tag}" },
            mode:    params.publish_dir_mode,
            pattern: 'trend/*.tif',
            saveAs:  { params.save_ard ? "trend_files/${it.tokenize('/')[-1]}" : null },
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "FORCE_PYRAMID" {
        stageInMode = 'copy'
        publishDir  = [
            path:    { "${params.outdir}/trend/pyramid/" },
            saveAs:  { "${it.substring(12,it.indexOf("."))}/trend/${it.substring(0,11)}/$it" },
            pattern: '*.tif*',
            mode:    params.publish_dir_mode,
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "FORCE_MOSAIC" {
        publishDir = [
            path:    { "${params.outdir}/trend/mosaic/" },
            mode:    params.publish_dir_mode,
            saveAs:  { filename -> filename.equals('versions.yml') ? null : "${params.outdir}/trend/mosaic/${task.tag}/${filename.replaceAll("trend/","")}"},
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "CHECK_RESULTS" {
        publishDir    = [
            enabled: false
        ]
    }

    withName: "CHECK_RESULTS_FULL" {
        publishDir    = [
            enabled: false
        ]
    }

    withName: "PREPROCESS_CONFIG" {
        publishDir    = [
            path:    { "${params.outdir}/preprocess/${task.tag}/param_files" },
            mode:    params.publish_dir_mode,
            pattern: '*.prm',
            enabled: params.publish_dir_enabled
        ]
    }

    withName: 'MULTIQC' {
        ext.args   = params.multiqc_title ? "--title \"$params.multiqc_title\"" : ''
        publishDir = [
            path:    { "${params.outdir}/multiqc" },
            mode:    params.publish_dir_mode,
            saveAs:  { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.publish_dir_enabled
        ]
    }

    withName: "UNTAR_*" {
        publishDir = [
            path:    { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode:    'symlink',
            saveAs:  { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.publish_dir_enabled
        ]
    }

}
