#	NetHack Makefile.
#	SCCS Id: @(#)Makefile.top	3.2	95/01/05

# newer makes predefine $(MAKE) to 'make' and do smarter processing of
# recursive make calls if $(MAKE) is used
# these makes allow $(MAKE) to be overridden by the environment if someone
# wants to (or has to) use something other than the standard make, so we do
# not want to unconditionally set $(MAKE) here
#
# unfortunately, some older makes do not predefine $(MAKE); if you have one of
# these, uncomment the following line
# (you will know that you have one if you get complaints about unable to
# execute things like 'data' and 'rumors')
# MAKE = make

# make NetHack
GAME     = nethack
GAMEUID  = games
GAMEGRP  = bin
GAMEK    = knethack

# Permissions - some places use setgid instead of setuid, for instance
# See also the option "SECURE" in include/config.h
GAMEPERM = 04755
FILEPERM = 0644
EXEPERM  = 0755
DIRPERM  = 0755

# GAMEDIR also appears in config.h as "HACKDIR".
#
# note that 'make install' believes in creating a nice tidy GAMEDIR for
# installation, free of debris from previous NetHack versions --
# therefore there should not be anything in GAMEDIR that you want to keep
# (if there is, you'll have to do the installation by hand or modify the
# instructions)
GAMEDIR  = /usr/games/lib/$(GAME)dir
SHELLDIR = /usr/games
KDEBINDIR  = $(KDEDIR)/bin
KDEDATDIR  = $(KDEDIR)/share/apps/$(GAMEK)
KDEICODIR  = $(KDEDIR)/share/icons

# per discussion in Install.X11 and Install.Qt
VARDATND = tiles.xpm rip.xpm

# VARDATND = x11tiles pet_mark.xbm
# VARDATND = x11tiles pet_mark.xbm rip.xpm

VARDATD = data oracles options quest.dat rumors
VARDAT = $(VARDATD) $(VARDATND)

# Some versions of make use the SHELL environment variable as the shell
# for running commands.  We need this to be a Bourne shell.
# SHELL = /bin/sh

# Commands for setting the owner and group on files during installation.
# Some systems fail with one or the other when installing over NFS or for
# other permission-related reasons.  If that happens, you may want to set the
# command to "true", which is a no-op. Note that disabling chown or chgrp
# will only work if setuid (or setgid) behavior is not desired or required.
CHOWN = chown
CHGRP = chgrp

#
# end of configuration
#

DATHELP = help hh cmdhelp history opthelp wizhelp

SPEC_LEVS = asmodeus.lev baalz.lev bigrm-?.lev castle.lev fakewiz?.lev \
	juiblex.lev knox.lev medusa-?.lev minend-?.lev minefill.lev \
	minetn-?.lev oracle.lev orcus.lev sanctum.lev tower?.lev valley.lev \
	wizard?.lev astral.lev air.lev earth.lev fire.lev water.lev
QUEST_LEVS = ?-goal.lev ?-fill?.lev ?-locate.lev ?-start.lev

DATNODLB = $(VARDATND) license
DATDLB = $(DATHELP) dungeon $(SPEC_LEVS) $(QUEST_LEVS) $(VARDATD)
DAT = $(DATNODLB) $(DATDLB)

$(GAME):
	( cd src ; $(MAKE) )

all:	$(GAME) Guidebook $(VARDAT) dungeon spec_levs check-dlb
	@echo "Done."

Guidebook:
	( cd doc ; $(MAKE) Guidebook )

manpages:
	( cd doc ; $(MAKE) manpages )

data:
	( cd dat ; $(MAKE) data )

rumors:
	( cd dat ; $(MAKE) rumors )

oracles:
	( cd dat ; $(MAKE) oracles )

#	Note: options should have already been made with make, but...
options:
	( cd dat ; $(MAKE) options )

quest.dat:
	( cd dat ; $(MAKE) quest.dat )

spec_levs:
	( cd util ; $(MAKE) lev_comp )
	( cd dat ; $(MAKE) spec_levs )
	( cd dat ; $(MAKE) quest_levs )

dungeon:
	( cd util ; $(MAKE) dgn_comp )
	( cd dat ; $(MAKE) dungeon )

x11tiles:
	( cd util ; $(MAKE) tile2x11 )
	( cd dat ; $(MAKE) x11tiles )

util/txt2ppm:
	( cd util ; $(MAKE) txt2ppm )

tiles.xpm:
	( cd dat ; $(MAKE) tiles.xpm )

%.xpm:
	( cd dat ; $(MAKE) $*.xpm )

%.pbm:
	( cd dat ; $(MAKE) $*.pbm )

pet_mark.xbm:
	( cd dat ; $(MAKE) pet_mark.xbm )

rip.xpm:
	( cd dat ; $(MAKE) rip.xpm )

check-dlb:
	( cd dat; $(MAKE) options )
	@if egrep -s librarian dat/options ; then $(MAKE) dlb ; else true ; fi

dlb:
	( cd util ; $(MAKE) dlb )
	( cd dat ; ../util/dlb cf nhdat $(DATDLB) )


dofiles:
	target=`sed -n					\
		-e '/librarian/{' 			\
		-e	's/.*/dlb/p' 			\
		-e	'q' 				\
		-e '}' 					\
	  	-e '$$s/.*/nodlb/p' < dat/options` ;	\
	$(MAKE) dofiles-$${target-nodlb}
	cp src/$(GAME) $(GAMEDIR)
	-rm -f $(SHELLDIR)/$(GAME)
	sed -e 's;/usr/games/lib/nethackdir;$(GAMEDIR);' \
		-e 's;HACKDIR/nethack;HACKDIR/$(GAME);' \
		< sys/unix/nethack.sh \
		> $(SHELLDIR)/$(GAME)
# set up their permissions
	-( cd $(GAMEDIR) ; $(CHOWN) $(GAMEUID) $(GAME) ; \
			$(CHGRP) $(GAMEGRP) $(GAME) )
	chmod $(GAMEPERM) $(GAMEDIR)/$(GAME)
	-$(CHOWN) $(GAMEUID) $(SHELLDIR)/$(GAME)
	$(CHGRP) $(GAMEGRP) $(SHELLDIR)/$(GAME)
	chmod $(EXEPERM) $(SHELLDIR)/$(GAME)

dofiles-dlb: check-dlb
	( cd dat ; cp nhdat $(DATNODLB) $(GAMEDIR) )
# set up their permissions
	-( cd $(GAMEDIR) ; $(CHOWN) $(GAMEUID) nhdat $(DATNODLB) ; \
			$(CHGRP) $(GAMEGRP) nhdat $(DATNODLB) ; \
			chmod $(FILEPERM) nhdat $(DATNODLB) )

dofiles-nodlb:
# copy over the game files
	( cd dat ; cp $(DAT) $(GAMEDIR) )
# set up their permissions
	-( cd $(GAMEDIR) ; $(CHOWN) $(GAMEUID) $(DAT) ; \
			$(CHGRP) $(GAMEGRP) $(DAT) ; \
			chmod $(FILEPERM) $(DAT) )


kdedofiles:
	target=`sed -n					\
		-e '/librarian/{' 			\
		-e	's/.*/dlb/p' 			\
		-e	'q' 				\
		-e '}' 					\
	  	-e '$$s/.*/nodlb/p' < dat/options` ;	\
	$(MAKE) kdedofiles-$${target-nodlb}
	cp src/$(GAMEK) $(KDEBINDIR)
# set up their permissions
	-( cd $(KDEBINDIR) ; $(CHOWN) $(GAMEUID) $(GAMEK) ; \
			$(CHGRP) $(GAMEGRP) $(GAMEK) )
	chmod $(GAMEPERM) $(KDEBINDIR)/$(GAMEK)

kdedofiles-dlb: check-dlb
	( cd dat ; cp nhdat $(DATNODLB) $(KDEDATDIR) )
# set up their permissions
	-( cd $(KDEDATDIR) ; $(CHOWN) $(GAMEUID) nhdat $(DATNODLB) ; \
			$(CHGRP) $(GAMEGRP) nhdat $(DATNODLB) ; \
			chmod $(FILEPERM) nhdat $(DATNODLB) )

kdedofiles-nodlb:
# copy over the game files
	( cd dat ; cp $(DAT) $(KDEDATDIR) )
# set up their permissions
	-( cd $(KDEDATDIR) ; $(CHOWN) $(GAMEUID) $(DAT) ; \
			$(CHGRP) $(GAMEGRP) $(DAT) ; \
			chmod $(FILEPERM) $(DAT) )

update: $(GAME) $(VARDAT) dungeon spec_levs
#	(don't yank the old version out from under people who're playing it)
	-mv $(GAMEDIR)/$(GAME) $(GAMEDIR)/$(GAME).old
#	quest.dat is also kept open and has the same problems over NFS
#	(quest.dat may be inside nhdat if dlb is in use)
	-mv $(GAMEDIR)/quest.dat $(GAMEDIR)/quest.dat.old
	-mv $(GAMEDIR)/nhdat $(GAMEDIR)/nhdat.old
# set up new versions of the game files
	( $(MAKE) dofiles )
# touch time-sensitive files
	-touch -c $(GAMEDIR)/bones* $(GAMEDIR)/?lock* $(GAMEDIR)/wizard*
	-touch -c $(GAMEDIR)/save/*
	touch $(GAMEDIR)/perm $(GAMEDIR)/record
# and a reminder
	@echo You may also want to install the man pages via the doc Makefile.

# if KDE
#install: kdeinstall
# else
install: qtinstall
# endif KDE

qtinstall: $(GAME) $(VARDAT) dungeon spec_levs
# set up the directories
	-mkdir $(SHELLDIR)
	-rm -rf $(GAMEDIR)
	-mkdir $(GAMEDIR) $(GAMEDIR)/save
	-$(CHOWN) $(GAMEUID) $(GAMEDIR) $(GAMEDIR)/save
	$(CHGRP) $(GAMEGRP) $(GAMEDIR) $(GAMEDIR)/save
	chmod $(DIRPERM) $(GAMEDIR) $(GAMEDIR)/save
# set up the game files
	( $(MAKE) dofiles )
# set up some additional files
	touch $(GAMEDIR)/perm $(GAMEDIR)/record $(GAMEDIR)/logfile
	-( cd $(GAMEDIR) ; $(CHOWN) $(GAMEUID) perm record logfile ; \
			$(CHGRP) $(GAMEGRP) perm record logfile ; \
			chmod $(FILEPERM) perm record logfile )
# and a reminder
	@echo You may also want to reinstall the man pages via the doc Makefile.

kdeinstall: $(GAME) $(VARDAT) dungeon spec_levs
# set up the directories
	-rm -rf $(KDEDATDIR)
	-mkdir $(KDEDATDIR) $(KDEDATDIR)/save
	-$(CHOWN) $(GAMEUID) $(KDEDATDIR) $(KDEDATDIR)/save
	$(CHGRP) $(GAMEGRP) $(KDEDATDIR) $(KDEDATDIR)/save
	chmod $(DIRPERM) $(KDEDATDIR) $(KDEDATDIR)/save
# set up the game files
	( $(MAKE) kdedofiles )
# set up some additional files
	touch $(KDEDATDIR)/perm $(KDEDATDIR)/record $(KDEDATDIR)/logfile
	-( cd $(KDEDATDIR) ; $(CHOWN) $(GAMEUID) perm record logfile ; \
			$(CHGRP) $(GAMEGRP) perm record logfile ; \
			chmod $(FILEPERM) perm record logfile )
	cp win/Qt/knethack.xpm $(KDEICODIR)
	cp win/Qt/knethack-mini.xpm $(KDEICODIR)/mini/knethack.xpm


# 'make clean' removes all the .o files, but leaves around all the executables
# and compiled data files
clean:
	( cd src ; $(MAKE) clean )
	( cd util ; $(MAKE) clean )

# 'make spotless' returns the source tree to near-distribution condition.
# it removes .o files, executables, and compiled data files
spotless:
	( cd src ; $(MAKE) spotless )
	( cd util ; $(MAKE) spotless )
	( cd dat ; $(MAKE) spotless )
	( cd doc ; $(MAKE) spotless )
