/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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 }
    ]

    withName: UNTAR {
        publishDir = [
            enabled: false
        ]
    }

    withName: BCLCONVERT {
        ext.when = { params.demultiplexer == "bclconvert" }
        ext.args = {[
            meta.lane ? "--bcl-only-lane ${meta.lane}" : "",
            "--force",
            "--strict-mode true"
        ].join(" ").trim()}
        publishDir = [
            [
                // Gather and write InterOp files
                path: { "${params.outdir}/${meta.id}/InterOp" },
                mode: params.publish_dir_mode,
                pattern: "**.bin",
                saveAs: {filename -> filename.split("/")[-1] }
            ],
            [
                // Fetch RunInfo.xml
                path: { "${params.outdir}/${meta.id}" },
                mode: params.publish_dir_mode,
                pattern: "RunInfo.xml",
                overwrite: true,
                saveAs: {filename -> filename.split("/")[-1] }
            ],
            [
                // Gather and write Reports
                path: { meta.lane ? "${params.outdir}/${meta.id}/L00${meta.lane}" : "${params.outdir}/${meta.id}" },
                mode: params.publish_dir_mode,
                pattern: "Reports",
                saveAs: {filename -> filename.split("/")[-1] }
            ],
            [
                path: { "${params.outdir}/${meta.id}" },
                pattern: "**.fastq.gz",
            ]
        ]
    }

    withName: BCL2FASTQ {
        ext.when = { params.demultiplexer == "bcl2fastq" }
        ext.args = {[
            meta.lane ? "--tiles s_${meta.lane}" : "",
        ].join(" ").trim()}
        publishDir = [
            [
                // Gather and write InterOp files
                path: { "${params.outdir}/${meta.id}/InterOp" },
                mode: params.publish_dir_mode,
                pattern: "**.bin",
                saveAs: {filename -> filename.split("/")[-1] }
            ],
            [
                // Fetch RunInfo.xml
                path: { "${params.outdir}/${meta.id}" },
                mode: params.publish_dir_mode,
                pattern: "RunInfo.xml",
                overwrite: true,
                saveAs: {filename -> filename.split("/")[-1] }
            ],
            [
                // Gather and write Reports and Stats
                path: { meta.lane ? "${params.outdir}/${meta.id}/L00${meta.lane}" : "${params.outdir}/${meta.id}" },
                mode: params.publish_dir_mode,
                pattern: "{Reports, Stats}",
                saveAs: {filename -> filename.split("/")[-1] }
            ],
            [
                path: { "${params.outdir}/${meta.id}" },
                pattern: "**.fastq.gz",
            ]
        ]
    }

    withName: CELLRANGER_MKFASTQ {
        ext.when = { params.demultiplexer == "cellranger" }
        publishDir = [
            path: { "${params.outdir}/fastq" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }

    withName: BASES2FASTQ {
        ext.when = { params.demultiplexer == "bases2fastq" }
        ext.args = "--strict"
        publishDir = [
            [
                path: { "${params.outdir}/${meta.id}/" },
                mode: params.publish_dir_mode,
                pattern: "output/*.{json,csv,html,log}",
                saveAs: { filename -> filename.minus("output/") }
            ],
            [
                path: { "${params.outdir}/${meta.id}/" },
                mode: params.publish_dir_mode,
                pattern: "output/Samples/*/*.{json,fastq.gz}",
                saveAs: { filename -> filename.minus("output/") }
            ],
            [
                path: { "${params.outdir}/${meta.id}/" },
                mode: params.publish_dir_mode,
                pattern: "output/info/*.{log}",
                saveAs: { filename -> filename.split("/")[-1] }
            ]
        ]
    }

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

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

    withName: MD5SUM {
        publishDir = [
            path: { "${params.outdir}/${meta.fcid}" },
            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'
        ]
    }

}
