#!/usr/bin/env python

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

import os
import shutil

from setuptools import setup

includes = ['numpy']
excludes = []
DATA_FILES = []
OPTIONS = {
  'argv_emulation': False,
  'includes': includes
}

setup(
    app=['AntsViewer.py'],
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

python_ver = '2.6'

boot_addition = """
    py_lib = os.path.join(base, 'lib', 'python2.6')
    sys.path = [py_lib] + sys.path
    sys.path = [os.path.join(py_lib, 'lib-dynload')] + sys.path
    sys.path = [os.path.join(py_lib, 'lib-dynload', 'OpenGL_accelerate')] + sys.path
    sys.path = [os.path.join(py_lib, 'lib-dynload', 'PySide')] + sys.path
    os.environ['PATH'] = os.path.join(py_lib, 'lib-dynload', 'PySide') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(py_lib, 'lib-dynload', 'OpenGL_accelerate') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(base, 'numpy', 'core') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(base, 'numpy', 'fft') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(base, 'numpy', 'lib') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(base, 'numpy', 'linalg') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(base, 'numpy', 'numarray') + ":" + os.environ['PATH']
    os.environ['PATH'] = os.path.join(base, 'numpy', 'random') + ":" + os.environ['PATH']
    if not 'DYLD_LIBRARY_PATH' in os.environ:
      os.environ['DYLD_LIBRARY_PATH'] = ''
    os.environ['DYLD_LIBRARY_PATH'] = os.path.join(py_lib, 'lib-dynload') + ":" + os.environ['DYLD_LIBRARY_PATH']
    os.environ['DYLD_LIBRARY_PATH'] = os.path.join(py_lib, 'lib-dynload', 'OpenGL_accelerate') + ":" + os.environ['DYLD_LIBRARY_PATH']
    os.environ['DYLD_LIBRARY_PATH'] = os.path.join(py_lib, 'lib-dynload', 'PySide') + ":" + os.environ['DYLD_LIBRARY_PATH']
"""

res_dir = 'dist/AntsViewer.app/Contents/Resources/'
lib_dir = 'dist/AntsViewer.app/Contents/Resources/lib/python' + python_ver + '/lib-dynload/'
boot_file = res_dir + '__boot__.py'
boot_contents = []
with open(boot_file, 'r') as fh:
  line = fh.readline()
  while line:
    boot_contents.append(line.rstrip())
    if 'base = ' in line:
      boot_contents.append(boot_addition)
    line = fh.readline()

with open(boot_file, 'w') as fh:
  fh.write('\n'.join(boot_contents))

def copy_lib_file(name):
  shutil.copy2('/usr/lib/' + name, res_dir + name)

def create_link(name):
  if not os.path.exists(res_dir + name):
    os.symlink('../Frameworks/' + name, res_dir + name)

copy_lib_file('libpyside-python' + python_ver + '.1.0.dylib')
copy_lib_file('libshiboken-python' + python_ver + '.1.0.dylib')
create_link('QtCore.framework')
create_link('QtGui.framework')
create_link('QtOpenGL.framework')
