set(HEADER_OUT bg_analytics.h)
set(SOURCE_OUT bg_analytics.c)

function(cat IN_FILE OUT_FILE)
  file(READ ${IN_FILE} CONTENTS)
  file(APPEND ${OUT_FILE} "${CONTENTS}\n")
endfunction()

file(REMOVE ${HEADER_OUT})
file(APPEND ${HEADER_OUT} "#ifndef BG_ANALYTICS_AMALGAMATION_H\n")
file(APPEND ${HEADER_OUT} "#define BG_ANALYTICS_AMALGAMATION_H\n")
file(APPEND ${HEADER_OUT} "#define AMALGAMATION\n")
cat(include/bg/analytics.h ${HEADER_OUT})
cat(src/bg/config.h ${HEADER_OUT})
cat(src/palloc/palloc.h ${HEADER_OUT})
cat(src/palloc/vector.h ${HEADER_OUT})
cat(src/palloc/sstream.h ${HEADER_OUT})
cat(src/http/http.h ${HEADER_OUT})
cat(src/bg/parson.h ${HEADER_OUT})
cat(src/bg/Collection.h ${HEADER_OUT})
cat(src/bg/Document.h ${HEADER_OUT})
cat(src/bg/State.h ${HEADER_OUT})
file(APPEND ${HEADER_OUT} "#endif\n")

file(REMOVE ${SOURCE_OUT})
file(APPEND ${SOURCE_OUT} "#include \"bg_analytics.h\"\n")
cat(src/palloc/palloc.c ${SOURCE_OUT})
cat(src/palloc/vector.c ${SOURCE_OUT})
cat(src/palloc/sstream.c ${SOURCE_OUT})
cat(src/http/http.c ${SOURCE_OUT})
cat(src/bg/Collection.c ${SOURCE_OUT})
cat(src/bg/Document.c ${SOURCE_OUT})
cat(src/bg/parson.c ${SOURCE_OUT})
cat(src/bg/State.c ${SOURCE_OUT})

file(COPY src/example/main.c DESTINATION .)

file(RENAME main.c example.c)
execute_process(COMMAND gcc -oexample -DAMALGAMATION_EXAMPLE bg_analytics.c example.c)

#file(RENAME main.c example.cpp)
#execute_process(COMMAND g++ -oexample -DAMALGAMATION_EXAMPLE bg_analytics.cpp example.cpp)
