#!/usr/bin/env python

import sys
import os
import os.path as op

top = '.'
out = 'build'

def options(opt):
    opt.load('compiler_c python')
    opt.add_option('--edition', default='se', help="dupeGuru edition to build (se, me pe)")

def configure(conf):
    if conf.options.edition not in ('se', 'me', 'pe'):
        conf.options.edition = 'se'
    print("Building dupeGuru {}".format(conf.options.edition.upper()))
    conf.env.DGEDITION = conf.options.edition
    # We use clang to compile our app
    conf.env.CC = 'clang'
    # WAF has a "pyembed" feature allowing us to automatically find Python and compile by linking
    # to it. The problem is that because we made a copy of the Python library to mangle with its
    # "install name", we don't actually want to link to our installed python, but to our mangled
    # Python. The line below tells the "pyembed" WAF feature to look in ../build for Python.
    conf.env.LIBPATH_PYEMBED = op.abspath('../build')
    # I did a lot of fiddling-around, but I didn't find how to tell WAF the Python library name
    # to look for without making the whole compilation process fail, so I just create a symlink
    # with the name WAF is looking for. 
    versioned_dylib_path = '../build/libpython{}.dylib'.format(sys.version[:3])
    if not op.exists(versioned_dylib_path):
        os.symlink('../build/Python', versioned_dylib_path)
    # The rest is standard WAF code that you can find the the python and macapp demos.
    conf.load('compiler_c python')
    conf.check_python_version((3,3,0))
    conf.check_python_headers()
    conf.env.FRAMEWORK_COCOA = 'Cocoa'
    conf.env.ARCH_COCOA = ['x86_64']
    # Add cocoalib dir to the framework search path so we can find Sparkle.
    conf.env.CFLAGS = ['-F'+op.abspath('../cocoalib')]
    conf.env.LINKFLAGS = ['-F'+op.abspath('../cocoalib')]

def build(ctx):
    # What do we compile?
    cocoalib_node = ctx.srcnode.find_dir('..').find_dir('cocoalib')
    cocoalib_folders = ['controllers', 'views']
    cocoalib_includes = [cocoalib_node] + [cocoalib_node.find_dir(folder) for folder in cocoalib_folders]
    cocoalib_uses = ['NSEventAdditions', 'Dialogs', 'HSAboutBox', 'Utils',
        'HSPyUtil', 'ProgressController', 'HSRecentFiles', 'HSQuicklook', 'ValueTransformers',
        'NSImageAdditions', 'NSNotificationAdditions',
        'views/HSTableView', 'views/HSOutlineView', 'views/NSIndexPathAdditions',
        'views/NSTableViewAdditions',
        'controllers/HSColumns', 'controllers/HSGUIController', 'controllers/HSTable',
        'controllers/HSOutline', 'controllers/HSPopUpList', 'controllers/HSSelectableList',
        'controllers/HSTextField', 'controllers/HSProgressWindow']
    cocoalib_src = [cocoalib_node.find_node(usename + '.m') for usename in cocoalib_uses] + cocoalib_node.ant_glob('autogen/*.m')
    project_folders = ['autogen', 'base', ctx.env.DGEDITION]
    project_src = sum([ctx.srcnode.ant_glob('%s/*.m' % folder) for folder in project_folders], [])
    
    # Compile
    ctx.program(
        # "pyembed" takes care of the include and linking stuff to compile an app that embed Python.
        features      = 'c cprogram pyembed',
        target        = ctx.bldnode.make_node("dupeGuru"),
        source        = cocoalib_src + project_src,
        includes      = project_folders + cocoalib_includes,
        use           = 'COCOA',
        # Because our python lib's install name is "@rpath/Python", we need to set the executable's
        # rpath. Fortunately, WAF supports it and we just need to supply the "rpath" argument.
        rpath         = '@executable_path/../Frameworks',
        framework     = ['Sparkle', 'Quartz'],
    )

from waflib import TaskGen
@TaskGen.extension('.m')
def m_hook(self, node):
    """Alias .m files to be compiled the same as .c files, gcc will do the right thing."""
    return self.create_compiled_task('c', node)

