# Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *


class Freebayes(MesonPackage):
    """Bayesian haplotype-based genetic polymorphism discovery and
       genotyping."""

    homepage = "https://github.com/ekg/freebayes"
    url      = "https://github.com/freebayes/freebayes/releases/download/v1.3.5/freebayes-1.3.5-src.tar.gz"
    git      = "https://github.com/ekg/freebayes.git"

    version('1.3.5', sha256='7e2635690e916ed85cec36b3263e6e5357413a4f2bf3035362d9749335e8a696')
    version('1.1.0', commit='39e5e4bcb801556141f2da36aba1df5c5c60701f',
            submodules=True, deprecated=True)

    depends_on('cmake', type='build')
    depends_on('zlib')

    # Deps for @1.3.5 and beyond
    depends_on('ninja', type='build', when='@1.3.5:')
    depends_on('htslib', when='@1.3.5:')
    depends_on('zlib', when='@1.3.5:')
    depends_on('xz', when='@1.3.5:')
    depends_on('parallel', when='@1.3.5:')
    depends_on('vcftools', when='@1.3.5:')
    depends_on('bc', when='@1.3.5:')
    depends_on('samtools', when='@1.3.5:')

    parallel = False

    @when('@:1.1.0')
    def edit(self, spec, prefix):
        makefile = FileFilter('Makefile')
        b = prefix.bin
        makefile.filter('cp bin/freebayes bin/bamleftalign /usr/local/bin/',
                        'cp bin/freebayes bin/bamleftalign {0}'.format(b))

    @when('@:1.1.0')
    @run_before('install')
    def make_prefix_dot_bin(self):
        mkdir(prefix.bin)

    @when('@:1.1.0')
    def meson(self, spec, prefix):
        pass

    @when('@:1.1.0')
    def build(self, spec, prefix):
        make()

    @when('@:1.1.0')
    def install(self, spec, prefix):
        make('install')

@@ -903,32 +903,90 @@
 when='@1.3.5:')%0A
+    depends_on('pkgconfig', type='build', when='@1.3.5:')%0A
     depends_on('
@@ -1870,20 +1870,588 @@
     make('install')%0A
+%0A    @property%0A    def vcflib_builddir(self):%0A        return join_path(self.build_directory, 'vcflib')%0A%0A    @when('@1.3.4:')%0A    def setup_build_environment(self, env):%0A        if self.run_tests:%0A            env.prepend_path('PATH', self.vcflib_builddir)%0A            env.prepend_path('PATH', self.build_directory)%0A%0A    @when('@1.3.4:')%0A    def check(self):%0A        mkdir(self.vcflib_builddir)%0A        with working_dir(self.vcflib_builddir):%0A            cmake('../../vcflib')%0A            make()%0A        with working_dir(self.build_directory):%0A            ninja('test')%0A

Fix running the testsuite and add pkgconfig (#26440)