from waftools.plugin import plugin

def plugin_configure(conf):
    if not conf.check_cfg(package="libmpg123", atleast_version="1.5.1", uselib_store="mpg123", args="--cflags --libs"):
        return False

    if not conf.check_cc(header_name='mpg123.h', uselib='mpg123',
                         errmsg='failed',
                         msg='Testing mpg123 with default off_t'):
        # ask python for lfs flags, this fails on Windows and Mac OS
        # windows needs some special care, and Mac OS X alwas uses 64 bit for
        # off_t so we shouldn't be here anyway
        
        import os
        try:
            lfsflags = os.confstr (os.confstr_names['CS_LFS_CFLAGS']).split () 
        # If os.confstr of 'CS_LFS_CFLAGS' is not supported, try with some
        # default values
        except KeyError:
            # Mac OS X and probably other unices end here
            lfsflags = ['-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64']
        except AttributeError:
            # Windows ends up here
            lfsflags = ['-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64']
        if not conf.check_cc(header_name='mpg123.h', uselib='mpg123',
                         cflags=lfsflags, errmsg='failed',
                         msg='Testing mpg123 with large off_t'):
            return False
        conf.env['CCFLAGS_mpg123'] += lfsflags

    return True

configure, build = plugin("mpg123", configure=plugin_configure,
                          source=["mpg123.c", "id3v1.c"], libs=["mpg123"])
