/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: params.publish_dir_mode,
        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
    ]

    withName: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            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: params.publish_dir_mode,
                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: params.publish_dir_mode,
                    pattern: "*.fastq.gz"
                ],
                [
                    path: { "${params.outdir}/fastq/md5" },
                    mode: params.publish_dir_mode,
                    pattern: "*.md5"
                ]
            ]
        }

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

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

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

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

        withName: MULTIQC_MAPPINGS_CONFIG {
            publishDir = [
                path: { "${params.outdir}/samplesheet" },
                mode: params.publish_dir_mode,
                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: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

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

        withName: SYNAPSE_SHOW {
            publishDir = [
                path: { "${params.outdir}/metadata" },
                mode: params.publish_dir_mode,
                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: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

    }

}
