# 
# 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:52 chravel Exp $"
#

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

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

# WARNING: we are using NSS config here because NSPR requires running autoconf
CORE_DEPTH := $(MOZILLA_DIR)/jss
include $(CORE_DEPTH)/coreconf/config.mk

# Get actual version of JSS
VERSION := $(shell grep JSS_VERSION $(CORE_DEPTH)/org/mozilla/jss/util/jssver.h | \
			sed -e 's/"$$//' -e 's/.*"//' -e 's/ .*//')
Timestamp := $(shell /bin/date +%Y.%m.%d.%H.%M)

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

COPYRIGHT := $(CUR_DIR)/copyright

# List of packages to build
NAME        = sun-jss
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/share/lib

# Copy and layout the bits to package ---------------------------------------
$(ROOT)/opt/sun/private/$(RPMLIBDIR):
	mkdir -p $@
	cp $(DIST)/lib/libjss4.so $@
$(ROOT)/opt/sun/private/share/lib:
	mkdir -p $@
	cp $(CORE_DEPTH)/../dist/release/no-policy/classes/jss4.jar $@

# ============== 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 "JSS 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 $@
