from distutils.core import setup
import py2exe

from distutils.filelist import findall
import os, sys
import matplotlib
import glob

use_xp_manifest = True
data_files = []

if not use_xp_manifest:
    print "Building pre-Windows XP executable - not including manifest"
else:
    print "Building Windows XP executable"
print "Looking in %s" % os.path.join("icons", "*.gif")
icons_data = glob.glob(os.path.join("icons", "*.gif"))
icons_data.extend(glob.glob(os.path.join("icons", "*.png")))
icons_data_files = []
cwd = os.getcwd()
for f in icons_data:
    p = ("icons", [os.path.join(cwd, f)])    
    print p
    icons_data_files.append(p)

doc_data = glob.glob(os.path.join("doc", "web", "*.pdf"))
doc_data_files = []
for f in doc_data:
    p = ("doc", [os.path.join(cwd, f)])
    doc_data_files.append(p)
    
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
matplotlibdata_files = []
version = matplotlib.__version__.split(".") 
# Newer versions of matplotlib use a different directory
# NOTE: if you're py2exe-generated executable has issues
# initializing matplotlib, see if the issue is finding
# the data files
if (int(version[0]) + int(version[1])*0.01) > .90: # later than 0.90.0
    top = "mpl-data"
else:
    top = 'matplotlibdata'
for f in matplotlibdata:
    dirname = os.path.join(top, f[len(matplotlibdatadir)+1:])
    print (os.path.split(dirname)[0], [f])
    matplotlibdata_files.append((os.path.split(dirname)[0], [f]))

data_files.extend(matplotlibdata_files)
data_files.extend(icons_data_files)
data_files.extend(doc_data_files)
# Applicaiton files
data_files.append("isilk.exe.manifest")
data_files.append("isilk.ico")
data_files.append("VERSION")

if use_xp_manifest:
    setup_windows = [
      {
        "script" : "isilk.py",
        "icon_resources" : [(1, "isilk.ico")],
        "other_resources": [(24,1,"isilk.exe.manifest")]
      }] 
else:
    setup_windows = [
      {
        "script" : "isilk.py",
        "icon_resources" : [(1, "isilk.ico")] #,
      }]
    
setup_options  = {
               'py2exe': {
                          'packages' : ['matplotlib', 'pytz', 'paramiko'],
                          'excludes': ['_gtkagg', '_tkagg', '_gdkagg', 'tcl'],
                          'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                           'libgobject-2.0-0.dll',
                                           'libgdk_pixbuf-2.0-0.dll',
                                           'tcl84.dll',
                                           'tk84.dll']
                         }
              }
# Run setup
setup(windows=setup_windows, options= setup_options,
    data_files=data_files)

# Other clean up (is there a better way to do this)
cmd ="del /F /Q %s" % os.path.join("dist", "tcl")
print "Removing pseudo-dependencies"
print "Running: %s" % cmd
os.system(cmd)


