import os
import glob

#here use a flag so don't automatically use setuptools if available, hard to test otherwise
with_setuptools = False
if 'USE_SETUPTOOLS' in os.environ or 'pip' in __file__ or 'easy_install' in __file__:
  try:
    from setuptools.command.install import install
    from setuptools import setup
    from setuptools import Extension
    from setuptools.command.build_ext import build_ext
    with_setuptools = True
  except ImportError:
    pass
if not with_setuptools:
    from distutils.command.install import install
    from distutils.core import setup
    from distutils.core import Extension
    from distutils.command.build_ext import build_ext

# sorry for this, welcome feedback on the "right" way.
# shipping pre-compiled bainries on windows, have
# to hack things up here.

class mybuild(build_ext):
    def run(self):
        if "nt" in os.name:
            print("On Windows, skipping build_ext.")
            return
        build_ext.run(self)

class post_install(install):
    def run(self):
        install.run(self)

        # on windows boxes, manually copy pre-compiled DLLs to
        # site-packages (or wherever the module is being installed)
        if "nt" in os.name:
            basedir = os.path.dirname(__file__)
            for f in glob.glob(os.path.join(basedir, "*.dll")):
                src = os.path.join(basedir, f)
                dst = os.path.join(self.install_lib, f)
                open(dst, "wb").write(open(src, "rb").read())

cHeatmap = Extension('cHeatmap', sources=['heatmap/heatmap.c', ])

#separate calls to remove errors
basekw = {
      'name' : 'heatmap',
      'version' : "2.2.1",
      'description' : 'Module to create heatmaps',
      'author' : 'Jeffrey J. Guy',
      'author_email' : 'jjg@case.edu',
      'url' : 'http://jjguy.com/heatmap/',
      'packages' : ['heatmap', ],
      'py_modules' : ['heatmap.colorschemes', ],
      'ext_modules' : [cHeatmap, ],
      'cmdclass' : {'install': post_install,
                'build_ext': mybuild},
      'classifiers' : [
                       'Programming Language :: Python', 
                       'Programming Language :: Python :: 2.6',
                       'Programming Language :: Python :: 2.7',
                       'Programming Language :: Python :: 3',
                       'Operating System :: OS Independent',
                       'License :: OSI Approved :: MIT License',
                       'Topic :: Software Development :: Libraries :: Python Modules',
                       'Topic :: Scientific/Engineering :: Visualization',
                       'Topic :: Scientific/Engineering :: GIS'
                      ]
      }
setuptoolskw = {      
      'install_requires' : ['Pillow'],
      'extras_require' : {'proj' : 'pyproj'},
      'test_suite' : "test",
      'tests_require' : ['pyproj']
      }
distutilskw = {
      'requires' : ["Pillow"]
      }

basekw.update(setuptoolskw) if with_setuptools else basekw.update(distutilskw)
setup(**basekw)
