#
# Build the exportable image of the P2P SDK
#

CLIENTROOT=../..
DEVKIT=./Pm2Pm
BWTREE=$(CLIENTROOT)/bw
P2PROOT=$(BWTREE)
DOCSRC=$(CLIENTROOT)/common/sdks/p2p
DOCDST=$(DEVKIT)/man/en_US

DATE=$(shell date +%y%m%d)
# Extract the version numbers from the header file.
P2P_HEADER=$(P2PROOT)/include/p2p.h
MAJOR_VERSION=$(shell grep '^\#define P2P_CORE_MAJOR' $(P2P_HEADER) | sed 's/^.* //')
MINOR_VERSION=$(shell grep '^\#define P2P_CORE_MINOR' $(P2P_HEADER) | sed 's/^.* //')
PATCH_VERSION=$(shell grep '^\#define P2P_CORE_PATCH' $(P2P_HEADER) | sed 's/^.* //')

ifeq ($(strip $(MAJOR_VERSION)),)
    $(error Failed to extract MAJOR_VERSION from $(P2P_HEADER).)
endif
ifeq ($(strip $(MINOR_VERSION)),)
    $(error Failed to extract MINOR_VERSION from $(P2P_HEADER).)
endif
ifeq ($(strip $(PATCH_VERSION)),)
    $(error Failed to extract PATCH_VERSION from $(P2P_HEADER).)
endif

SDK_PATH = $(P2PROOT)/exports
SDK_IMAGE = $(SDK_PATH)/Pm2Pm-$(MAJOR_VERSION)_$(MINOR_VERSION)_$(PATCH_VERSION)-$(DATE).zip

default headers exports runtests:

install:
	rm -rf $(DEVKIT)
	mkdir -p $(DEVKIT)
	#
	# install exported P2P root files
	cat p2prootfiles | (cd $(P2PROOT); tar cf - -T -) | (cd $(DEVKIT); tar xf -)
	#
	# install demo apps
	cat demofiles | (cd $(BWTREE); tar cf - -T -) | (cd $(DEVKIT); tar xf -)
	#
	# install docs
	mkdir -p $(DOCDST)
	cat docfiles | (cd $(DOCSRC); tar cf - -T -) | (cd $(DOCDST); tar xf -)
	#
	# filter out CVS directories (TODO: It would probably be more efficient to avoid
	# copying them in the first place, by using tar's "--exclude" option). 
	find $(DEVKIT) -type d -name CVS -prune -exec rm -rf "{}" ";"
	#
	# Copy right and Readme
	install -p README NOTICE $(DEVKIT)
	#
	# zip up the resulting tree
	rm -f $(SDK_IMAGE)
	mkdir -p $(SDK_PATH)
	zip -rq $(SDK_IMAGE) $(DEVKIT)

clean:
	rm -rf $(DEVKIT)

clobber: clean
	rm -f $(SDK_IMAGE)
