process {
  withLabel: lowmem { memory = 13.Gb }
  withLabel: lowcpu { cpus = 4 }
  withLabel: midmem { memory = 13.Gb }
  withLabel: midcpu { cpus = 4 }
  withLabel: highmem { memory = 13.Gb }
  withLabel: highcpu { cpus = 4 }
  withLabel: veryhighmem { memory = 13.Gb }
  withLabel: lowdisk {
    disk = {process.disk ? process.disk : null}
  }
  withLabel: middisk {
    disk = {process.disk ? process.disk : null}
  }
  withLabel: highdisk {
    disk = {process.disk ? process.disk : null}
  }
  withLabel: veryhighdisk {
    disk = {process.disk ? process.disk : null}
  }
}

env.NUMBA_CACHE_DIR = '/tmp'

trace {
    enabled = true
    overwrite = true
}
dag {
  overwrite = true
}

process.maxForks = 1

profiles {
  // detect tempdir
  tempDir = java.nio.file.Paths.get(
    System.getenv('NXF_TEMP') ?:
      System.getenv('VIASH_TEMP') ?: 
      System.getenv('TEMPDIR') ?: 
      System.getenv('TMPDIR') ?: 
      '/tmp'
  ).toAbsolutePath()

  mount_temp {
    docker.temp            = tempDir
    podman.temp            = tempDir
    charliecloud.temp      = tempDir
  }

  no_publish {
    process {
      withName: '.*' {
        publishDir = [
          enabled: false
        ]
      }
    }
  }

  docker {
    docker.fixOwnership    = true
    docker.enabled         = true
    singularity.enabled    = false
    podman.enabled         = false
    shifter.enabled        = false
    charliecloud.enabled   = false
  }

  local {
    // This config is for local processing.
    process {
        maxMemory = 25.GB
        withLabel: verylowcpu { cpus = 2 }
        withLabel: lowcpu { cpus = 4 }
        withLabel: midcpu { cpus = 6 }
        withLabel: highcpu { cpus = 12 }
  
        withLabel: lowmem { memory = { get_memory( 8.GB * task.attempt ) } }
        withLabel: midmem { memory = { get_memory( 12.GB * task.attempt ) } }
        withLabel: highmem { memory = { get_memory( 20.GB * task.attempt ) } }
    }
  }
}

def get_memory(to_compare) {
  if (!process.containsKey("maxMemory") || !process.maxMemory) {
    return to_compare
  }

  try {
    if (process.containsKey("maxRetries") && process.maxRetries && task.attempt == (process.maxRetries as int)) {
      return process.maxMemory
    }
    else if (to_compare.compareTo(process.maxMemory as nextflow.util.MemoryUnit) == 1) {
      return max_memory as nextflow.util.MemoryUnit
    }
    else {
      return to_compare
    }  
  } catch (all) {
        println "Error processing memory resources. Please check that process.maxMemory '${process.maxMemory}' and process.maxRetries '${process.maxRetries}' are valid!"
        System.exit(1)
  }
}
