import glob
import shutil
import os

def build(target, options):
    cmd = './waf configure --prefix=%s' % target.directory
    if target.platform == 'windows':
        cmd += ' --target-windows'

    target.command(cmd)
    target.command('./waf')

def package_windows(target, version):
    out = 'build/dumpxyz-%s' % version
    target.command('mkdir %s' % out)
    shutil.copy('build/src/dumpxyz.exe', out)
    shutil.copy('%s/bin/dcp-1.0.dll' % target.directory, out)
    shutil.copy('%s/bin/asdcp-cth.dll' % target.directory, out)
    shutil.copy('%s/bin/kumu-cth.dll' % target.directory, out)
    shutil.copy('%s/bin/libopenjp2.dll' % target.directory, out)
    shutil.copy('%s/bin/cxml-0.dll' % target.directory, out)
    shutil.copy('%s/bin/libeay32.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/zlib1.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libboost_filesystem-mt.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libboost_system-mt.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libgcc_s_sjlj-1.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libstdc++-6.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libglibmm-2.4-1.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libxml++-2.6-2.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libxml2-2.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libxmlsec1-openssl.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libxmlsec1.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libffi-6.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libglib-2.0-0.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libgmodule-2.0-0.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libgobject-2.0-0.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libsigc-2.0-0.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libintl-8.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libiconv-2.dll' % target.windows_prefix, out)
    shutil.copy('%s/bin/libxslt-1.dll' % target.windows_prefix, out)
    target.command('cd build && zip -9 -r dumpxyz-%s.zip dumpxyz-%s' % (version, version))
    return os.path.abspath('build/dumpxyz-%s.zip' % version)

def dependencies(target):
    return (('libdcp', '1.0'),)

def package(target, version):
    if target.platform == 'windows':
        return package_windows(target, version)

    return None
