#!/usr/bin/env python

import os

programs = """
krad_radio.c
krad_radio_simple.c
kr_api_demo.c
""".split()

programs2 = """
khaos_monkey.c
krad_radio_osc.c
""".split()

programs3 = """
krad_radio_manager.c
""".split()

programs4 = """
krad_radio_vp.c
krad_radio_ap.c
""".split()

programs5 = """
krad_radio_web.c
""".split()

libsources = """
../lib/krad_ebml/krad_ebml2.c
../lib/krad_ebml/krad_ebml2w.c
../lib/krad_io/krad_io2.c
../lib/krad_system/krad_system.c
../lib/krad_ipc/krad_ipc_client.c
../lib/krad_radio/krad_radio_client.c
../lib/krad_radio/krad_radio_client_ctl.c
../lib/krad_mixer/krad_mixer_client.c
../lib/krad_compositor/krad_compositor_client.c
../lib/krad_transponder/krad_transponder_client.c
../lib/krad_radio/krad_radio_common.c
../lib/krad_mixer/krad_mixer_common.c
../lib/krad_compositor/krad_compositor_common.c
../lib/krad_transponder/krad_transponder_common.c
""".split()

depsources2 = """
../lib/krad_osc/krad_osc.c
""".split()

includedirs = """
../lib/krad_compositor/
../lib/krad_chronometer/
../lib/krad_xmms2/
../lib/krad_y4m/
../lib/krad_framepool/
../lib/krad_web/
../lib/krad_web/ext/
../lib/krad_web/ext/libwebsockets/
../lib/krad_web/res/
../lib/krad_vhs/
../lib/krad_sfx/
../lib/krad_tone/
../lib/krad_ipc/
../lib/krad_tags/
../lib/krad_audio/
../lib/krad_theora/
../lib/krad_jack/
../lib/krad_alsa/
../lib/krad_mixer/
../lib/krad_osc/
../lib/krad_xmms2/
../lib/krad_wayland/
../lib/krad_vpx/
../lib/krad_v4l2/
../lib/krad_flac/
../lib/krad_vorbis/
../lib/krad_opus/
../lib/krad_radio/
../lib/krad_ring/
../lib/krad_ogg/
../lib/krad_io/
../lib/krad_transponder/
../lib/krad_transmitter/
../lib/krad_container/
../lib/krad_x11/
../lib/krad_udp/
../lib/krad_system/
../lib/krad_decklink/
../lib/krad_ebml/
../lib/krad_legacy/
/usr/local/include
/usr/include
""".split()

depsources5 = """
../lib/krad_web/ext/cJSON.c
../lib/krad_web/ext/libwebsockets/base64-decode.c
../lib/krad_web/ext/libwebsockets/output.c
../lib/krad_web/ext/libwebsockets/extension.c
../lib/krad_web/ext/libwebsockets/extension-deflate-stream.c
../lib/krad_web/ext/libwebsockets/extension-deflate-frame.c
../lib/krad_web/ext/libwebsockets/handshake.c
../lib/krad_web/ext/libwebsockets/server.c
../lib/krad_web/ext/libwebsockets/server-handshake.c
../lib/krad_web/ext/libwebsockets/sha-1.c
../lib/krad_web/ext/libwebsockets/libwebsockets.c
../lib/krad_web/ext/libwebsockets/parsers.c
../lib/krad_web/krad_http.c
../lib/krad_web/krad_websocket.c
""".split()

#shlibs = ["cairo", "pangocairo", "opus", "xmms2-client", "vpx"]
shlibs = ["opus"]

shlibs2 = ["cairo", "gtk+-3.0"]

stlibs = []
syslibs = ['m', 'dl', 'z']

syslibs2 = ['pthread']
plibs = ['rt', 'pthread']

def check_system(systm):

  global programs
  global includedirs
  global libsources
  global shlibs
  global shlibs2  
  global syslibs
  global syslibs2

  if systm.env['KR_LINUX']:
    #shlibs = shlibs + ["alsa"]    
    #libsources = ["../lib/krad_alsa/krad_alsa.c", "../lib/krad_alsa/krad_alsa_seq.c"] + libsources
    #programs = ["krad_radio_ev.c"] + programs
    syslibs = syslibs + syslibs2


  if systm.env['FRAK_MACOSX']:
    libsources = ["../lib/krad_legacy/krad_mach.c"] + libsources
    includedirs = ["/opt/libjpeg-turbo/include"] + includedirs
    shlibs = shlibs + ['turbojpeg']

def configure(conf):

  global programs
  global shlibs
  global shlibs2  
  global syslibs
  check_system(conf)

  for l in syslibs:
    conf.check(lib = l, cflags='-Wall', uselib_store = l)

  for l in shlibs:
    conf.check_cfg(package = l, uselib_store = l, args='--cflags --libs')

  if conf.options.nogtk == False:
    conf.env['KRAD_GTK'] = "yes"
    for l in shlibs2:
      conf.check_cfg(package = l, uselib_store = l, args='--cflags --libs')

  for l in stlibs:
    conf.check_cfg(package = l, uselib_store = l, args='--cflags --libs --static')

def build(bld):

  global programs
  global shlibs
  global shlibs2  
  global syslibs
  global syslibs2
  check_system(bld)

  # read version number to be used for the shared library
  f = open("lib/krad_radio/krad_radio_version.h", "r")
  for line in f:
    if "#define VERSION_NUMBER" in line:
      krad_version = line.split(' ')[2].strip()
      break
  f.close()

  # we use "KRAD_VERSION.0.0" for now as vnum. this basically means 
  # that we advise to rebuild clients every new version due to 
  # possible API-breakages. closest to reality currently.
  # plan for later: "API_VERSION.KRAD_VERSION.0" so rebuild is only 
  # needed when we _really_ break the API


  for p in programs:
    bld(features = 'c cprogram', 
    source = [p], 
    includes = includedirs, 
    target = p.replace(".c", ""),
    use = ["kradradio_client"],
    uselib = shlibs + stlibs + syslibs)

  for p in programs2:
    bld(features = 'c cprogram', 
      source = [p] + depsources2,
      includes = includedirs, 
      target = p.replace(".c", ""),
      use = ["kradradio_client"],
      uselib = shlibs + stlibs + syslibs + plibs)

  for p in programs5:
    bld(features = 'c cprogram', 
    source = [p] + depsources5,
    includes = includedirs, 
    target = p.replace(".c", ""),
    use = ["kradradio_client"],
    uselib = shlibs + stlibs + syslibs + plibs)
      
  if bld.env['KRAD_GTK'] == "yes":
    for p in programs3:
      bld(features = 'c cprogram', 
        source = [p], 
        includes = includedirs, 
        target = p.replace(".c", ""),
        use = ["kradradio_client"],
        uselib = shlibs + shlibs2 + stlibs + syslibs)

  bld.shlib(
    source = libsources, 
    includes = includedirs, 
    target = "kradradio_client",
    vnum = krad_version + ".0.0",
    uselib = shlibs + stlibs + syslibs)

