PREFIX?=C:/bsdconv
BSDCONV_PATH?=${PREFIX}
CFLAGS+=-Wall -O2 -DPREFIX='"${PREFIX}"' -DBSDCONV_PATH='"${BSDCONV_PATH}"'
CC=mingw32-cc

all: builddir libbsdconv bsdconv_mktable bsdconv modules meta

builddir:
	mkdir -p build/
	mkdir -p build/include
	mkdir -p build/mofules
	mkdir -p build/modules/from
	mkdir -p build/modules/inter
	mkdir -p build/modules/to

libbsdconv: builddir
	$(CC) ${CFLAGS} src/missing_func.c src/libbsdconv.c -shared -o build/libbsdconv.dll

bsdconv: builddir libbsdconv src/bsdconv.c
	$(CC) ${CFLAGS} -lbsdconv -L./build/ src/bsdconv.c -o build/bsdconv.exe

bsdconv_mktable: builddir src/bsdconv_mktable.c
	$(CC) ${CFLAGS} src/missing_func.c src/bsdconv_mktable.c -o build/bsdconv_mktable.exe

codecs_table: builddir bsdconv_mktable
	cd modules && \
	find */*.txt -type f | awk -F. '{cmd="bsdconv_mktable ../modules/"$$1"."$$2" modules/"$$1; print(cmd);}' > ../build/mk_table.bat

codecs_callback: builddir libbsdconv
	cd modules && \
	find */*.c -type f | awk -F. '{cmd="$(CC) ${CFLAGS} -shared -lbsdconv -L../build/ -o ../build/modules/"$$1".dll "$$1"."$$2" -lwsock32"; system(cmd);}'

modules: builddir codecs_table codecs_callback

meta:
	cp src/bsdconv.h build/include/
	cp tools/setEnvVar.bat build/

clean:
	rm -rf build
