#!/usr/bin/env python
import os
import sys

from waflib.extras import autowaf as autowaf
import waflib.Options as Options

# Version of this package (even if built as a child)
LV2CORE_VERSION = '4.1'

# Variables for 'waf dist'
APPNAME = 'lv2core'
VERSION = LV2CORE_VERSION

# Mandatory variables
top = '.'
out = 'build'

def options(opt):
    opt.load('compiler_c')
    autowaf.set_options(opt)
    opt.add_option('--bundle-only', action='store_true', default=False,
                   dest='bundle_only',
                   help="Only install bundle (not header or pkg-config file)")
    opt.add_option('--copy-headers', action='store_true', default=False,
                   dest='copy_headers',
                   help='Copy headers instead of linking to bundle')

def configure(conf):
    conf.load('compiler_c')
    autowaf.configure(conf)

    autowaf.display_msg(conf, "LV2 bundle directory", conf.env['LV2DIR'])
    print('')

def build(bld):
    # Header "library"
    obj = bld(export_includes = ['.'],
              name            = 'liblv2core',
              target          = 'lv2core')

    # Bundle (data)
    bld.install_files('${LV2DIR}/lv2core.lv2', 'lv2.ttl manifest.ttl')

    if not Options.options.bundle_only:
        # Header
        bld.install_files('${INCLUDEDIR}', 'lv2.h')
        bld.install_files('${LV2DIR}/lv2core.lv2', 'lv2.h')

        # Pkgconfig file
        autowaf.build_pc(bld, 'LV2CORE', LV2CORE_VERSION, '', [])

        # URI-like header include
        include_dir = os.path.join(bld.env['INCLUDEDIR'], 'lv2/lv2plug.in/ns')
        bundle_dir  = os.path.join(bld.env['LV2DIR'], 'lv2core.lv2')
        if bld.env['COPY_HEADERS']:
            bld.install_files(os.path.join(include_dir, 'lv2core'),
                              bld.path.ant_glob('*.h'))
        else:
            bld.symlink_as(os.path.join(include_dir, 'lv2core'),
                           os.path.relpath(bundle_dir, include_dir))

def dist():
    import Scripting
    Scripting.g_gz = 'gz'
    Scripting.dist()