#!/usr/bin/env python

import os
import sys

Import('env')
module_env = env.Clone()

# libsndfile - build configuration
switch_options = [
('COMPILER_IS_GCC', '0'), 
('CPU_CLIPS_NEGATIVE', '0'), 
('CPU_CLIPS_POSITIVE', '0'), 
('CPU_IS_BIG_ENDIAN', '0'), 
('CPU_IS_LITTLE_ENDIAN', '1'), 
('ENABLE_EXPERIMENTAL_CODE', '0'), 
('HAVE_ALSA_ASOUNDLIB_H', '0'), 
('HAVE_BYTESWAP_H', '0'), 
('HAVE_CALLOC', '1'), 
('HAVE_CEIL', '1'), 
('HAVE_DECL_S_IRGRP', '1'), 
('HAVE_DLFCN_H', '1'), 
('HAVE_ENDIAN_H', '1'), 
('HAVE_EXTERNAL_XIPH_LIBS', '0'), 
('HAVE_FLOOR', '1'), 
('HAVE_FMOD', '0'), 
('HAVE_FREE', '1'), 
('HAVE_FSTAT', '1'), 
('HAVE_FSTAT64', '1'), 
('HAVE_FSYNC', '0'), 
('HAVE_FTRUNCATE', '1'), 
('HAVE_GETPAGESIZE', '1'), 
('HAVE_GETTIMEOFDAY', '0'), 
('HAVE_INTTYPES_H', '1'), 
('HAVE_LIBM', '1'), 
('HAVE_LOCALE_H', '1'), 
('HAVE_LRINT', '1'), 
('HAVE_LRINTF', '1'), 
('HAVE_LROUND', '1'), 
('HAVE_LSEEK', '1'), 
('HAVE_LSEEK64', '1'), 
('HAVE_MALLOC', '1'), 
('HAVE_MEMORY_H', '1'), 
('HAVE_MMAP', '1'), 
('HAVE_OPEN', '1'), 
('HAVE_PIPE', '1'), 
('HAVE_READ', '1'), 
('HAVE_REALLOC', '1'), 
('HAVE_SETLOCALE', '1'), 
('HAVE_SNDIO_H', '0'), 
('HAVE_SNPRINTF', '1'), 
('HAVE_SQLITE3', '0'), 
('HAVE_SSIZE_T', '0'), 
('HAVE_STDINT_H', '1'), 
('HAVE_STDLIB_H', '1'), 
('HAVE_STRINGS_H', '1'), 
('HAVE_STRING_H', '1'), 
('HAVE_SYS_STAT_H', '1'), 
('HAVE_SYS_TIME_H', '0'), 
('HAVE_SYS_TYPES_H', '1'), 
('HAVE_SYS_WAIT_H', '1'), 
('HAVE_UNISTD_H', '0'), 
('HAVE_VSNPRINTF', '1'), 
('HAVE_WAITPID', '1'), 
('HAVE_WRITE', '1'), 
('OSX_DARWIN_VERSION', '1'), 
('OS_IS_OPENBSD', '1'), 
('OS_IS_WIN32', '1'), 
('STDC_HEADERS', '1'), 
('USE_WINDOWS_API', '1'), 
('WIN32_TARGET_DLL', '0'), 
('WORDS_BIGENDIAN', '0'), 
('_MINIX', '0')
]

bool_options = [
('HAVE_DIRECT_H', True), 
('HAVE_GMTIME', False), 
('HAVE_GMTIME_R', True), 
('HAVE_IO_H', True), 
('HAVE_LOCALTIME', True), 
('HAVE_LOCALTIME_R', True)
]

size_options = [
('SIZEOF_DOUBLE', '8'), 
('SIZEOF_FLOAT', '4'), 
('SIZEOF_INT', '4'), 
('SIZEOF_INT64_T', '8'), 
('SIZEOF_LOFF_T', ''), 
('SIZEOF_LONG', '4'), 
('SIZEOF_LONG_LONG', '8'), 
('SIZEOF_OFF64_T', '8'), 
('SIZEOF_OFF_T', '4'), 
('SIZEOF_SF_COUNT_T', '8'), 
('SIZEOF_SHORT', '2'), 
('SIZEOF_SIZE_T', '4'), 
('SIZEOF_SSIZE_T', '8'), 
('SIZEOF_VOIDP', '4'), 
('SIZEOF_WCHAR_T', '2')
]

misc_options = [
('@TYPEOF_SF_COUNT_T@', 'int64_t'), 
('@PACKAGE_NAME@', 'PACKAGE_NAME'), 
('@PACKAGE_BUGREPORT@', 'PACKAGE_BUGREPORT'), 
('@CPACK_PACKAGE_VERSION_FULL@', 'CPACK_PACKAGE_VERSION_FULL'), 
('@PACKAGE_URL@', 'PACKAGE_URL'), 
('@SF_COUNT_MAX@', '0x7fffffffffffffffi64'), 
('@INLINE_CODE@', '')
]

# libsndfile - sources
libsndfile_sources = [
"./libsndfile/src/common.c",
"./libsndfile/src/file_io.c",
"./libsndfile/src/command.c",
"./libsndfile/src/pcm.c",
"./libsndfile/src/ulaw.c",
"./libsndfile/src/alaw.c",
"./libsndfile/src/float32.c",
"./libsndfile/src/double64.c",
"./libsndfile/src/ima_adpcm.c",
"./libsndfile/src/ms_adpcm.c",
"./libsndfile/src/gsm610.c",
"./libsndfile/src/dwvw.c",
"./libsndfile/src/vox_adpcm.c",
"./libsndfile/src/interleave.c",
"./libsndfile/src/strings.c",
"./libsndfile/src/dither.c",
"./libsndfile/src/cart.c",
"./libsndfile/src/broadcast.c",
"./libsndfile/src/audio_detect.c",
"./libsndfile/src/ima_oki_adpcm.c",
"./libsndfile/src/alac.c",
"./libsndfile/src/chunk.c",
"./libsndfile/src/ogg.c",
"./libsndfile/src/chanmap.c",
"./libsndfile/src/id3.c",
"./libsndfile/src/sndfile.c",
"./libsndfile/src/aiff.c",
"./libsndfile/src/au.c",
"./libsndfile/src/avr.c",
"./libsndfile/src/caf.c",
"./libsndfile/src/dwd.c",
"./libsndfile/src/flac.c",
"./libsndfile/src/g72x.c",
"./libsndfile/src/htk.c",
"./libsndfile/src/ircam.c",
"./libsndfile/src/macos.c",
"./libsndfile/src/mat4.c",
"./libsndfile/src/mat5.c",
"./libsndfile/src/nist.c",
"./libsndfile/src/paf.c",
"./libsndfile/src/pvf.c",
"./libsndfile/src/raw.c",
"./libsndfile/src/rx2.c",
"./libsndfile/src/sd2.c",
"./libsndfile/src/sds.c",
"./libsndfile/src/svx.c",
"./libsndfile/src/txw.c",
"./libsndfile/src/voc.c",
"./libsndfile/src/wve.c",
"./libsndfile/src/w64.c",
"./libsndfile/src/wavlike.c",
"./libsndfile/src/wav.c",
"./libsndfile/src/xi.c",
"./libsndfile/src/mpc2k.c",
"./libsndfile/src/rf64.c",
"./libsndfile/src/ogg_vorbis.c",
"./libsndfile/src/ogg_speex.c",
"./libsndfile/src/ogg_pcm.c",
"./libsndfile/src/ogg_opus.c",
"./libsndfile/src/ogg_vcomment.c",
"./libsndfile/src/nms_adpcm.c",
"./libsndfile/src/GSM610/add.c",
"./libsndfile/src/GSM610/code.c",
"./libsndfile/src/GSM610/decode.c",
"./libsndfile/src/GSM610/gsm_create.c",
"./libsndfile/src/GSM610/gsm_decode.c",
"./libsndfile/src/GSM610/gsm_destroy.c",
"./libsndfile/src/GSM610/gsm_encode.c",
"./libsndfile/src/GSM610/gsm_option.c",
"./libsndfile/src/GSM610/long_term.c",
"./libsndfile/src/GSM610/lpc.c",
"./libsndfile/src/GSM610/preprocess.c",
"./libsndfile/src/GSM610/rpe.c",
"./libsndfile/src/GSM610/short_term.c",
"./libsndfile/src/GSM610/table.c",
"./libsndfile/src/G72x/g721.c",
"./libsndfile/src/G72x/g723_16.c",
"./libsndfile/src/G72x/g723_24.c",
"./libsndfile/src/G72x/g723_40.c",
"./libsndfile/src/G72x/g72x.c",
"./libsndfile/src/ALAC/ALACBitUtilities.c",
"./libsndfile/src/ALAC/ag_dec.c",
"./libsndfile/src/ALAC/ag_enc.c",
"./libsndfile/src/ALAC/dp_dec.c",
"./libsndfile/src/ALAC/dp_enc.c",
"./libsndfile/src/ALAC/matrix_dec.c",
"./libsndfile/src/ALAC/matrix_enc.c",
"./libsndfile/src/ALAC/alac_decoder.c",
"./libsndfile/src/ALAC/alac_encoder.c",
]

if module_env["platform"] == "windows":
  print("libsndfile: windows")
  libsndfile_sources.append("./libsndfile/src/windows.c")
  
if module_env["platform"] == "x11":
  print("libsndfile: x11")

if module_env["platform"] == "osx":
  print("libsndfile: osx")

# libsndfile - generate configure files
subst_dict = []
for switch_option in switch_options:
  subst_dict.append(('#cmakedefine01 ' + switch_option[0], '#define ' + switch_option[0] + ' ' + switch_option[1]))
  
for bool_option in bool_options:
  if (bool_option[1]):
    subst_dict.append(('#cmakedefine ' + bool_option[0], '#define ' + bool_option[0]))
  else:
    subst_dict.append(('#cmakedefine ' + bool_option[0], '// #define ' + bool_option[0]))
    
for size_option in size_options:
  subst_dict.append(('@' + size_option[0] + '_CODE@', '#define ' + size_option[0] + ' ' + size_option[1]))
  
for misc_option in misc_options:
  subst_dict.append((misc_option[0], misc_option[1]))
  
module_env.Substfile(dest='./libsndfile/src/config.h', source='./libsndfile/src/config.h.cmake', SUBST_DICT = subst_dict)
module_env.Substfile(dest='./libsndfile/src/sndfile.h', source='./libsndfile/src/sndfile.h.in', SUBST_DICT = subst_dict)

# libsndfile
module_env.add_source_files(env.modules_sources, libsndfile_sources)
module_env.Append(CPPPATH=["libsndfile/src"])
module_env.Append(CPPPATH=["libsndfile/src/GSM610"])
module_env.Append(CPPPATH=["libsndfile/src/G72x"])
module_env.Append(CPPPATH=["libsndfile/src/ALAC"])


# Godot
sources = [
"register_types.cpp",

"libsndfile.cpp",
]
module_env.add_source_files(env.modules_sources, sources)
