/*
========================================================================================
    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.
----------------------------------------------------------------------------------------
*/

//
// Generic process options for all workflows
//
process {

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

    withName: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: 'copy',
            pattern: '*_versions.yml'
        ]
    }

}

//
//  Process options for the SRA workflow
//
if (params.input_type == 'sra') {

    process {

        withName: SRA_IDS_TO_RUNINFO {
            publishDir = [
                path: { "${params.outdir}/metadata" },
                enabled: false
            ]
        }

        withName: SRA_RUNINFO_TO_FTP {
            publishDir = [
                path: { "${params.outdir}/metadata" },
                mode: 'copy',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: SRA_FASTQ_FTP {
            ext.args = '--retry 5 --continue-at - --max-time 1200'
            publishDir = [
                [
                    path: { "${params.outdir}/fastq" },
                    mode: 'copy',
                    pattern: "*gz"
                ],
                [
                    path: { "${params.outdir}/fastq/md5" },
                    mode: 'copy',
                    pattern: "*.md5"
                ]
            ]
        }

        withName: SRATOOLS_PREFETCH {
            publishDir = [
                path: { "${params.outdir}/sra" },
                enabled: false
            ]
        }

        withName: SRATOOLS_FASTERQDUMP {
            publishDir = [
                [
                    path: { "${params.outdir}/fastq" },
                    mode: 'copy',
                    pattern: "*gz"
                ],
                [
                    path: { "${params.outdir}/fastq/md5" },
                    mode: 'copy',
                    pattern: "*.md5"
                ]
            ]
        }

        withName: SRA_TO_SAMPLESHEET {
            publishDir = [
                path: { "${params.outdir}/samplesheet" },
                enabled: false
            ]
        }

        withName: SRA_MERGE_SAMPLESHEET {
            publishDir = [
                path: { "${params.outdir}/samplesheet" },
                mode: 'copy',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: MULTIQC_MAPPINGS_CONFIG {
            publishDir = [
                path: { "${params.outdir}/samplesheet" },
                mode: 'copy',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

    }

}

//
//  Process options for the Synapse workflow
//
if (params.input_type == 'synapse') {

    process {

        withName: SYNAPSE_LIST {
            ext.args = '--long'
            publishDir = [
                path: { "${params.outdir}/metadata" },
                mode: 'copy',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: SYNAPSE_GET {
            publishDir = [
                [
                    path: { "${params.outdir}/fastq" },
                    mode: 'copy',
                    pattern: "*gz"
                ],
                [
                    path: { "${params.outdir}/fastq/md5" },
                    mode: 'copy',
                    pattern: "*.md5"
                ]
            ]
        }

        withName: SYNAPSE_SHOW {
            publishDir = [
                path: { "${params.outdir}/metadata" },
                mode: 'copy',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: SYNAPSE_TO_SAMPLESHEET {
            publishDir = [
                path: { "${params.outdir}/samplesheet" },
                enabled: false
            ]
        }

        withName: SYNAPSE_MERGE_SAMPLESHEET {
            publishDir = [
                path: { "${params.outdir}/samplesheet" },
                mode: 'copy',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

    }

}
