#!/usr/bin/env python

from distutils.core import setup, Extension
import subprocess
import os

def pkgconfig(what):
    out = []
    cmd = 'pkg-config %s %s' % (what, '@PACKAGE_NAME@')
    pcout = subprocess.check_output(cmd.split()).decode()
    for token in pcout.split():
        out.append(token[2:])
    return out

cflags = ['-std=c99', '-Wno-strict-aliasing']
additional_link_args = []

# See if we're building in-tree
if os.access('Makefile.am', os.F_OK):
    cflags.append('-I../include')
    additional_link_args.extend(['-Wl,-L../rpmio/.libs',
                                 '-Wl,-L../lib/.libs',
                                 '-Wl,-L../build/.libs',
                                 '-Wl,-L../sign/.libs'])
    os.environ['PKG_CONFIG_PATH'] = '..'

rpmmod = Extension('rpm._rpm',
                   sources = ['header-py.c', 'rpmds-py.c', 'rpmfd-py.c',
                              'rpmfi-py.c', 'rpmii-py.c', 'rpmkeyring-py.c',
                              'rpmmacro-py.c', 'rpmmi-py.c', 'rpmps-py.c',
                              'rpmstrpool-py.c', 'rpmfiles-py.c',
                              'rpmarchive-py.c', 'rpmtd-py.c',
                              'rpmte-py.c', 'rpmts-py.c', 'rpmver-py.c',
                              'spec-py.c',
                              'rpmmodule.c'],
                   include_dirs = pkgconfig('--cflags'),
                   library_dirs = pkgconfig('--libs-only-L'),
                   libraries = pkgconfig('--libs-only-l') + ['rpmbuild', 'rpmsign'],
                   extra_compile_args = cflags,
                   extra_link_args = additional_link_args
                  )

setup(name='@PACKAGE_NAME@',
      version='@VERSION@',
      description='Python bindings for @PACKAGE_NAME@',
      maintainer_email='@PACKAGE_BUGREPORT@',
      url='http://www.rpm.org/',
      license='GNU General Public License v2',
      packages=['@PACKAGE_NAME@'],
      ext_modules=[rpmmod]
     )
