#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
#ident	"$Id: Makefile,v 1.1 2007/07/30 21:35:53 chravel Exp $"
#

###################################################################
# Makefile to create packages and patches for NSS on Linux      #
###################################################################

# where are we in the source tree ?
MOZILLA_DIR = ../../../..
MOZILLA_ABS := $(shell cd $(MOZILLA_DIR);pwd)
CUR_DIR := $(shell pwd)
CORE_DEPTH := $(MOZILLA_DIR)/nss

include $(CORE_DEPTH)/coreconf/config.mk

# Get actual version of NSS
VERSION := $(shell grep NSS_VERSION $(MOZILLA_DIR)/dist/public/nss/nss.h \
		| head -1 \
		| sed -e 's/[^"]*"//' -e 's/".*//' -e 's/ .*//' \
		| cut -d. -f1-3)
Timestamp := $(shell /bin/date +%Y.%m.%d.%H.%M)

# source directories
DIST := $(MOZILLA_DIR)/dist/$(OBJDIR)

HEADER_DIR := public/nss

COPYRIGHT := $(CUR_DIR)/copyright

# List of packages to build
NAME        = sun-nss
ifndef RPM_RELEASE
RPM_RELEASE = 1
endif

# Force i386 for non 64 bit build
ifeq ($(USE_64),1)
	RPMLIBDIR = lib64
	SPEC_KEEP := \#64_ONLY\#
	SPEC_DEL := \#32_ONLY\#
else
	RPMTARGET = "--target=i386"
	RPMLIBDIR = lib
	SPEC_KEEP := \#32_ONLY\#
	SPEC_DEL := \#64_ONLY\#
endif

# destination directory
BUILD_DIR := $(CUR_DIR)/$(OBJDIR)
ROOT := $(BUILD_DIR)/$(NAME)-root
PKG_DIR := $(MOZILLA_ABS)/dist/$(OBJDIR)/pkgarchive

# Layout of the packages
PROTO := \
	$(ROOT)/opt/sun/private/$(RPMLIBDIR) \
	$(ROOT)/opt/sun/private/include/nss \
	$(ROOT)/opt/sun/private/bin

# Copy and layout the bits to package ---------------------------------------
$(ROOT)/opt/sun/private/$(RPMLIBDIR):
	mkdir -p $@
	cp $(DIST)/lib/*.so $@
	cp $(DIST)/lib/*.chk $@
$(ROOT)/opt/sun/private/bin:
	mkdir -p $@
	cp $(DIST)/bin/* $@
$(ROOT)/opt/sun/private/include/nss:
	mkdir -p $@
	cp -rL $(SOURCE_PREFIX)/$(HEADER_DIR)/*.h $@

# ============== below is independent of the product to build ==============
# List of targets ------------------------------
package: $(PROTO) $(PKG_DIR) $(NAME).spec
	mkdir -p $(BUILD_DIR)/RPMS
	mkdir -p $(BUILD_DIR)/SOURCES
	mkdir -p $(BUILD_DIR)/BUILD
	echo "The content of this file is not used." > $(BUILD_DIR)/SOURCES/$(NAME)-$(VERSION)
	# Build the packages
	cd $(BUILD_DIR) && rpmbuild $(RPMTARGET) -bb $(BUILD_DIR)/$(NAME).spec
	# Copy the packages to their final destination
	cp $(BUILD_DIR)/RPMS/*/*.rpm $(PKG_DIR)
	@echo "NSS packages done."
	@echo "-----------------------------------------------"
	
%.spec: %.spec_tmpl
	# modify the .spec file
	sed -e "s|#NAME#|$(NAME)|g" \
		-e "s|#VERSION#|$(VERSION)|g" \
		-e "s|#RELEASE#|$(RPM_RELEASE)|g" \
		-e "s|#BUILD_DIR#|$(BUILD_DIR)|g" \
		-e "s|#RPMLIBDIR#|$(RPMLIBDIR)|g" \
		-e "s|$(SPEC_KEEP)||g" \
		-e "/$(SPEC_DEL)/d" \
		-e "/#LICENSE#/r $(COPYRIGHT)" \
		-e "/#LICENSE#/d" \
	$< > $(BUILD_DIR)/$@

# Cleanup target
clean clobber::
	rm -r $(BUILD_DIR)
	rm -r $(PKG_DIR)

# mkdir targets
$(PKG_DIR) $(BUILD_DIR):
	mkdir -p $@
