import os
from logging import warning

def build(bld):
    obj = bld.new_task_gen('cc', 'shlib', 'perlext')
    obj.target = 'XMMSClient'
    obj.includes = '. ../../../.. ../../../include ../../../includepriv'
    obj.source = """
        perl_xmmsclient.c
        XMMSClient.xs
        XMMSClientPlaylist.xs
        XMMSClientCollection.xs
        XMMSClientResult.xs
    """.split()
    obj.uselib_local = 'xmmsclient'
    obj.install_path = "${INSTALLDIR_PERL_AUTO}"
    obj.mac_bundle = True

    bld.install_files('${INSTALLDIR_PERL_LIB}', 'pm/XMMSClient.pm')
    bld.install_files('${INSTALLDIR_PERL_LIB}/XMMSClient', 'pm/XMMSClient/Sync.pm')
    bld.install_files('${INSTALLDIR_PERL_LIB}/XMMSClient', 'pm/XMMSClient/Glib.pm')
    bld.install_files('${INSTALLDIR_PERL_LIB}/XMMSClient', 'pm/XMMSClient/Collection.pm')

def configure(conf):
    conf.check_tool('perl')

    if not conf.check_perl_version((5,7,3)):
        conf.fatal("Wrong perl version")

    conf.check_perl_ext_devel()

    if not os.path.commonprefix([conf.env['ARCHDIR_PERL'], conf.env['PREFIX']]).startswith(conf.env['PREFIX']):
        warning('default perl libdir is not under PREFIX. specify path '
                + 'manually using --with-perl-archdir if you don\'t want the '
                + 'perl bindings to be installed to ' + conf.env['ARCHDIR_PERL'])

    conf.env['INSTALLDIR_PERL_AUTO'] = os.path.join(conf.env['ARCHDIR_PERL'], "auto", "Audio", "XMMSClient")
    conf.env['INSTALLDIR_PERL_LIB'] = os.path.join(conf.env['ARCHDIR_PERL'], "Audio")

    return True

def set_options(opt):
    opt.tool_options('perl')
