###############################################################################
# Makefile for P2P network test application
#
# Copyright 2007 BroadOn Communications Corp.  All rights reserved.
#
###############################################################################
# Template makefile for new modules
#
# Copyright (C) 2005-2006 Nintendo.  All rights reserved.
#
# These coded instructions, statements, and computer programs contain
# proprietary information of Nintendo of America Inc. and/or Nintendo
# Company Ltd., and are protected by Federal copyright law.  They may
# not be disclosed to third parties or copied or duplicated in any form,
# in whole or in part, without the prior written consent of Nintendo.
###############################################################################

include ../../../../devroot/include/make/SDKdefs

# ensures that 'make' does 'make all' instead of first target
default:  all

GBA_SOURCE         = ../../../../..

REVOLUTION_EXT_INSTALL_ROOT := $(REVOLUTION_P2P_ROOT)
P2P_INC_ROOT                 = "$(REVOLUTION_EXT_INSTALL_ROOT)\include"

REX_TITLEID = 34
REX_TITLEFW = noFW

TICKET             = $(REVOLUTION_EXT_ROOT)/X86/bin/rvl.no.34.bin
BANNER             = vcmvtest.met
# ntsC - network test client
TITLE_ID           = 6e747343
APP_TYPE           = RVL_APP
VERSION_NUM       ?= 0

WAD_OPTION  = -I "$(VERSION_NUM)" -gc "0x$(TITLE_ID)" -m "$(BANNER)" -t "$(TICKET)"

###############################################################################
# commondefs must be included near the top so that all common variables
# will be defined before their use.
###############################################################################

include $(REVOLUTION_EXT_ROOT)/build/buildtools/commondefs
include $(GBA_SOURCE)/sw/bw/build/buildtools/commondefs

###############################################################################
# The following defines force the commondef defines to be directory independent
###############################################################################

# MODULENAME should be name of current directory; it is the name of the app
# as far as the build is concerned
MODULENAME	= ntsClient
MODULE_ROOT	= .
DEMO	 	= TRUE
DEMO_ROOT	= ..
SAMPLE	 	=
LINCLUDES	= -Iinclude -I$(SRCDIR)
LINCLUDES	+= -nosyspath \
		   -I$(P2P_INC_ROOT)

SRCDIR          = $(GBA_SOURCE)/sw/common/lib/p2p/test/ntsClient

###############################################################################
# Definitions associated with Licensing and Publishing
###############################################################################

# PRODUCTNAME does NOT have to be same as MODULENAME, but often should be as
# a matter of convenience.  It's the name that the Shop Channel will use.
PRODUCTNAME     = ntsClient
HEX_TITLE_ID    = 00010001$(TITLE_ID)

SCRIPT_DIR         = $(GBA_SOURCE)/sw/tools/build/cygwin

PUBLISH            = $(SCRIPT_DIR)/publish.sh
APPROVE            = $(SCRIPT_DIR)/approve-title.sh


# PUBLISH_ROOT is the location of the wad & properties file you wish you use
# It's 'bin/RVL' by default, but you may pass in via command line a different
# directory
PUBLISH_ROOT      ?= $(FULLBIN_ROOT)

include $(GBA_SOURCE)/sw/bw/etc/properties.template

###############################################################################
# Binary building
# BINNAMES lists all binaries that will be linked.  Note that no suffix is
# required, as that will depend on whether this is a DEBUG build or not.
# The final name of the binaries will be $(BINNAME)$(BINSUFFIX)
#
# CSRCS lists all C files that should be built
# ASMSRCS lists all assembly files that should be built.
# The makefile determines which objects are linked into which binaries
# based on the dependencies you fill in at the bottom of this file
###############################################################################

TEST_BIN         = p2p_ntsClient

BINNAMES	+= $(TEST_BIN)
CSRCS           += main.c ping.c
ASMSRCS		=

# we're in 2007, C99 was 8 years ago...
CCFLAGS     += -dialect c99
# the gcc style errors looks much more sane than the default
COMPILER_MSG_STYLE ?= gcc
CCFLAGS     += -msgstyle $(COMPILER_MSG_STYLE)

CCFLAGS_WARNING = -W padding,nounused

###############################################################################
# All modules have "setup" and "build" as targets.  System libraries
# and demo programs also have an "install" target that copies the compiled
# binaries to the binary tree (/revolution/$(ARCH_TARGET)).
###############################################################################

all:    setup build

###############################################################################
# Include LCF file
###############################################################################
ifdef EPPC
LCF_FILE =  $(INC_ROOT)/revolution/eppc.$(ARCH_TARGET).lcf
endif

###############################################################################
# modulerules contains the rules that will use the above variables
# and dependencies below to construct the library and binaries specified.
###############################################################################
include $(REVOLUTION_EXT_ROOT)/build/buildtools/modulerules

REX_DIR		?= $(subst $(space),\$(space),$(subst \,/,$(REVOLUTION_EXT_ROOT)))
REX_INCDIR	:= $(REX_DIR)/include
REX_LIBNAMES     = ncd so nwc24 net
REX_LIBTYPE	 = $(ARCH_TARGET)/lib
REX_LIBDIR	 = $(REX_DIR)/$(REX_LIBTYPE)
REX_LIBFILES     = $(addsuffix $(LIBSUFFIX),$(REX_LIBNAMES)) 
REX_LIBS	 = $(addprefix $(REX_LIBDIR)/,$(REX_LIBFILES))
GINCLUDES	+= -I$(REX_INCDIR)
CCFLAGS 	+= -DUSE_REVOEX

setup: 
	@if [ -z '$(REVOLUTION_P2P_ROOT)' ]; then\
	    echo "You must establish environment variable REVOLUTION_P2P_ROOT before proceeding."; \
	    exit 1; \
	fi
	@if [ ! -w '$(REVOLUTION_P2P_ROOT)' -o ! -d '$(REVOLUTION_P2P_ROOT)' ]; then\
	    mkdir -p '$(REVOLUTION_P2P_ROOT)'; \
	fi
# $(SETUP_ROOTS) does not create directory $(DEP_ROOT), without which build will fail
	@mkdir -p $(DEP_ROOT)
# make the properties file from template
	@$(REMAKE) properties

headers:
	@if [ ! -w $(P2P_INC_ROOT) -o ! -d $(P2P_INC_ROOT) ]; then \
	    	mkdir -p $(P2P_INC_ROOT); \
	fi

publish: 
	@if [ ! -s $(PUBLISH_ROOT)/$(TEST_BIN)$(WADSUFFIX) ]; then\
		echo "$(PUBLISH_ROOT)/$(TEST_BIN)$(WADSUFFIX) not found"; \
		exit 1; \
	fi
	@if [ ! -s $(PUBLISH_ROOT)/$(PROPERTIES_FILE) ]; then\
		echo "$(PUBLISH_ROOT)/$(PROPERTIES_FILE) not found"; \
		exit 1; \
	fi
	$(PUBLISH) $(PUBLISH_ROOT)/$(TEST_BIN)$(WADSUFFIX) $(PUBLISH_ROOT)/$(PROPERTIES_FILE) $(PRODUCTNAME)
	$(APPROVE) $(HEX_TITLE_ID) $(PRODUCTNAME) $(PRODUCTNAME)


###############################################################################
# Dependencies for the binaries listed in BINNAMES should come here
# They are your typical makefile rule, with extra variables to ensure
# that the names and paths match up.
# $(FULLBIN_ROOT) is the location of the local bin directory
# $(BINSUFFIX) depends on whether this is a debug build or not
# $(LOCALLIB_ROOT) points at the local lib directory
# $(REVOLUTION_LIBS) includes all the Revolution libraries.
###############################################################################
REVOLUTION_LIBS += $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p$(LIBSUFFIX) \
		   $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p_services$(LIBSUFFIX) \
		   $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p_servicesmodules$(LIBSUFFIX) \
		   $(REVOLUTION_P2P_ROOT)/RVL/lib/p2p_testframework$(LIBSUFFIX) 


$(FULLBIN_ROOT)/$(TEST_BIN)$(BINSUFFIX): main.o ping.o \
			 	 $(REVOLUTION_LIBS) $(REX_LIBS)
