# top level makefile for p4

SHELL=/bin/sh

MFLAGS = 
#MFLAGS = -i    # for alliant fx/8
  
P4_DIRS = util lib alog usc include bin servers 

EXAMPLES_DIRS = contrib messages monitors

ALL_DIRS = $(P4_DIRS) $(EXAMPLES_DIRS) lib_f messages_f contrib_f doc usc/usctest

DIRS = $(ALL_DIRS)

RM = /bin/rm

all:
	@echo making p4 for machine=$(MACHINE)
	@make makefiles MACHINE=$(MACHINE)
	@make $(MFLAGS) links MACHINE=$(MACHINE)
	@for dir in $(DIRS) ;\
	  do \
	  echo " " ;\
	  echo making p4 in directory $$dir ;\
	  (cd $$dir; make $(MFLAGS) default) ;\
	  done 

p4:
	@echo making p4 for machine=$(MACHINE)
	@make makefiles MACHINE=$(MACHINE)
	@make $(MFLAGS) links MACHINE=$(MACHINE)
	@for dir in $(P4_DIRS) ;\
	  do \
	  echo " " ;\
	  echo making p4 in directory $$dir ;\
	  (cd $$dir; make $(MFLAGS) default) ;\
	  done 

makefiles:
	@(cd util; make -f makefile.proto defs.MD MACHINE=$(MACHINE))
	@echo "making Makefiles in " $(DIRS)
	@for dir in $(DIRS) ;\
	  do \
	  cat util/defs.MD $$dir/makefile.proto > $$dir/Makefile ;\
	  done 

#  @echo "#define  P4_$_MACHINE_TYPE \"$(MACHINE)\"" >> lib/p4_config.h
config_hdr:
	@echo "#define P4_COMPILED_TIME  \"`date`\"" > lib/p4_config.h
	@echo "#define  P4_MACHINE_TYPE \"$(MACHINE)\"" >> lib/p4_config.h
	@echo "#ifndef  $(MACHINE)" >> lib/p4_config.h
	@echo "#define  $(MACHINE)" >> lib/p4_config.h
	@echo "#endif  "            >> lib/p4_config.h
	@cat OPTIONS >> lib/p4_config.h

links:
	@if [ -f lib/alog.h ] ;\
	then\
	    true ;\
	else\
	    ln alog/alog*.h lib ;\
	    ln alog/alog*.h include ;\
	fi
	@if [ -f lib/usc.h ] ;\
	then\
	    true ;\
	else\
	    ln usc/usc*.h lib ;\
	    ln usc/usc*.h include ;\
	fi
	@if [ -f lib/p4_config.h ] ;\
	then\
	    true ;\
	else\
	    make config_hdr MACHINE=$(MACHINE) ;\
	fi
	@if [ -f include/p4.h ] ;\
	then\
	    true ;\
	else\
	    ln lib/p4*.h include ;\
	fi
	@if [ -f messages_f/p4f.h ] ;\
	then\
	    true ;\
	else\
	    ln lib_f/p4f.h messages_f ;\
	fi
	@if [ -f contrib_f/p4f.h ] ;\
	then\
	    true ;\
	else\
	    ln lib_f/p4f.h contrib_f ;\
	fi

clean:  
	@for dir in $(DIRS) ;\
	  do \
	  echo cleaning $$dir ;\
	  (cd $$dir; make clean) ;\
	  done
	$(RM) -f *~ *.o *.bak

dist:	realclean   

realclean:
	$(RM) -f *~ *.o *.bak
	@for dir in $(DIRS) ;\
	  do \
	  echo cleaning $$dir ;\
	  (cd $$dir; make realclean) ;\
	  done

Makefiles: makefiles

INSTALLDIR = ./$(MACHINE)
install:
	@echo installing p4 in $(INSTALLDIR)
	@if [ -d $(INSTALLDIR) ] ;\
	then\
	    echo 'using existing directory $(INSTALLDIR)' ;\
	else\
	    mkdir $(INSTALLDIR) ;\
	fi
	@cp Makefile $(INSTALLDIR)
	@mkdir $(INSTALLDIR)/util
	@cp util/* $(INSTALLDIR)/util
	@mkdir $(INSTALLDIR)/include
	@cp include/*.h $(INSTALLDIR)/include
	@mkdir $(INSTALLDIR)/lib
	@cp lib/libp4.a $(INSTALLDIR)/lib
	@grep RANLIB util/defs.MD > $(INSTALLDIR)/lib/makeranlib
	@echo "doran:" >> $(INSTALLDIR)/lib/makeranlib
	@echo "	$$(RANLIB) libp4.a" >> $(INSTALLDIR)/lib/makeranlib
	@(cd  $(INSTALLDIR)/lib; make -f makeranlib doran; $(RM) makeranlib)
	@mkdir $(INSTALLDIR)/lib_f
	@cp lib_f/libp4_f.a lib_f/*.h $(INSTALLDIR)/lib_f
	@grep RANLIB util/defs.MD > $(INSTALLDIR)/lib_f/makeranlib
	@echo "doran:" >> $(INSTALLDIR)/lib_f/makeranlib
	@echo "	$$(RANLIB) libp4_f.a" >> $(INSTALLDIR)/lib_f/makeranlib
	@(cd  $(INSTALLDIR)/lib_f; make -f makeranlib doran; $(RM) makeranlib)
	@mkdir $(INSTALLDIR)/bin
	@cp bin/* $(INSTALLDIR)/bin
	@mkdir $(INSTALLDIR)/examples
	@cp messages/sr_test.c messages/sr_user.h  \
	  messages/systest.c messages/makefile.proto \
	  messages/runcube messages/rundelta \
	  messages/procgroup $(INSTALLDIR)/examples
	@sed -e "s@P4_HOME_DIR = ..@P4_HOME_DIR = $(INSTALLDIR)@" \
	  $(INSTALLDIR)/examples/makefile.proto >  $(INSTALLDIR)/temp
	@mv $(INSTALLDIR)/temp  $(INSTALLDIR)/examples/makefile.proto
	@make makefiles DIRS=$(INSTALLDIR)/examples
	@(cd $(INSTALLDIR)/examples; make sr_test systest)
	@mkdir $(INSTALLDIR)/examples_f
	@cp messages_f/sr_test.f messages_f/p4f.h  \
	  messages_f/makefile.proto \
	  messages_f/procgroup $(INSTALLDIR)/examples_f
	@sed -e "s@P4_HOME_DIR = ..@P4_HOME_DIR = $(INSTALLDIR)@" \
	  $(INSTALLDIR)/examples_f/makefile.proto >  $(INSTALLDIR)/temp
	@mv $(INSTALLDIR)/temp  $(INSTALLDIR)/examples_f/makefile.proto
	@make makefiles DIRS=$(INSTALLDIR)/examples_f
	@(cd $(INSTALLDIR)/examples_f; make sr_test)
	@echo installed p4 in $(INSTALLDIR)
