#!/usr/bin/env python
# vim: fileencoding=utf-8 ft=python et sw=4 ts=4 sts=4 tw=79

from __future__ import (
    generators, division, absolute_import, with_statement, print_function
)
import sys
try:
    import venv
except ImportError:
    venv = None

try:
    from packaging.tags import sys_tags

    def get_tag():
        tag = sys_tags().__next__()
        return (tag.interpreter, tag.abi, tag.platform)

except ImportError:
    from wheel.pep425tags import (
        get_abbr_impl, get_impl_ver, get_abi_tag, get_platform,
    )

    # sporked from wheel.bdist_wheel.get_tag
    def get_tag():
        """Get the specific implementation name in a wheel-compatible format."""
        plat_name = get_platform().replace('-', '_').replace('.', '_')
        impl_name = get_abbr_impl()
        impl_ver = get_impl_ver()
        impl = impl_name + impl_ver
        abi_tag = str(get_abi_tag()).lower()
        tag = (impl, abi_tag, plat_name)
        return tag


PY2 = sys.version_info[0] == 2


def main():
    tag = '%s-%s-%s' % get_tag()
    mk_filename = 'python_vars_%s.mk' % tag
    with open(mk_filename, 'wt') as f:
        # TODO: add proper escaping whenever I feel like staring into the abyss
        f.write('PYTHON_IMPL:=%s\n' % tag)

        f.write("PYTHON_VENV:=%s\n" % (
            "virtualenv -p $(PYTHON_EXE)"
            if venv is None else
            "$(PYTHON_EXE) -m venv"
        ))

        f.write("PYTHON_VENV_INSTALL:=%s\n" % (
            "'setuptools<45.0.0' 'pip<20.3' 'pip-tools<6'"
            if PY2 else
            "'pip-tools'"
        ))
    print("include %s" % mk_filename)


if __name__ == '__main__':
    main()