#!/usr/bin/env python
import subprocess
import waflib.Options as Options
import string
import os;

# Version of this package (even if built as a child)
PACKAGE_VERSION = '1.2.0'

# Variables for 'waf dist'
APPNAME = 'non-timeline'
VERSION = PACKAGE_VERSION

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

def options(opt):
    opt.load('compiler_c')
    opt.load('compiler_cxx')
    opt.load('gnu_dirs')

def configure(conf):
    conf.load('compiler_c')
    conf.load('compiler_cxx')
    conf.load('gnu_dirs')

    # conf.env.append_value('CFLAGS', '-D_LARGEFILE64_SOURCE')
    # conf.env.append_value('CFLAGS', '-D_FILE_OFFSET_BITS=64')
    # conf.env.append_value('CXXFLAGS', '-D_LARGEFILE64_SOURCE')
    # conf.env.append_value('CXXFLAGS', '-D_FILE_OFFSET_BITS=64')

    conf.check_cfg(package='sndfile', uselib_store='SNDFILE',args="--cflags --libs",
                      atleast_version='1.0.18', mandatory=True)

    conf.check(msg='Checking for SF_FORMAT_VORBIS',
                  define_name='HAVE_SF_FORMAT_VORBIS',
                  fragment='#include <sndfile.h>\nint main ( int argc, char **argv ) { return SF_FORMAT_VORBIS; }',
                  execute=False, mandatory=False)

    conf.define('VERSION', PACKAGE_VERSION)
    conf.define('SYSTEM_PATH', '/'.join( [ conf.env.DATADIR, APPNAME ] ) )
    conf.define('DOCUMENT_PATH', '/'.join( [ conf.env.DATADIR, 'doc' ] ) )
    conf.define('PIXMAP_PATH', '/'.join( [ conf.env.DATADIR, 'pixmaps' ] ) )

    conf.write_config_header('config.h', remove=False)

    print('')

def build(bld):
    #     libs = 'LILV SUIL JACK SERD SRATOM LV2'

    libs = '' 

    obj = bld(features = 'cxx cxxprogram',
              source = '''
src/Annotation_Region.C
src/Audio_Region.C
src/Audio_Sequence.C
src/Control_Point.C
src/Control_Sequence.C
src/Cursor_Point.C
src/Cursor_Region.C
src/Cursor_Sequence.C
src/Engine/Audio_File.C
src/Engine/Audio_File_Dummy.C
src/Engine/Audio_File_SF.C
src/Engine/Audio_Region.C
src/Engine/Audio_Sequence.C
src/Engine/Control_Sequence.C
src/Engine/Disk_Stream.C
src/Engine/Engine.C
src/Engine/Peaks.C
src/Engine/Playback_DS.C
src/Engine/Record_DS.C
src/Engine/Timeline.C
src/Engine/Track.C
src/NSM.C
src/OSC_Thread.C
src/Project.C
src/Sequence.C
src/Sequence_Point.C
src/Sequence_Region.C
src/Sequence_Widget.C
src/TLE.fl
src/Track_Header.fl
src/Tempo_Point.C
src/Tempo_Sequence.C
src/Time_Point.C
src/Time_Sequence.C
src/Timeline.C
src/Track.C
src/Transport.C
src/Waveform.C
src/main.C
''',
              target       = 'non-timeline',
              includes     = ['.', 'src', '..', '../nonlib'],
              uselib = [ 'JACK', 'LIBLO', 'SNDFILE', 'NTK', 'NTK_IMAGES', 'PTHREAD'],
              use = [ 'fl_widgets', 'nonlib'],
              install_path = '${BINDIR}')

    obj = bld(features = 'cxx cxxprogram',
              source='bin/import-ardour-session_gui.fl',
              target       = 'import-ardour-session_gui',
              includes     = ['.', 'src', '..', '../nonlib'],
              uselib = [ 'NTK', 'NTK_IMAGES', 'PTHREAD'],
         ##     use = [ 'fl_widgets', 'nonlib'],
              install_path = '${BINDIR}')

    bld.install_files('${BINDIR}', 'bin/import-ardour-session', chmod=0o0555,
                      relative_trick=True)

    bld( features = 'subst',
         source = 'non-timeline.desktop.in',
         target = 'non-timeline.desktop',
         encoding = 'utf8',
         install_path = "${DATADIR}" + '/applications',
         BIN_PATH = bld.env.BINDIR )


    bld.symlink_as( '${BINDIR}/' + 'non-daw', APPNAME )

    start_dir = bld.path.find_dir( 'icons/hicolor' )

    bld.install_files('${DATADIR}/icons/hicolor', start_dir.ant_glob('**/*.png'),
                      cwd=start_dir, relative_trick=True)

    bld.install_as('${DATADIR}/pixmaps/' + APPNAME + '/icon-256x256.png', 'icons/hicolor/256x256/apps/' + APPNAME + '.png')

    bld.install_files( '/'.join( [ '${DATADIR}/doc', APPNAME ] ), bld.path.ant_glob( 'doc/*.html doc/*.png' ) )

