#
# $Header: Makefile,v 1.15 88/11/14 20:08:50 muir Locked $
#
#
#
# Makefile for making complete empire game
#
# things to make
#
#	makefile   - do this first, to make the makefile know where the src is
#	first	   - do this first (after "make makefile")
#	everything - a combo make makefile ; make first ; make install
#
#	all	   - compile everything
#	install	   - installs emp_serv and new daemons
#		     note: you don't need to shut down the game
#		     to install a new emp_serv
#
#	start	- installs and fires up new daemons
#	stop	- stops daemons
#	restart - stop & start
#	changes - install & restart
#	tags	- tags file
#	depend  - Makefile depend info
#
#	fixdeadserver - will restart a hung server
#
#	newworld - makes a new world.  quick & dirty
#
#	update	- will force the transaction manager to do a world update
#
# variables to modify:
#
#	BSD={4.2,4.3}
#	OOB={,-DKILLSERVER,-DOLDSOCKETS}
#	SERVDIR
#	GAMEDIR
#	MANDIR
#	GLOBALCFLAGS
#	INDENT={,2>&1 |sed 's,^,	,'}

SRCDIR= 
# note: SRCDIR is filled in automatically by "make makefile"

BSD= 4.3
#BSD= 4.2

INDENT= 2>&1 | sed 's,^,	,'
#INDENT= 
# set to 2>&1|sed 's,^,	,' if you are backgrounding the make
# this will allow you to see which make is running (and more
# importantly, which make it died in)


#
# global cflag -DVSPRINTF says to use vsprintf routines 
# 	(on suns > 3.0 I think + others maybe)
# global cflag -DRANDOM should be set iff you want population to
# 	grow smoothly rather than predictably
# global cflag -g is allways a good idea...
# global cflag -DSHIPGAINCONSTANT says that ships should gain effic
#	at a constant rate rather than dependant on workforce and tonnage
# global cflag -DRANDOMTIME says that rather than charging at least 1 min
#	no matter how much time was actually spent logged in, it should do
#	it randomly depending on how much time was actaully spent.
#
GLOBALCFLAGS = -DRANDOM


# OOB (out of band) data is broken under BSD 4.2.  There are two 
# "fixes":
#
#    KILLSERVER which is a very bad kludge but is GUARENTEED to work
#    OLDSOCKETS which makes use of an undocumented feature on 4.2
#
# not that neither is needed under 4.3
#
#OOB = -DKILLSERVER	# use a ^C daemon
OOB = -DOLDSOCKETS	# use an undocumented feature of BSD 4.2
#OOB = 			# don't use either: oob data works (4.3BSD)

SERVDIR= $(SRCDIR)/EMP
# this must agree with gamesdefs.h

GAMEDIR= $(SRCDIR)/BIN
# where player binarys get put

MANDIR= /tmp
# where player and system manuals get put
MANEXT= .p
# manual page extention

CC= /bin/cc

LD= /bin/ld

NICE =
#NICE = nice +5

INSCP= /bin/cp
#INSCP= /bin/ln
# set to /bin/ln if installing on the same filesystem and ld doesn't
# mind linking over running binarys

# ------------------------------------------------------------------------

DIRS= tm UTIL client empserver
ALLDIRS= $(DIRS) empmain/SUBS empmain/COMS empmain/gen empmain INFO lib

CFLAGS= -Iempmain ${GLOBALCFLAGS}

CFILES= empmain/empglb.c empmain/empmod.c client/sailio.c empvers.c
OFILES= empmain/empglb.o empmain/empmod.o client/sailio.o empvers.o

PWD= $(SRCDIR)

BINDIR= $(SRCDIR)/BIN

BINARYS= $(SERVDIR)/BIN/emp_serv $(GAMEDIR)/empire \
	$(SERVDIR)/empire_tmserver $(SERVDIR)/empserver $(SERVDIR)/killd

all: .bsd .depend .tags glib.a dirs

makefile: .pwd

first: .pwd .mkdir .tags

everything:;
	@ echo make makefile
	@ make makefile $(INDENT)
	@ echo make first
	@ make first $(INDENT)
	@ echo make install
	@ make install $(INDENT)

changes: install restart

fixdeadserver:
	kill `ps x| awk '/empserver|emp_serv|killer/ && ! /awk/{print $$1}'`
	cd $(SERVDIR)/BIN; empserver

restart: stop start

newworld: install
	$(BINDIR)/file 
	$(BINDIR)/land
	$(BINDIR)/sctmap
	$(BINDIR)/ore
	$(BINDIR)/sctmap

info: 
	cd INFO; make update

.bsd: Makefile
	@echo "making editing changes file (ho)"
	@rm -f ho
	@touch ho
	@echo "/^#BEGINMOD/" >> ho
	@echo ".,/^#ENDOFMOD/d" >> ho
	@echo "i" >> ho
	@echo "#BEGINMOD		-do not remove this line" >> ho
	@echo "BSD= ${BSD}" | tee -a ho 
	@echo "CC= ${CC}" | tee -a ho 
	@echo "LD= ${LD}" | tee -a ho 
	@echo "SERVDIR =${SERVDIR}" | tee -a ho
	@echo "INDENT =${INDENT}" | tee -a ho
	@echo "MANDIR =${MANDIR}" | tee -a ho
	@echo "MANEXT =${MANEXT}" | tee -a ho
	@echo "SRCDIR =${SRCDIR}" | tee -a ho
	@echo "BINDIR =${BINDIR}" | tee -a ho
	@echo "GLOBALCFLAGS =${GLOBALCFLAGS}" | tee -a ho
	@echo "GAMEDIR =${GAMEDIR}" | tee -a ho
	@echo "OOB =${OOB}" | tee -a ho
	@echo "#ENDOFMOD		-do not remove this line" >> ho
	@echo "." >> ho
	@echo "wq" >> ho
	@for i in ${ALLDIRS}; do \
	    (echo changing $$i/Makefile ; ex - $$i/Makefile < ho > /dev/null);\
	done
	rm -f ho
	touch .bsd

glib.a: /dev/null
	@echo make glib.a in lib
	@cd lib; make ../glib.a $(INDENT)

emm:;
	@echo "cd empmain ; make"
	@cd empmain ; make $(INDENT)

dirs:	emm 
	@for i in ${DIRS}; do \
		(echo making in $$i; cd $$i; make $(INDENT); cd ..); \
	done

.pwd:;
	@echo "making editing changes file (ho)"
	@rm -f ho
	@touch ho
	@echo "/^SRCDIR=/" >> ho
	@echo ".d" >> ho
	@echo "i" >> ho
	@echo -n "SRCDIR= " >>ho
	@pwd  >>ho
	@echo "." >> ho
	@echo "wq" >> ho
	@echo changing Makefile 
	@ex - Makefile < ho > /dev/null;
	rm -f ho
	@touch .pwd

.mkdir:;
	-mkdir $(SERVDIR) $(SERVDIR)/DATA $(SERVDIR)/BIN\
		$(SERVDIR)/INFO.nr $(BINDIR) $(SERVDIR)/BAK
	-ln -s $(SERVDIR)/BIN $(BINDIR)
	touch .mkdir

$(SERVDIR)/BIN/emp_serv: empmain/emp1
	-rm -f $(SERVDIR)/BIN/emp_serv
	-$(INSCP) empmain/emp1 $(SERVDIR)/BIN/emp_serv

$(GAMEDIR)/empire: client/empire
	-rm -f $(GAMEDIR)/empire 
	-$(INSCP) client/empire $(GAMEDIR)/empire

$(SERVDIR)/empire_tmserver: tm/tm_server
	-rm -f $(SERVDIR)/empire_tmserver 
	-$(INSCP) tm/tm_server $(SERVDIR)/empire_tmserver

$(SERVDIR)/empserver: empserver/empserver 
	-rm -f $(SERVDIR)/empserver 
	-$(INSCP) empserver/empserver $(SERVDIR)/empserver

$(SERVDIR)/killd: empserver/killd
	-rm -f $(SERVDIR)/killd
	-$(INSCP) empserver/killd $(SERVDIR)/killd

install: $(BINDIR)/minusm all $(BINARYS)
	@echo "cd UTIL; make install"
	@cd UTIL; make install $(INDENT)
	@echo "cd INFO; make install"
	@cd INFO; make install $(INDENT)
	@echo "cd client; make install"
	@cd client; make install $(INDENT)

update:
	@echo forcing an update...
	-kill -ALRM `ps x|awk '/empire_tm/ && ! /awk/{print $$1}'`

stop:
	@echo killing off all the processes...
	-kill `ps x|awk \
	'/empserver|emp_serv|killer|empire_tms/&&! /awk/{print $$1}'`

start: 
	rm -f $(SERVDIR)/BAK/empire_tmserver $(SERVDIR)/BAK/empserver 
	rm -f $(SERVDIR)/BAK/killd
	-ln $(SERVDIR)/BIN/empire_tmserver $(SERVDIR)/BAK/empire_tmserver
	-ln $(SERVDIR)/BIN/empserver $(SERVDIR)/BAK/empserver
	-ln $(SERVDIR)/BIN/killd $(SERVDIR)/BAK/killd
	rm -f $(SERVDIR)/BIN/empire_tmserver $(SERVDIR)/BIN/empserver 
	rm -f $(SERVDIR)/BIN/killd
	ln $(SERVDIR)/empire_tmserver $(SERVDIR)/BIN/empire_tmserver
	ln $(SERVDIR)/empserver $(SERVDIR)/BIN/empserver
	ln $(SERVDIR)/killd $(SERVDIR)/BIN/killd
	-cp $(SERVDIR)/*log $(SERVDIR)/BAK
	$(BINDIR)/sctmap
	cd $(SERVDIR)/BIN ; $(NICE) empserver &
	cd $(SERVDIR)/BIN ; $(NICE) empire_tmserver &

client/sailio.o: client/sailio.c
	@echo "cd client; make sailio.o"
	@cd client; make sailio.o $(INDENT)

empmain/empmod.o: empmain/empmod.c
	@echo "cd empmain; make empmod.o"
	@cd empmain; make empmod.o $(INDENT)

empmain/empglb.o: empmain/empglb.c
	@echo "cd empmain; make empglb.o"
	@cd empmain; make empglb.o $(INDENT)

$(BINDIR)/minusm: minusm.c
	$(CC) -o $(BINDIR)/minusm minusm.c

tags:
	@echo "rm -f .tags; make .tags"
	@rm -f .tags; make .tags $(INDENT)

.tags:
	@csh -c "ctags	${PWD}/{h/*.h,empvers.c,minusm.c}"
	@csh -c "ctags -a ${PWD}/{lib,empmain,empserver}/*.[ch]"
	@csh -c "ctags -a ${PWD}/empmain/{COMS,SUBS,gen}/*.[ch]"
	@csh -c "ctags -a ${PWD}/tm/{[a-o],p[b-z],[q-z]}*.c"
	cd client ; ctags *.[ch]
	sort tags >.tags
	mv -f .tags tags
	@for i in empmain empmain/COMS empmain/SUBS tm empserver lib ; do \
		(rm -f $$i/tags;ln -s ${PWD}/tags $$i;echo tags linked to $$i);\
	done
	touch .tags

clean:;
	@echo "clearing cruft out of Makefile"
	@rm -f HO
	@touch HO
	@echo "/^#DEPEND START/" >> HO
	@echo '.,/#DEPEND END/d' >> HO
	@echo "i" >> HO
	@echo '#DEPEND START' >>HO
	@echo '#DEPEND END' >>HO
	@echo "." >> HO
	@echo "/^SRCDIR=/" >> HO
	@echo ".d" >> HO
	@echo "i" >> HO
	@echo "SRCDIR= " >>HO
	@echo "." >> HO
	@echo "wq" >> HO
	@ex - Makefile < HO > /dev/null
	rm -f .Makefile.bak HO ho
	@echo "making editing changes file (ho) for all other Makefiles"
	@touch ho
	@echo "/^#BEGINMOD/" >> ho
	@echo ".,/^#ENDOFMOD/d" >> ho
	@echo "i" >> ho
	@echo "#BEGINMOD 		-do not remove this line" >> ho
	@echo "#  configuration data from the main makefile" >>ho
	@echo "#  will go here" >>ho
	@echo "#ENDOFMOD		-do not remove this line" >> ho
	@echo "." >> ho
	@echo "wq" >> ho
	@for i in ${ALLDIRS}; do \
	    (echo cleaning $$i/Makefile ; ex - $$i/Makefile < ho > /dev/null);\
	done
	rm -f ho
	rm -f $(BINDIR)/minusm empsub.a *.o .bsd .mkdir .pwd \
		.tags tags tm.a glib.a .depend
	@for i in ${DIRS} empmain lib INFO; do \
		(echo make clean in $$i; cd $$i; make clean $(INDENT); cd ..); \
	done

depend:
	@echo "rm -f .depend; make .depend"
	@rm -f .depend; make .depend $(INDENT)

.depend: $(BINDIR)/minusm
	@rm -f .Makefile.bak
	cp Makefile .Makefile.bak
	@echo "making editing changes file"
	@rm -f HO
	@touch HO
	@echo "/^#DEPEND START/" >> HO
	@echo '.,/#DEPEND END/d' >> HO
	@echo "i" >> HO
	@echo '#DEPEND START' >>HO
	$(BINDIR)/minusm "${CFLAGS}" ${CFILES} >>HO
	@echo '#DEPEND END' >>HO
	@echo "." >> HO
	@echo "wq" >> HO
	ex - Makefile < HO > /dev/null
	@rm -f HO
	@for i in ${ALLDIRS}; do \
		(echo makeing depend in $$i; cd $$i; make depend $(INDENT) ; cd ..); \
	done
	touch .depend

#DEPEND START
#DEPEND END

