#
#	Only change CONFIG to the right .mk file.
#

CONFIG = system.mk

#
#	Do Not Edit below here. Everything should be setup in the config
#

include $(CONFIG)

DEP_INCS = cflags.h ntserv/data.c ntserv/data.h ntserv/defs.h \
	config.h system.mk null

all: $(PMAKE) server startup utils rsa_utils do_xsg do_pledit do_robots

depend: system.mk
	cd ntserv; make depend
	cd robots; make depend
#	cd ntstart; make depend
	cd newstartd; make depend
	cd tools; make depend
	cd pledit; make depend
	cd keycomp; make depend
	-@if [ -f res-rsa/Makefile ]; then \
		cd res-rsa; echo "	cd res-rsa; make depend"; make depend; \
	fi
	-@if [ -f xsg/Makefile ]; then \
		cd xsg; echo "	cd xsg; make depend"; make depend; \
	fi
	-@if [ -f pledit/Makefile ]; then \
		cd pledit; echo "	cd pledit; make depend"; make depend; \
	fi

null: 
	touch null
	make depend

system.mk: system.mk.in config.h.in configure
	./configure -v

config.h: system.mk
	touch config.h

# make the server binaries and such
server: $(DEP_INCS)
	cd ntserv; $(MAKE) 

do_robots: $(DEP_INCS)
	cd robots; $(MAKE)

do_xsg: $(DEP_INCS)
	-@if [ -d $(PWD)/xsg ]; then \
		cd xsg; echo "	cd xsg; $(MAKE)"; $(MAKE); \
	fi

do_pledit: $(DEP_INCS)
	cd pledit; $(MAKE) 


utils: $(DEP_INCS)
	-@if [ -f res-rsa/Makefile ]; then \
		cd res-rsa; echo "	cd res-rsa; $(MAKE)"; $(MAKE); \
	fi
	cd tools; $(MAKE) 

rsa_utils: $(DEP_INCS)
	cd keycomp; $(MAKE)

startup: $(DEP_INCS)
	cd newstartd; $(MAKE)
#	cd ntstart; $(MAKE)

cflags.h: Makefile $(CONFIG)
	cd ntserv; make cflags

lint:
	cd ntserv; make lint
	cd robots; make lint
	cd keycomp; make lint
	cd tools; make lint

clean:
	cd ntserv; make clean
	cd robots; make clean
	cd newstartd; make clean
#	cd ntstart; make clean
	cd tools; make clean
	cd pledit; make clean
	-@if [ -f res-rsa/Makefile ]; then \
		cd res-rsa; echo "	cd res-rsa; make clean"; make clean; \
	fi
	cd keycomp; make clean
	-@if [ -f xsg/Makefile ]; then \
		cd xsg; echo "	cd xsg; make clean"; make clean; \
	fi
	rm -f make.out name.o name cflags.h *~ #*#

reallyclean:
	cd ntserv; make reallyclean
	cd robots; make reallyclean
	cd newstartd; make reallyclean
#	cd ntstart; make reallyclean
	cd tools; make reallyclean
	cd pledit; make reallyclean
	-@if [ -f res-rsa/Makefile ]; then \
		cd res-rsa; echo "	cd res-rsa; make reallyclean"; \
		make reallyclean; \
	fi
	cd keycomp; make reallyclean
	-@if [ -f xsg/Makefile ]; then \
		cd xsg; echo "	cd xsg; make reallyclean"; make reallyclean; \
	fi
	rm -f make.out cflags.h tools/trekon *~ #*#

installbin: all
	cd ntserv; make installbin
	cd robots; make install
	cd newstartd; make install
#	cd ntstart; make install
	-@if [ -f res-rsa/Makefile ]; then \
		cd res-rsa; echo "	cd res-rsa; make install"; make install; \
	fi
	cd tools; make install
	cd keycomp; make install
	cd pledit; make install
	-@if [ -f xsg/Makefile ]; then \
		cd xsg; echo "	cd xsg; make install"; make install; \
	fi
	

install: installbin 
	cd docs; make install
	cd tools; make installtrekon

name: name.c version.h patchlevel.h
	$(CC) $(CFLAGS) -o name name.c

patches: name rmdependencies
	cd $(PATCHDIR)/Server; make name
	@echo Making Server`name`\-`$(PATCHDIR)/Server/name`.diffs
	-rm Server`name`\-`$(PATCHDIR)/Server/name`.diffs
	-for f in `cat manifest` ; do \
	  if [ ! -L ../$${f} ] ; then \
	    if [ ! -f $(PATCHDIR)/$${f} ] ; then \
	      touch $(PATCHDIR)/$${f} ; \
	    fi ; \
	    diff -c $(PATCHDIR)/$${f} ../$${f} >> Server`name`\-`$(PATCHDIR)/Server/name`.diffs ;\
	  fi ; \
	done

# for people on dynix, parallel compiling:
# /bin/make BINDIR="$(BINDIR)"parallel
#parallel: & pntserv startup xsg
# commented out because of all the whining from Ultrix and other people
# whose make BINDIR="$(BINDIR)"BINDIR="$(BINDIR)"barfs on it.  All I can say is: compile gnumake.


# more fun parallel compiling
pntserv:
	cd ntserv; /bin/make BINDIR="$(BINDIR)"BINDIR="$(BINDIR)"-P10 CC="$(CC)" EXTRALIBS="$(EXTRALIBS)" \
	    EXTRACFLAGS="$(EXTRACFLAGS)" XTREKDIR="$(XTREKDIR)" parallel

#archive is for yourself so you get absolutely everything including RCS files
archive: reallyclean name
	rmdependencies
	tar cvf - . | compress - > Server`name`.archive.tar.Z

# a distribution of code. xsg is not included in distribution
# 	-cd ntserv; rm reserved.c; ln -s reserved.c.shipped reserved.c
# ^^^^^^^^ intelligent people will use links so let's not make life hard

dist: name rmdependencies
	cd ..; tar cvf - `cat Server/manifest` | gzip - > Server`Server/name`.tar.gz

distxsg: name
	cd ..; tar cvf - `cat Server/manifest.xsg` | gzip - > xsg2.1-`Server/name`.tar.gz

distrsa: name
	cd ..; tar cvf - `cat Server/manifest.rsa` | gzip - > res-rsa`Server/name`.tar.gz
	cd ..; crypt $(KEY) < res-rsa`Server/name`.tar.gz > res-rsa`Server/name`.tar.gz.crypt
	cd ..; rm res-rsa`Server/name`.tar.gz

# a distribution of the bare necessities
# maybe inefficient but it works
bindist: install
	cd docs; make bindist

binclean:
	cd docs; make binclean

rmdependencies:
	cd ntserv; make rmdependencies
	cd robots; make rmdependencies
#	cd ntstart; make rmdependencies
	cd newstartd; make rmdependencies
	cd tools; make rmdependencies
	cd pledit; make rmdependencies
	cd keycomp; make rmdependencies
	-@if [ -f res-rsa/Makefile ]; then \
		cd res-rsa; echo "      cd res-rsa; make rmdependencies"; \
		make rmdependencies; \
	fi
	-@if [ -f xsg/Makefile ]; then \
		cd xsg; echo "  cd xsg; make rmdependencies"; \
		make rmdependencies; \
	fi

#########
# DO NOT DELETE THIS LINE -- make depend depends on it.
