
################################################################################
##                                                                            ##
##  This file is part of NCrystal (see https://mctools.github.io/ncrystal/)   ##
##                                                                            ##
##  Copyright 2015-2025 NCrystal developers                                   ##
##                                                                            ##
##  Licensed under the Apache License, Version 2.0 (the "License");           ##
##  you may not use this file except in compliance with the License.          ##
##  You may obtain a copy of the License at                                   ##
##                                                                            ##
##      http://www.apache.org/licenses/LICENSE-2.0                            ##
##                                                                            ##
##  Unless required by applicable law or agreed to in writing, software       ##
##  distributed under the License is distributed on an "AS IS" BASIS,         ##
##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  ##
##  See the License for the specific language governing permissions and       ##
##  limitations under the License.                                            ##
##                                                                            ##
################################################################################

# This is the _ncrystal_core.info or _ncrystal_core_monolithic.info Python
# module, containing an appropriate wrapper function for exposing
# ncrystal-config as a Python entry-point, and containing information about the
# installation. Several of the strings in this file has content generated by
# CMake.

#Expanded by CMake:
_has_shlib_in_scripts_dir = bool('')

def _ncrystal_config_cli_wrapper():
    import subprocess
    import pathlib
    import sys
    f = pathlib.Path(__file__).parent.joinpath('data','bin','ncrystal-config')
    a = sys.argv[:]
    a[0] = f
    if _has_shlib_in_scripts_dir:
        #Windows, must let the binary ncrystal-config know about the actual
        #shlibpath:
        lp = libpath().parent
        a.append('+')
        a.append(str(lp))
    rv = subprocess.run( a )
    raise SystemExit(rv.returncode)

def version():
    #Expanded by CMake:
    return '4.1.8'

def libname():
    #Expanded by CMake:
    return 'libNCrystal.so'#NB: generator expression

def namespace():
    #Expanded by CMake:
    return ''

_cache_libpath = [None]
def libpath():
    if _cache_libpath[0] is None:
        _cache_libpath[0] = _libpath_impl()
    return _cache_libpath[0]

def _libpath_impl():
    import pathlib
    shlib_name = libname()
    if not _has_shlib_in_scripts_dir:
        #Not windows, we have the shared library (.so or .dylib) in the module
        #data dir:
        shlib_dirname = 'lib' #Expanded by CMake
        return pathlib.Path(__file__).parent.joinpath(
            'data', shlib_dirname, shlib_name
        ).absolute().resolve()

    #Windows. The DLL is in the scripts directory (to be in %PATH%). To be safe
    #we look not only in the default scheme, but also in a few other preferred
    #schemes:
    import sysconfig
    schemes = []

    if hasattr(sysconfig,'get_default_scheme'):
        #py 3.10+
        schemes += [ sysconfig.get_default_scheme() ]
    else:
        #py 3.9 or earlier
        schemes += [ sysconfig._get_default_scheme() ]

    if hasattr(sysconfig,'get_preferred_scheme'):
        #py 3.10+
        for scheme_key in ['prefix','home','user']:
            scheme = sysconfig.get_preferred_scheme(scheme_key)
            if scheme not in schemes:
                schemes.append(scheme)
    else:
        #Tack on everything in py 3.9 or earlier:
        schemes += list( sysconfig.get_scheme_names() )

    for scheme in schemes:
        p = pathlib.Path( sysconfig.get_path('scripts',scheme) )
        p = p.joinpath(shlib_name)
        if p.is_file():
            return p.absolute().resolve()

    #Error:
    ln = libname()
    raise RuntimeError('Unable to locate shared library (expected '
                       f'{ln} in Python site scripts directory)')
