###############################################################################
#
# Makefile for rogue
#
# Rogue: Exploring the Dungeons of Doom
# Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
# All rights reserved.
#       
# See the file LICENSE.TXT for full copyright and licensing information.
#
###############################################################################
        
###############################################################################
# Site configuration occurs beneath this comment
# Typically ./configure (autoconf tools) configures this section
# This section could be manually configured if autoconf/configure fails
###############################################################################

DISTNAME=@PACKAGE_TARNAME@@PACKAGE_VERSION@
PACKAGE_TARNAME = @PACKAGE_TARNAME@-@PACKAGE_VERSION@
PROGRAM=@PROGRAM@

O=o

#CC=gcc
CC    = @CC@

#CFLAGS=-O2
CFLAGS=

#LIBS=-lcurses
LIBS =	@LIBS@

#RM=rm -f
RM    = rm -f

#GROFF=groff
GROFF = @GROFF@

#NROFF=nroff
NROFF = @NROFF@

#TBL=tbl
TBL   = @TBL@

#COLCRT=colcrt
COLCRT = @COLCRT@

#SCOREFILE=rogue54.scr
SCOREFILE = @SCOREFILE@

#LOGFILE=rogue54.log
LOGFILE = @LOGFILE@

#LOCKFILE=rogue54.lck
LOCKFILE = @LOCKFILE@

#SAVEDIR=rogue5save
SAVEDIR = @SAVEDIR@

#GROUPOWNER=games
GROUPOWNER = @GROUPOWNER@

#CPPFLAGS=-DHAVE_CONFIG_H
CPPFLAGS =@DEFS@ @CPPFLAGS@

#DISTFILE = $(PROGRAM)
DISTFILE = $(DISTNAME)-@DISTSYS@

INSTALL=@INSTALL@

#INSTGROUP=-g games
INSTGROUP=
#INSTOWNER=-u root
INSTOWNER=

CHGRP=chgrp

MKDIR=mkdir

TOUCH=touch

RMDIR=rmdir

CHMOD=chmod

DESTDIR=

prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@
datadir=@datadir@
bindir=@bindir@
mandir=@mandir@
docdir=@docdir@
man6dir = $(mandir)/man6

###############################################################################
# Site configuration occurs above this comment
# It should not be necessary to change anything below this comment
############################################################################### 

HDRS     = rogue.h extern.h score.h config.h
OBJS1    = vers.$(O) extern.$(O) armor.$(O) chase.$(O) command.$(O) \
           daemon.$(O) daemons.$(O) fight.$(O) init.$(O) io.$(O) list.$(O) \
           mach_dep.$(O) main.$(O) mdport.$(O) misc.$(O) monsters.$(O) \
           move.$(O) new_level.$(O)
OBJS2    = options.$(O) pack.$(O) passages.$(O) potions.$(O) rings.$(O) \
           rip.$(O) rooms.$(O) save.$(O) scrolls.$(O) state.$(O) sticks.$(O) \
           things.$(O) weapons.$(O) wizard.$(O) xcrypt.$(O)
OBJS     = $(OBJS1) $(OBJS2)
CFILES   = vers.c extern.c armor.c chase.c command.c daemon.c \
           daemons.c fight.c init.c io.c list.c mach_dep.c \
           main.c  mdport.c misc.c monsters.c move.c new_level.c \
           options.c pack.c passages.c potions.c rings.c rip.c \
           rooms.c save.c scrolls.c state.c sticks.c things.c \
           weapons.c wizard.c xcrypt.c
MISC_C   = findpw.c scedit.c scmisc.c
DOCSRC   = rogue.me.in rogue.6.in rogue.html.in
DOCS     = $(PROGRAM).doc $(PROGRAM).cat
AFILES   = configure Makefile.in configure.ac config.h.in config.sub config.guess \
           install-sh rogue.6.in rogue.me.in rogue.html.in
MISC     = Makefile.std LICENSE.TXT rogue54.sln rogue54.vcproj rogue.spec \
           rogue.png rogue.desktop

.SUFFIXES: .obj .o .c

.c.obj:
	$(CC) $(CFLAGS) $(CPPFLAGS) /c $*.c
    
.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $*.c

all: $(PROGRAM) docs
    
$(PROGRAM): $(HDRS) $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
    
clean:
	$(RM) $(OBJS1)
	$(RM) $(OBJS2)
	$(RM) core a.exe a.out a.exe.stackdump $(PROGRAM) $(PROGRAM).exe
	$(RM) $(PROGRAM).tar $(PROGRAM).tar.gz $(PROGRAM).zip 
	$(RM) $(DOCS)
	$(RM) -r $(DISTNAME)
    
maintainer-clean:
	$(RM) config.h
	$(RM) Makefile
	$(RM) config.status
	$(RM) -r autom4te.cache
	$(RM) config.log
	$(RM) $(PROGRAM).scr $(PROGRAM).lck

docs: $(DOCS)

stddocs:
	sed -e 's/@PROGRAM@/rogue/' -e 's/@SCOREFILE@/rogue.scr/' rogue.6.in > rogue.6
	sed -e 's/@PROGRAM@/rogue/' -e 's/@SCOREFILE@/rogue.scr/' rogue.me.in > rogue.me
	sed -e 's/@PROGRAM@/rogue/' -e 's/@SCOREFILE@/rogue.scr/' rogue.html.in > rogue,html
	sed -e 's/@PROGRAM@/rogue/' -e 's/@SCOREFILE@/rogue.scr/' rogue.doc.in > rogue.doc
	sed -e 's/@PROGRAM@/rogue/' -e 's/@SCOREFILE@/rogue.scr/' rogue.cat.in > rogue.cat

dist.src:
	$(MAKE) $(MAKEFILE) clean
	mkdir $(DISTNAME)
	cp $(CFILES) $(HDRS) $(MISC) $(AFILES) $(DISTNAME)
	tar cf $(DISTNAME)-src.tar $(DISTNAME)
	gzip -f $(DISTNAME)-src.tar
	rm -fr $(DISTNAME)

findpw: findpw.c xcrypt.o mdport.o xcrypt.o
	$(CC) -s -o findpw findpw.c xcrypt.o mdport.o -lcurses

scedit: scedit.o scmisc.o vers.o mdport.o xcrypt.o
	$(CC) -s -o scedit vers.o scedit.o scmisc.o mdport.o xcrypt.o -lcurses

scmisc.o scedit.o:
	$(CC) -O -c $(SF) $*.c

$(PROGRAM).doc: rogue.me
	if test "x$(GROFF)" != "x" ; then \
	$(GROFF) -P-c -P-b -P-u -t -me -Tutf8 rogue.me > $(PROGRAM).doc ;\
	elif test "x$(NROFF)" != "x" && test "x$(TBL)" != "x" && test "x$(COLCRT)" != "x" ; then \
        tbl rogue.me | $(NROFF) -me | colcrt - > $(PROGRAM).doc ;\
	fi

#$(PROGRAM).html: rogue.me
#	if test "x$(GROFF)" != "x" ; then \
#	$(GROFF) -t -me -Thtml -P-l rogue.me > $(PROGRAM).html ;\
#	fi

$(PROGRAM).cat: rogue.6
	if test "x$(GROFF)" != "x" ; then \
	$(GROFF) -P-c -P-b -P-u -Tascii -man rogue.6 > $(PROGRAM).cat ;\
	elif test "x$(NROFF)" != "x" && test "x$(COLCRT)" != "x" ; then \
	$(NROFF) -man rogue.6 | $(COLCRT) - > $(PROGRAM).cat ;\
	fi

dist: clean $(PROGRAM)
	tar cf $(DISTFILE).tar $(PROGRAM) LICENSE.TXT $(DOCS)
	gzip -f $(DISTFILE).tar

install: all
	$(TOUCH) test
	if test ! -f $(DESTDIR)$(SCOREFILE) ; then \
	    $(MKDIR) -p `dirname $(DESTDIR)$(SCOREFILE)` ; \
	    $(INSTALL) -m 0664 test $(DESTDIR)$(SCOREFILE) ; fi
	if test "x$(LOGFILE)" != "x" && test ! -f $(DESTDIR)$(LOGFILE) ; then \
	    $(MKDIR) -p `dirname $(DESTDIR)$(LOGFILE)` ; \
	    $(INSTALL) -m 0664 test $(DESTDIR)$(LOGFILE) ; fi
	if test "x$(SAVEDIR)" != "x" && test ! -d $(DESTDIR)$(SAVEDIR) ; then \
	    $(MKDIR) -p $(DESTDIR)$(SAVEDIR) ; fi
	$(MKDIR) -p $(DESTDIR)$(bindir)
	$(INSTALL) -m 0755 $(PROGRAM) $(DESTDIR)$(bindir)/$(PROGRAM)
	-if test "x$(GROUPOWNER)" != "x" ; then \
	    $(CHGRP) $(GROUPOWNER) $(DESTDIR)$(SCOREFILE) ; \
	    $(CHGRP) $(GROUPOWNER) $(DESTDIR)$(LOGFILE) ; \
	    $(CHGRP) $(GROUPOWNER) $(DESTDIR)$(bindir)/$(PROGRAM) ; \
	    $(CHGRP) $(GROUPOWNER) $(DESTDIR)$(SAVEDIR) ; \
	    $(CHMOD) 02755 $(DESTDIR)$(bindir)/$(PROGRAM) ; \
	    $(CHMOD) 0464 $(DESTDIR)$(SCOREFILE) ; \
	    $(CHMOD) 0464 $(DESTDIR)$(LOGFILE) ; \
	    $(CHMOD) 0775 $(DESTDIR)$(SAVEDIR) ; \
        fi
	if test ! -d $(DESTDIR)$(man6dir) && test -d $(DESTDIR)$(mandir) ; \
	    then $(INSTALL) -m 0644 rogue.6 $(DESTDIR)$(mandir)/$(PROGRAM).6 ; \
	    else mkdir -p $(DESTDIR)$(man6dir) ; \
	    $(INSTALL) -m 0644 rogue.6 $(DESTDIR)$(man6dir)/$(PROGRAM).6 ; fi
	$(MKDIR) -p $(DESTDIR)$(docdir)
	$(INSTALL) -m 0644 $(DOCS) $(DESTDIR)$(docdir)
	$(INSTALL) -m 0644 rogue.html $(DESTDIR)$(docdir)/$(PROGRAM).html
	$(INSTALL) -m 0644 LICENSE.TXT $(DESTDIR)$(docdir)/LICENSE.TXT
	$(INSTALL) -m 0644 rogue.me $(DESTDIR)$(docdir)/$(PROGRAM).me
	if test "x$(LOCKFILE)" != "x" && test ! -f $(DESTDIR)$(LOCKFILE) ; then \
            $(INSTALL) -m 0666 test $(DESTDIR)$(LOCKFILE)  ; \
            $(RM) $(DESTDIR)$(LOCKFILE) ; \
        fi
	$(RM) test

uninstall:	
	$(RM) $(DESTDIR)$(bindir)/$(PROGRAM)
	$(RM) $(DESTDIR)$(man6dir)/$(PROGRAM).6
	$(RM) $(DESTDIR)$(mandir)/$(PROGRAM).6
	$(RM) -r $(DESTDIR)$(docdir)
	if test "x$(LOCKFILE)" != "x" ; then \
	    $(RM) $(DESTDIR)$(LOCKFILE) ; fi
    
reinstall: uninstall install
