#

# Define the root for the RenderWare build.

BASEDIR         = ..

# This target must be defined before makeopt is included. It contains a single
# forward reference to the main target

all: rwplugin
 
# Include the shared RenderWare make definitions

include $(BASEDIR)/makeopt

-include ../makedir/pipe/$(PIPETYPE)

# Find the pipeline files
VPATH 	      += pipe/$(PIPETYPE)
#VPATH 	      += pipe/$(PIPETYPE)/$(RWTARGET)
C_INC 	      += -Ipipe/$(PIPETYPE)
#C_INC         += -Ipipe/$(PIPETYPE)/$(RWTARGET)

ifneq ("$(PIPETYPE)", "generic")
VPATH += pipe/$(PIPETYPE)/$(RWTARGET)
C_INC += -Ipipe/$(PIPETYPE)/$(RWTARGET)
endif

# derive the library, header and error filename from the plugin name
RPLIB 	        = $(SDKLIBDIR)/$(LIBPREFIX)$(PLUGIN).$(LIBEXT)
RPHEAD 	        = $(SDKINCDIR)/$(PLUGIN).h
RPERR 	        = $(SDKINCDIR)/$(PLUGIN).rpe
RPDOCDIR        = $(SDKDOCDIR)/plugin
RPHLPDIR        = $(SDKHLPDIR)/plugin
RPHTMDIR        = $(SDKHTMDIR)/plugin

# and what object files are required. Note, if the RenderWare error & 
# debug mechnism is to be used then rpdbgerr.$(O) must be included
# because of the needs for some compilers for the module names to be unique
# we generate a unique name for this file based on the name of the plugin

OBJS            = $(WORLDOBJ) $(WORLDPIPEOBJ) $(WORLDPIPEPSOBJ)
ifeq ($(RWDEBUG), 1)
OBJS           += $(OBJDIR)/u$(PLUGIN).$(O)
endif
CSRC            = $(addsuffix .c, $(basename $(subst $(OBJDIR), ., $(WORLDOBJ))))
CSRC           += $(addsuffix .c, $(basename $(subst $(OBJDIR), pipe/$(PIPETYPE), $(WORLDPIPEOBJ))))
CSRC           += $(addsuffix .c, $(basename $(subst $(OBJDIR), pipe/$(PIPETYPE)/$(RWTARGET), $(WORLDPIPEPSOBJ))))
HSRC            = $(subst .c,.h,$(CSRC))
DOCSRC          = $(RPHEAD) $(CSRC)

# The main entry point - checkopt is defined in makeopt and 
# checks that valid values for target, compiler and os have been defined

rwplugin: header checkopt dirsetup $(RPLIB)

WORLDHFILES         = $(SDKINCDIR)/baaplylt.c

$(SDKINCDIR)/baaplylt.c:
	$(CP) $(BASEDIR)/world/pipe/generic/baaplylt.c $@

header: $(WORLDHFILES) $(RPHEAD) $(RPERR) 

# The plugin library defined above
$(RPLIB): $(OBJS)
	$(RM) $@
	$(AR) $(ARCREATEFLAGS)
	$(BINDIR)/patchlib $@

.PHONY : always
always: # dummy target - always out of date

# Create and install the headers in the right place
$(RPHEAD): $(RPERR) $(HSRC)
	$(BUILDTOOLDIR)/incgen/incgen		\
		$(HSRC)				\
		-I.				\
		$(C_INC)			\
		-sstdlib			\
		-sstdarg			\
		-prpworld			\
		-o$(RPHEAD)			\
		-lrpworld.lst			\
		-grwcore.h

# Generate the documentation

$(RPDOCDIR) $(RPHLPDIR) $(RPHTMDIR) $(RPHLPDIR)/$(PLUGIN):
	$(MD) $@

doxygen:
	echo $(DOCSRC) |                                \
	$(BINDIR)/doxycfg				\
	  -b$(BASEDIR)					\
	  -d$(RPHLPDIR)/$(PLUGIN)/			\
	  -o$(RWOS)					\
	  -s$(BASEDIR)/misc				\
	  -t$(RWTARGET)					\
	> $(RPHLPDIR)/$(PLUGIN)/Doxyfile

doc: 
	echo $(addprefix $(CURDIR)/, $(DOCSRC)) \
	>> $(SDKHLPDIR)/$(RWTARGET)/Docfile

defgroup:
	-$(BINDIR)/egrep --line-number --with-filename \
	"\\\defgroup" $(addprefix $(CURDIR)/, $(DOCSRC))

plugindoc:
	doxygen $(RPHLPDIR)/$(PLUGIN)/Doxyfile
	-$(CP) $(BASEDIR)/misc/rwlogo.jpg $(RPHLPDIR)/$(PLUGIN)/html/rwlogo.jpg
	$(BINDIR)/UNIX2DOS $(RPHLPDIR)/$(PLUGIN)/html/index.hhc
	-$(BINDIR)/hhc $(RPHLPDIR)/$(PLUGIN)/html/index.hhp

doxy: $(RPHLPDIR)/$(PLUGIN) $(RPHEAD) doxygen plugindoc

# Create the directory for the object files
dirsetup: $(OBJDIR)

$(OBJDIR):
	$(MD) $@

verify:
	rwcheck $(RWCHECKFLAGS) $(CSRC)

longline:
	-egrep --line-number --with-filename "................................................................................" $(CSRC)


tabs:
	-egrep --line-number --with-filename "	" $(CSRC)


# delete all intermediate files
clean: checkopt
	$(RM) -r $(OBJDIR)
	$(RM) *.i
	$(RM) *.err
	$(RM) *.cmd
	$(RM) *.pdb

# delete all files in preparation for a release
distclean: clean
	$(RM) $(RPLIB)
	$(RM) $(RPHEAD)
	$(RM) $(RPERR)
	$(RM) -r obj
	$(RM) -r $(PLUGIN).lib
	$(RM) -r $(PLUGIN).rtf

# dependencies

$(PLUGIN).c: $(RPHEAD)

$(RPERR): rperror.def 

HDEPEND = $(patsubst %.h,$(OBJDIR)/%.hd, $(notdir $(HSRC)))


ifeq ($(RWDEPEND), 1)
ifdef WITH_DEPEND
-include $(WORLDOBJ:.$(O)=.d)
-include $(HDEPEND)
endif
endif