FXC="fenix-fxc"
FXI="fenix-fxi"

all: plugins/ttf.so plugins/agua.so plugins/fgfx.so plugins/fire.so plugins/image.so \
	plugins/net.so plugins/mpeg.so plugins/mixer.so plugins/exec.so plugins/tcpsock.so \
	plugins/fsock.so

fullclean: distclean
	rm -f */aclocal.m4 */configure */Makefile.in
	rm -f */config.guess */config.sub */config.status */config.log

distclean: clean_tests
	@printf "\n == CLEAN: $@ ==\n\n"
	for i in */; do [ ! -f "$$i/Makefile" ] || $(MAKE) -C "$$i" distclean || exit 1 ; done
	rm -f plugins/*

clean: clean_tests
	@printf "\n == CLEAN: $@ ==\n\n"
	for i in */; do [ ! -f "$$i/Makefile" ] || $(MAKE) -C "$$i" clean || exit 1 ; done

clean_tests:
	@printf "\n == CLEAN: $@ ==\n\n"
	rm -f test_*.prg test_*.dcb

%/configure: %/configure.ac
	@printf "\n == CONFIGURE: $@ ==\n\n"
	cd `echo $@ | sed -e "s|/configure||"` && \
		autoreconf --force --verbose
	touch "$@"

%/config.status: %/configure
	@printf "\n == CONFIGURE: $@ ==\n\n"
	cd `echo $@ | sed -e "s|/config.status||"` && \
		./configure CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" $(CONFFLAGS) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--with-vorbis-includes=/usr/include/vorbis/
	touch "$@"

plugins:
	mkdir -p plugins

plugins/agua.so: plugins agua-1.0/configure agua-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C agua-1.0
	cp agua-1.0/.libs/libagua.so "$@"

plugins/fgfx.so: plugins fgfx-1.0/configure fgfx-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C fgfx-1.0
	cp fgfx-1.0/.libs/libfgfx.so "$@"

plugins/fire.so: plugins fire-1.0/configure fire-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C fire-1.0
	cp fire-1.0/.libs/libfire.so "$@"

plugins/image.so: plugins image-1.0/configure image-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C image-1.0
	cp image-1.0/.libs/libimage.so "$@"

plugins/net.so: plugins net-1.0/configure net-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C net-1.0
	cp net-1.0/.libs/libnet.so "$@"

plugins/mpeg.so: plugins mpeg-1.0/configure mpeg-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C mpeg-1.0
	cp mpeg-1.0/.libs/libmpeg.so "$@"

plugins/mixer.so: plugins mixer-1.0/configure mixer-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C mixer-1.0
	cp mixer-1.0/.libs/libmixer.so "$@"

plugins/ttf.so: plugins ttf-1.0/configure ttf-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C ttf-1.0
	cp ttf-1.0/.libs/libttf.so "$@"

plugins/exec.so: plugins exec-0.4a/configure exec-0.4a/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C exec-0.4a
	cp exec-0.4a/.libs/libexec.so "$@"

plugins/tcpsock.so: plugins tcpsock-2.0/configure tcpsock-2.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C tcpsock-2.0
	cp tcpsock-2.0/.libs/libtcpsock.so "$@"

plugins/fsock.so: plugins fsock-1.0/configure fsock-1.0/config.status
	@printf "\n == BUILD: $@ ==\n\n"
	make -C fsock-1.0
	cp fsock-1.0/.libs/libfsock.so "$@"

check: check_symbols test_load.dcb

check_symbols:
	@printf "\n == CHECK: $@ ==\n\n"
	@for i in plugins/*.so; do \
		if objdump -T "$$i" | grep "RegisterFunctions" >/dev/null ; then \
			echo \"$$i\" has the dynamic symbol \"RegisterFunctions\" \[OK\] ; \
		else \
			echo \"$$i\" does not have the dynamic symbol \"RegisterFunctions\" \[ERROR\] ; \
			exit 1 ; \
		fi \
	done

	@for i in plugins/*.so; do \
		if objdump -T "$$i" | grep "PluginVersion" >/dev/null ; then \
			echo \"$$i\" has the dynamic symbol \"PluginVersion\" \[OK\] ; \
		else \
			echo \"$$i\" does not have the dynamic symbol \"PluginVersion\" \[ERROR\] ; \
			exit 1 ; \
		fi \
	done

.SUFFIXES: .prg .dcb

.prg.dcb:
	@printf "\n == CHECK: $@ ==\n\n"
	$(FXC) $< -o $@

test_load.prg:
	echo "PROGRAM TestLoad;" > $@
	for i in plugins/*.so; do \
		echo "IMPORT \"`echo \"$$i\" | sed -e \"s/\.so//\"`\"" >> $@ ; \
	done
	echo "GLOBAL" >> $@
	echo "BEGIN" >> $@
	echo "END" >> $@

update-config:
	@printf "\n == CONFIGURE: $@ ==\n\n"
	for i in */ ; do \
		if [ -e "$$i/configure.ac" ]; then \
			echo "Copying config.sub and config.guess files in \"$$i\"" ; \
			test -e /usr/share/misc/config.sub && \
				cp -f /usr/share/misc/config.sub "$$i/config.sub" ; \
			test -e /usr/share/misc/config.guess && \
				cp -f /usr/share/misc/config.guess "$$i/config.guess" ; \
		fi \
	done

configure: update-config
	@printf "\n == CONFIGURE: $@ ==\n\n"
	for i in */ ; do \
		if [ -e "$$i/configure.ac" ]; then \
			printf "\n - Creating autotools files in \"$$i\" -\n\n" ; \
			cd "$$i" && autoreconf --force --verbose || exit 1 ; \
			cd - ; \
		fi \
	done
