#This is the AFKMud Makefile.
#This should be usable on pretty much any Linux system.
#Refer to your 'man gcc' for explanations of these options
#To compile in FreeBSD, use the gmake compiler.

#Uncomment if not compiling on Cygwin or FreeBSD
LINUX = 1

#Uncomment to compile in Cygwin
#WINDOZE = -DWINDOZE

#Uncomment to compile in FreeBSD
#FBSD = -DFREEBSD

# Uncomment the two lines below if compiling on a Solaris box
#SOLARIS_FLAG = -Dsun -DSYSV -Wno-char-subscripts
#SOLARIS_LINK = -lnsl -lsocket -lresolv

#Used for Alsherok stuff, uncommenting it should have no affect, though why would you want to?
#ALSHEROK = 1

#Change this to reflect whichever compiler you want to use.
CC      = gcc
#PROF    = -p
NOCRYPT = 

#Type of machine to compile for. Athlon works just as well on Duron too.
#The march option needs to match the general family of CPU.
#If you don't know what to set for these options, and your system administrator doesn't either, comment this line out
MACHINE = -mcpu=athlon -march=athlon

#Uncomment the line below if you are getting undefined crypt errors.
NEED_CRYPT = -lcrypt

#String hashing. Only comment this out if you have a very good reason for it.
#If you don't know what this is for, LEAVE IT ALONE!
HASH = 1

#Math functions - uncomment if you get errors or warnings about math functions
MATH_LIB = -lm

#Memory Debugger - comment out to turn the debugger off, uncomment to use it.
#MEMDEBUG = 1

#Intermud-3 - Comment out to disable I3 support in your code
I3 = 1

#IMC2 - Comment out to disable IMC2 support
#IMC = 1

#Internal Web Server - comment out to disable web server code.
WEB = 1

#Multiport support. Comment out to disable this feature.
#MULTIPORT = 1

#Mud Shell support. Comment out to disable this feature.
#MUDSHELL = 1

#Miscellaneous compiler options.
OPT_FLAG = -pipe

W_FLAGS = -Wall -Werror -Wformat-security -Winline -Wstrict-prototypes -Wnested-externs -Wshadow -Wpointer-arith -Wcast-align -Wcast-qual

C_FLAGS = $(MACHINE) -g2 -Os $(W_FLAGS) $(OPT_FLAG) $(PROF) $(NOCRYPT) $(WINDOZE) $(SOLARIS_FLAG) -export-dynamic
ifdef LINUX
L_FLAGS = $(MACHINE) $(OPT_FLAG) $(PROF) $(SOLARIS_LINK) $(NEED_CRYPT) $(MATH_LIB) -lz -ldl
else
L_FLAGS = $(MACHINE) $(OPT_FLAG) $(PROF) $(SOLARIS_LINK) $(NEED_CRYPT) $(MATH_LIB) -lz
endif
#D_FLAGS : For the DNS Resolver process. No need in linking all the extra libs for this.
D_FLAGS = $(MACHINE) -g2 -O $(W_FLAGS) $(PROF) $(SOLARIS_LINK)

C_FILES = act_comm.c act_info.c act_move.c act_obj.c act_wiz.c afk.c \
          archery.c areaconvert.c auction.c ban.c bits.c boards.c \
          build.c calendar.c channels.c clans.c color.c comm.c comments.c \
          const.c db.c deity.c editor.c environment.c event.c event_handler.c fight.c finger.c \
          handler.c hashstr.c help.c hotboot.c iafk.c idale.c imm_host.c \
          interface.c interp.c ismaug.c liquids.c magic.c mccp.c \
          misc.c msp.c mspecial.c mud_comm.c mud_prog.c mxp.c \
          new_auth.c olcmob.c olcobj.c olcroom.c overland.c pets.c \
	    pfiles.c player.c polymorph.c rent.c renumber.c reset.c \
	    save.c ships.c shops.c skills.c skyship.c slay.c starmap.c \
          tables.c track.c treasure.c update.c

ifdef HASH
   C_FLAGS := $(C_FLAGS) -DHASHSTR
endif

ifdef WEB
   C_FILES := websvr.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DWEBSVR
endif

ifdef IMC
   C_FILES := imc.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DIMC
endif

ifdef I3
   C_FILES := i3.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DI3
endif

ifdef MUDSHELL
   C_FILES := mudshell.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DMUDSHELL
endif

ifdef MULTIPORT
   C_FILES := shell.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DMULTIPORT
endif

ifdef MEMDEBUG
   C_FILES := fortify.c $(C_FILES)
   C_FLAGS := $(C_FLAGS) -DFORTIFY
endif

O_FILES := $(patsubst %.c,o/%.o,$(C_FILES))

H_FILES = $(wildcard *.h) 

all:
	$(MAKE) -s afkmud
	$(MAKE) -s resolver

ifdef WINDOZE
afkmud: $(O_FILES)
	rm -f afkmud.exe
	dlltool --export-all --output-def afkmud.def $(O_FILES)
	dlltool --dllname afkmud.exe --output-exp afkmud.exp --def afkmud.def
	$(CC) -export-dynamic -o afkmud.exe $(O_FILES) afkmud.exp $(L_FLAGS)
	echo "Done compiling mud.";
	chmod g+w afkmud.exe
	chmod a+x afkmud.exe
	chmod g+w $(O_FILES)

resolver: resolver.o
	rm -f resolver.exe
	$(CC) $(D_FLAGS) -o resolver resolver.o
	echo "Done compiling resolver.";
	chmod g+w resolver.exe 
	chmod a+x resolver.exe
	chmod g+w resolver.o

clean:  
	rm -f o/*.o afkmud.exe afkmud.def afkmud.exp core resolver.exe resolver.o
	$(MAKE) all
        
purge:  
	rm -f o/*.o afkmud.exe afkmud.def afkmud.exp core resolver.exe resolver.o
else
afkmud: $(O_FILES)
	rm -f afkmud
	$(CC) -export-dynamic -o afkmud $(O_FILES) $(L_FLAGS)
	echo "Done compiling mud.";
	chmod g+w afkmud
	chmod a+x afkmud
	chmod g+w $(O_FILES)

resolver: resolver.o
	rm -f resolver 
	$(CC) $(D_FLAGS) -o resolver resolver.o
	echo "Done compiling resolver.";
	chmod g+w resolver 
	chmod a+x resolver 
	chmod g+w resolver.o

clean:  
	rm -f o/*.o afkmud core resolver resolver.o
	$(MAKE) all
        
purge:  
	rm -f o/*.o afkmud core resolver resolver.o
endif

o/%.o: %.c
	echo "  Compiling $@....";
	$(CC) -c $(C_FLAGS) $< -o $@

.c.o: mud.h
	$(CC) -c $(C_FLAGS) $<
