#!/usr/bin/python

import os, sys
from glob import glob
os.chdir(os.path.dirname(__file__))

def sysExec(cmd):
    print " ".join(cmd)
    r = os.system(" ".join(cmd))
    if r != 0: sys.exit(r)

sysExec(["mkdir","-p","build"])
os.chdir("build")

def includeExists(fn):
	for p in ["/usr/include", "/usr/local/include"]:
		if os.path.exists(p + "/" + fn): return True
	return False

staticChromaprint = False
staticSwresample = False

if not includeExists("libswresample/libswresample.h"):
	print "forcing staticSwresample = True"
	staticSwresample = True

ffmpegFiles = ["../ffmpeg.c"] + \
	(glob("../chromaprint/*.cpp") if staticChromaprint else [])

if staticSwresample:
	ffmpegFiles += map(lambda fn: "../libs/libswresample/" + fn,
		[
			"audioconvert.c",
			"dither.c",
			"rematrix.c",
			"resample.c",
			"swresample.c",
		])

sysExec(["cc", "-std=c99", "-c"] + ffmpegFiles +
	[
		"-DHAVE_CONFIG_H",
		"-I", "/System/Library/Frameworks/Python.framework/Headers/",
		"-g",
	] +
	(["-I", "../chromaprint"] if staticChromaprint else []) +
	(["-I", "../libs/libswresample"] if staticSwresample else [])
)

sysExec(["libtool", "-dynamic", "-o", "../ffmpeg.so"] +
	[os.path.splitext(os.path.basename(fn))[0] + ".o" for fn in ffmpegFiles] +
	[
		"-framework", "Python",
		"-lavformat", "-lavutil", "-lavcodec", "-lswresample", "-lportaudio",
		"-lc", "-lstdc++",
	] +
	([] if staticChromaprint else ["-lchromaprint"])
)

levelDbFiles = glob("../leveldb*.cc")

sysExec(["cc", "-c"] + levelDbFiles +
	[
		"-I", "/System/Library/Frameworks/Python.framework/Headers/",
		"-g",
	]
)

sysExec(["libtool", "-dynamic", "-o", "../leveldb.so"] +
	[os.path.splitext(os.path.basename(fn))[0] + ".o" for fn in levelDbFiles] +
	[
		"-framework", "Python",
		"-lleveldb", "-lsnappy",
		"-lc", "-lstdc++",
	]
)

@@ -244,591 +244,127 @@
 %22)%0A%0A
-def includeExists(fn):%0A%09for p in %5B%22/usr/include%22, %22/usr/local/include%22%5D:%0A%09%09if os.path.exists(p + %22/%22 + fn): return True%0A%09return False%0A%0AstaticChromaprint = False%0AstaticSwresample = False%0A%0Aif not includeExists(%22libswresample/libswresample.h%22):%0A%09print %22forcing staticSwresample = True%22%0A%09staticSwresample = True%0A%0AffmpegFiles = %5B%22../ffmpeg.c%22%5D + %5C%0A%09(glob(%22../chromaprint/*.cpp%22) if staticChromaprint else %5B%5D)%0A%0Aif staticSwresample:%0A%09ffmpegFiles += map(lambda fn: %22../libs/libswresample/%22 + fn,%0A%09%09%5B%0A%09%09%09%22audioconvert.c%22,%0A%09%09%09%22dither.c%22,%0A%09%09%09%22rematrix.c%22,%0A%09%09%09%22resample.c%22,%0A%09%09%09%22swresample.c%22,%0A%09%09
+staticChromaprint = False%0A%0AffmpegFiles = %5B%22../ffmpeg.c%22%5D + %5C%0A%09(glob(%22../chromaprint/*.cpp%22) if staticChromaprint else %5B
 %5D)%0A%0A
@@ -573,73 +573,8 @@
  %5B%5D)
- +%0A%09(%5B%22-I%22, %22../libs/libswresample%22%5D if staticSwresample else %5B%5D)
 %0A)%0A%0A

Revert "compile.py: initial code to have libswresample statically linked"