/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.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.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: BASES2FASTQ {
        ext.args = [ "--strict",
                    params.trim_fastq ? "--settings 'R1AdapterTrim,true' --settings 'R2AdapterTrim,true'" : "",
        ].join(" ").trim()
        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 },
                pattern: "*.{html,json}"
            ],
            [
                // if ! params.trim_fastq, only output reports but not trimmed fastq files
                path: { "${params.outdir}/${meta.fcid}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> params.trim_fastq ? filename : null },
                pattern: "*.fastq.gz"
            ]
        ]
    }

    withName: FALCO {
        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'
        ]
    }

    withName: SGDEMUX {
        publishDir = [
            [
                path: { "${params.outdir}/${meta.id}/" },
                mode: params.publish_dir_mode,
                pattern: "output/*.{tsv, fastq.gz}",
                saveAs: { filename -> filename.minus("output/") }
            ],
        ]
    }

    withName: FQTK {
        publishDir = [
            [
                path: { "${params.outdir}/${meta.id}/" },
                mode: params.publish_dir_mode,
                pattern: "output/*.{txt, fq.gz}",
                saveAs: { filename -> filename.minus("output/") }
            ],
        ]
    }
}

