#                               MAIN MAKEFILE
#                               -------------

ROOT_DIR             = @ROOT_DIR@
INSTALL_DIRNAME      = @INSTALL_DIRNAME@
INSTALL_DIR          = @INSTALL_DIR@
LINKS_DIR            = @LINKS_DIR@
INSTALL              = @INSTALL@
INSTALL_PROGRAM      = @INSTALL_PROGRAM@
INSTALL_DATA         = @INSTALL_DATA@
MAKE_LE_DIRS         = @MAKE_LE_DIRS@
MAKE_FD_DIRS         = @MAKE_FD_DIRS@
MAKE_INSTALL_LINKS   = @MAKE_INSTALL_LINKS@
MAKE_INSTALL_LE      = @MAKE_INSTALL_LE@
MAKE_INSTALL_FD      = @MAKE_INSTALL_FD@
MAKE_INSTALL_DOC     = @MAKE_INSTALL_DOC@
MAKE_INSTALL_EXAMPLES= @MAKE_INSTALL_EXAMPLES@
MAKE_UNINSTALL_PKG   = @MAKE_UNINSTALL_PKG@
LIB_LINEDIT          = @LIB_LINEDIT@
LIB_ENGINE_PL        = @LIB_ENGINE_PL@
LIB_BIPS_PL          = @LIB_BIPS_PL@
LIB_ENGINE_FD        = @LIB_ENGINE_FD@
LIB_BIPS_FD          = @LIB_BIPS_FD@
TOP_LEVEL            = @TOP_LEVEL@
GPLC                 = @GPLC@
HEXGPLC              = @HEXGPLC@
LN_S                 = @LN_S@
RANLIB               = @RANLIB@
EXEEXT               = @EXEEXT@

LE_DIRS       = Linedit
FD_DIRS       = Fd2C EngineFD BipsFD
SUB_DIRS      = TopComp EnginePl Wam2Ma Ma2Asm $(MAKE_LE_DIRS) \
                BipsPl Pl2Wam $(MAKE_FD_DIRS)
SUB_DIRS_ALL  = TopComp EnginePl Wam2Ma Ma2Asm $(LE_DIRS) \
                BipsPl Pl2Wam $(FD_DIRS)

DOC_FILES     = ../doc/???*.dvi ../doc/???*.ps
DOC_FILES_HTML=../doc/Html/???*.html ../doc/Html/???*.gif

# optional targets

INSTALL_LINKS      = install-links
INSTALL_LE         = install-le
INSTALL_FD         = install-fd
INSTALL_DOC        = install-doc
INSTALL_EXAMPLES   = install-examples

UNINSTALL_ALL      = uninstall-all
UNINSTALL_IN_PLACE = uninstall-in-place


all:
	(cd EnginePl; $(MAKE) config)
	. ./SETVARS;for i in $(SUB_DIRS);do (cd $$i; $(MAKE)) || exit 1; done;\
	(cd TopComp; $(MAKE) $(TOP_LEVEL)$(EXEEXT))


install-strip:
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install

install: install-package $(MAKE_INSTALL_LINKS)

install-package: install-system $(MAKE_INSTALL_DOC) $(MAKE_INSTALL_EXAMPLES)

install-system: install-dirs install-system0 $(MAKE_INSTALL_LE) \
                $(MAKE_INSTALL_FD) do-ranlib

install-system0:
	$(INSTALL_PROGRAM) TopComp/$(GPLC)$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_PROGRAM) TopComp/$(HEXGPLC)$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_PROGRAM) TopComp/$(TOP_LEVEL)$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_PROGRAM) Pl2Wam/pl2wam$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_PROGRAM) Wam2Ma/wam2ma$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_PROGRAM) Ma2Asm/ma2asm$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_DATA) EnginePl/$(LIB_ENGINE_PL) $(INSTALL_DIR)/lib
	$(INSTALL_DATA) EnginePl/obj_begin.o $(INSTALL_DIR)/lib
	$(INSTALL_DATA) EnginePl/obj_end.o $(INSTALL_DIR)/lib
	$(INSTALL_DATA) BipsPl/$(LIB_BIPS_PL) $(INSTALL_DIR)/lib
	$(INSTALL_DATA) BipsPl/top_level.o $(INSTALL_DIR)/lib
	$(INSTALL_DATA) BipsPl/all_pl_bips.o $(INSTALL_DIR)/lib
	$(INSTALL_DATA) BipsPl/debugger.o $(INSTALL_DIR)/lib
	for i in EnginePl/*.h BipsPl/*.h; do \
$(INSTALL_DATA) $$i $(INSTALL_DIR)/include; done

do-ranlib:
	for i in $(INSTALL_DIR)/lib/*.a; do \
$(RANLIB) $$i; done


install-le:
	$(INSTALL_DATA) Linedit/$(LIB_LINEDIT) $(INSTALL_DIR)/lib
	$(INSTALL_DATA) Linedit/linedit.h $(INSTALL_DIR)/include


install-fd:
	$(INSTALL_PROGRAM) Fd2C/fd2c$(EXEEXT) $(INSTALL_DIR)/bin
	$(INSTALL_DATA) EngineFD/$(LIB_ENGINE_FD) $(INSTALL_DIR)/lib
	$(INSTALL_DATA) BipsFD/$(LIB_BIPS_FD) $(INSTALL_DIR)/lib
	$(INSTALL_DATA) BipsFD/all_fd_bips.o $(INSTALL_DIR)/lib
	for i in EngineFD/*.h BipsFD/*.h; do \
$(INSTALL_DATA) $$i $(INSTALL_DIR)/include; done


install-doc:
	for i in $(DOC_FILES) ; do \
$(INSTALL_DATA) $$i $(INSTALL_DIR)/doc/; done
	for i in $(DOC_FILES_HTML) ; do \
$(INSTALL_DATA) $$i $(INSTALL_DIR)/doc/Html; done


install-examples:
	for i in ../ExamplesPl/*; do \
$(INSTALL_DATA) $$i $(INSTALL_DIR)/ExamplesPl/; done
	for i in ../ExamplesFD/*; do \
$(INSTALL_DATA) $$i $(INSTALL_DIR)/ExamplesFD/; done



install-dirs:
	./mkinstalldirs $(INSTALL_DIR) $(INSTALL_DIR)/bin \
                        $(INSTALL_DIR)/include $(INSTALL_DIR)/lib \
                        $(INSTALL_DIR)/doc $(INSTALL_DIR)/doc/Html \
	                $(INSTALL_DIR)/ExamplesPl $(INSTALL_DIR)/ExamplesFD


install-links: clean-links
	./mkinstalldirs $(LINKS_DIR)
	(cd $(LINKS_DIR) ; $(LN_S) $(INSTALL_DIR)/bin/* .)



clean-links:
	(cd $(INSTALL_DIR)/bin; for i in *; do \
if test -f $(LINKS_DIR)/$$i; then rm -f $(LINKS_DIR)/$$i; fi; done)


uninstall: $(MAKE_UNINSTALL_PKG)

uninstall-all: clean-links
	-rm -rf $(INSTALL_DIR)

uninstall-in-place:
	-rm -rf $(INSTALL_DIR)/bin
	-rm -rf $(INSTALL_DIR)/lib
	-rm -rf $(INSTALL_DIR)/include


check:
	@echo "No checks implemented (yet)."



RUN_PL_DIRS = EnginePl BipsPl 
RUN_FD_DIRS = EngineFD BipsFD
RUN_SUB_DIRS= $(RUN_PL_DIRS) $(RUN_PL_DIRS)



clean:
	for i in $(SUB_DIRS_ALL); do (cd $$i; $(MAKE) clean); done

clean-pl:
	for i in $(RUN_PL_DIRS); do (cd $$i; $(MAKE) clean); done

clean-fd:
	for i in $(RUN_FD_DIRS); do (cd $$i; $(MAKE) clean); done

clean-pl-fd:
	for i in $(RUN_PL_DIRS) $(RUN_FD_DIRS);do (cd $$i; $(MAKE) clean); done

distclean:
	rm -f config.status config.log config.cache EnginePl/config.h
	for i in $(SUB_DIRS_ALL); do (cd $$i; $(MAKE) distclean); done




DST=./${INSTALL_DIRNAME}

dist:
	rm -f $(DST)
	(while read f;do \
            if [ x$$f != x ]; then \
                files="$$files $(INSTALL_DIRNAME)/$$f"; \
            fi; \
         done <DISTRIB_FILES; \
	 ln -s $(ROOT_DIR) $(DST); \
	 tar cvf $(INSTALL_DIRNAME).tar $$files)
	gzip -f $(INSTALL_DIRNAME).tar
	rm -f $(DST)
	@echo "distribution file: $(INSTALL_DIRNAME).tar.gz"
