OS:=$(shell uname -s)
ifeq ($(OS),Darwin)
	MACOS_LD_FLAGS=-lintl -framework SystemConfiguration -framework CoreFoundation
	MACOS_CFLAGS=-arch $(shell uname -m)
endif

JAVACFLAGS=@JAVA_CPPFLAGS@
JAVALDFLAGS=@JAVA_LDFLAGS@
JAVA_ENABLED=@JAVA_ENABLED@
JAVA_FILE=@JAVA_FILE@

MS_CFLAGS=@MS_CFLAGS@
MS_LDFLAGS=@MS_LIBS@
MS_FILE=@MS_FILE@

CFLAGS=${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
LDFLAGS=-lcurl -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS} -lfcgi -lcrypto ${MS_LDFLAGS} ${MACOS_LD_FLAGS}

PHPCFLAGS=@PHP_CPPFLAGS@
PHPLDFLAGS=@PHP_LDFLAGS@
PHP_ENABLED=@PHP_ENABLED@
PHP_FILE=@PHP_FILE@


PYTHONCFLAGS=@PYTHON_CPPFLAGS@
PYTHONLDFLAGS=@PYTHON_LDFLAGS@
PYTHON_ENABLED=@PYTHON_ENABLED@
PYTHON_FILE=@PYTHON_FILE@

JSCFLAGS=@JS_CPPFLAGS@
JSLDFLAGS=@JS_LDFLAGS@
JS_ENABLED=@JS_ENABLED@
JS_FILE=@JS_FILE@

XML2CFLAGS=@XML2_CPPFLAGS@
XML2LDFLAGS=@XML2_LDFLAGS@

GDAL_CFLAGS=@GDAL_CFLAGS@
GDAL_LIBS=@GDAL_LIBS@

PERLCFLAGS=@PERL_CPPFLAGS@
PERLLDFLAGS=@PERL_LDFLAGS@
PERL_ENABLED=@PERL_ENABLED@
PERL_FILE=@PERL_FILE@


all: zoo_loader.cgi

main_conf_read.tab.c: main_conf_read.y service.h
	bison -p cr -d main_conf_read.y

main_conf_read.tab.o: main_conf_read.tab.c service.h
	g++ ${CFLAGS} -c main_conf_read.tab.c

lex.cr.c: main_conf_read.y main_conf_read.l main_conf_read.tab.c service.h
	flex -P cr --header-file main_conf_read.l

lex.cr.o: lex.cr.c service.h
	g++ ${CFLAGS} -c lex.cr.c

service_conf.tab.c: service_conf.y service.h
	bison -p sr -d service_conf.y

service_conf.tab.o: service_conf.tab.c service.h
	g++ ${CFLAGS} -c service_conf.tab.c

lex.sr.c: service_conf.y service_conf.l service_conf.tab.c service.h
	flex -P sr --header-file service_conf.l

lex.sr.o: lex.sr.c service.h
	g++ ${CFLAGS} -c lex.sr.c

ulinet.o: ulinet.c
	gcc ${XML2CFLAGS} ${CFLAGS} ${JSCFLAGS} ${JS_ENABLED} -c ulinet.c

service_internal.o: service_internal.c service.h
	gcc ${JS_ENABLED} ${JSCFLAGS} ${XML2CFLAGS} ${CFLAGS} -fPIC -c service_internal.c

service_internal_ms.o: service_internal_ms.c
	gcc ${JS_ENABLED} ${JSCFLAGS} ${XML2CFLAGS} ${CFLAGS} -fPIC -c service_internal_ms.c

service_internal_python.o: service_internal_python.c service.h
	g++ ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c service_internal_python.c

service_internal_php.o: service_internal_php.c service.h
	g++ -c ${XML2CFLAGS} ${PHPCFLAGS} ${CFLAGS}  ${PHP_ENABLED} service_internal_php.c

service_internal_perl.o: service_internal_perl.c service.h
	gcc -c ${XML2CFLAGS} ${PERLCFLAGS} ${CFLAGS}  ${PERL_ENABLED} service_internal_perl.c


service_internal_java.o: service_internal_java.c service.h
	gcc -c ${XML2CFLAGS} ${JAVACFLAGS} ${CFLAGS} ${JAVA_ENABLED} service_internal_java.c

service_internal_js.o: service_internal_js.c service_internal_js.h
	gcc ${XML2CFLAGS} ${JSCFLAGS} ${CFLAGS} ${JS_ENABLED} -c service_internal_js.c


service_loader.o: service_loader.c service.h
	g++ -c ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} service_loader.c

zoo_service_loader.o: zoo_service_loader.c service.h
	g++ -g -O2 ${XML2CFLAGS} ${CFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${PYTHON_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o

zoo_loader.cgi: zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o service_internal.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE}
	g++ -g -O2 ${JSCFLAGS} ${PHPCFLAGS}  ${PERLCFLAGS}   ${JAVACFLAGS} ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c zoo_loader.c  -fno-common -DPIC -o zoo_loader.o
	g++  ${JSCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o service_internal.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi ${LDFLAGS}

install:
	@echo "##############################################################################"
	@echo "# This won't install anything !!!                                            #"
	@echo "#                                                                            #"
	@echo "# Please copy the zoo_loader.cgi and its companion main.cfg into your cgbin  #"
	@echo "# directory.                                                                 #"
	@echo "##############################################################################"

clean:
	rm -f *.o *.zo *.eo *.tab.c *.tab.h *.sr.c* service_loader lex.* *.lreg *.sibling service_loader.dSYM 
