// main script name
manifest {
    author = 'Robert A. Petit III'
    name = 'ismapper'
    homePage = 'https://github.com/bactopia/bactopia'
    description = 'Identify transposase insertion sites in bacterial genomes with ISMapper'
    mainScript = 'main.nf'
    version = '1.6.1'
    nextflowVersion = '>=19'
}

// Container/Conda env version
container_version = '1.6.x'

// Includes
includeConfig "${baseDir}/conf/params.config"

// Set up containers
docker_repo = params.registry == "github" ? "ghcr.io/bactopia" : params.registry == "quay" ? "quay.io/bactopia" : "bactopia"
docker_repo = params.aws_ecr_registry ? params.aws_ecr_registry : docker_repo
singularity_cache = System.getenv("NXF_SINGULARITY_CACHEDIR") ? "${NXF_SINGULARITY_CACHEDIR}" : "${params.singularity_cache}"

includeConfig "${baseDir}/conf/base.config"
includeConfig "${baseDir}/conf/profiles.config"


if (params.nfconfig) {
    includeConfig check_path(params.nfconfig)
}

// Reporting configuration
timeline {
    enabled = true
    file = "${params.outdir}/bactopia-tools/${manifest.name}/${params.prefix}/bactopia-info/${manifest.name}-timeline.html"
}

report {
    enabled = true
    file = "${params.outdir}/bactopia-tools/${manifest.name}/${params.prefix}/bactopia-info/${manifest.name}-report.html"
}

trace {
    enabled = true
    file = "${params.outdir}/bactopia-tools/${manifest.name}/${params.prefix}/bactopia-info/${manifest.name}-trace.txt"
    fields = 'task_id,hash,native_id,process,tag,name,status,exit,module,container,cpus,time,disk,memory,attempt,start,complete,duration,realtime,queue,%cpu,%mem,rss,vmem'
}


// Function to ensure that resource requirements don't go beyond a maximum limit
// Source: https://github.com/nf-core/rnaseq/blob/master/nextflow.config
def check_max(obj, type) {
    if (type == 'memory') {
        try {
            max_memory = (params.max_memory).GB
            if (obj.compareTo(max_memory as nextflow.util.MemoryUnit) == 1)
                return max_memory as nextflow.util.MemoryUnit
            else
                return obj
        } catch (all) {
            println "ERROR - Max memory '${params.max_memory} GB' is not valid! Using default value: $obj"
            return obj
        }
    } else if (type == 'time') {
        try {
            max_time = (params.max_time).m
            if (obj.compareTo(max_time as nextflow.util.Duration) == 1)
                return max_time as nextflow.util.Duration
            else
            return obj
        } catch (all) {
            println "ERROR - Max time '${params.max_time} minutes' is not valid! Using default value: $obj"
            return obj
        }
    } else if (type == 'cpus') {
        try {
            return Math.min(obj, params.cpus as int)
        } catch (all) {
            println "ERROR - Max cpus '${params.cpus}' is not valid! Using default value: $obj"
            return obj
        }
    }
}

def check_path(file_path) {
    // Try relative first
    launchDir = System.properties['user.dir']
    relative_path = "${launchDir}/${file_path}"
    File file_obj = new File(relative_path)
    if (file_obj.exists()) {
        return relative_path
    } else {
        // Try as absolute path
        file_obj = new File(file_path)
        if (file_obj.exists()) {
            return file_path
        } else {
            println "ERROR - Unable to locate '${params.nfconfig}' please check it exists"
        }
    }
}
