#! gmake
#
# Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
#
#

OS_VERSION := $(shell uname -s)
ifeq ($(OS_VERSION),Linux)
OS_RELEASE := $(shell uname -r | cut -d. -f1,2)
else
OS_RELEASE := $(shell uname -r)
endif
ifeq (MINGW32_NT,$(findstring MINGW32_NT,$(OS_VERSION)))
OS_VERSION := WINNT
endif

ifeq ($(OS_RELEASE),5.8)
PATCH_NUMBER=115924-5 115926-7
endif
ifeq ($(OS_RELEASE),5.9)
PATCH_NUMBER=114049-0 115926-7
endif
#ifeq ($(OS_RELEASE),5.6)
#PATCH_NUMBER=114047-8
#endif

patches:
ifdef PATCH_DEF
	./build_security_patch -p $(PATCH_DEF)
else
	for patch in $(PATCH_NUMBER); \
	do \
		if [ -f build.$${patch} ]; then \
			chmod 777 build.$${patch}; \
			./build.$${patch}; \
		fi; \
	done
endif

patches_338x:
ifeq ($(OS_VERSION),SunOS)
# SunOS 5.8 patches
	./build_security_patch -p nss338x/115924 -f
# SunOS 5.9 patches
	./build_security_patch -p nss338x/115926 -f
# JSS SunOS 5.8/5.9 point patch
	./build_security_patch -p nss338x/117704
# JSS SunOS 5.10 point patch
	./build_security_patch -p nss338x/121767
else
# JSS3 point patch for Linux
	./build_linux_patch -p nss338x/121647
endif

patches_334x:
# SunOS 5.8 patches
	./build_security_patch -p nss334x/114045
# SunOS 5.9 patches
	./build_security_patch -p nss334x/114049

patches_39x:
# SunOS 5.8 patches
	./build_security_patch -p nss39x/117722 -f
# SunOS 5.9 patches
	./build_security_patch -p nss39x/117724 -f
# SunOS 5.10 patches
	./build_security_patch -p nss39x/117726 -f

patches_310x:
ifeq ($(OS_VERSION),SunOS)
# SunOS 5.8 patches
	./build_security_patch -p nss310x/119209 -f
# SunOS SunOS 5.9 patches
	./build_security_patch -p nss310x/119211 -f
# SunOS 5.10 patches
	./build_security_patch -p nss310x/119213 -f
# JSS point patch for SunOS 8/9
#	./build_security_patch -p nss310x/120778
# JSS point patch for SunOS 10
#	./build_security_patch -p nss310x/120785
else
ifeq ($(OS_VERSION),Linux)
# Linux patch
	./build_linux_patch -p nss310x/121656
# JSS point patch for Linux
#	./build_linux_patch -p nss310x/121648
else
ifeq ($(OS_VERSION),HP-UX)
# HP-UX patch
	./build_hpux_patch -p nss310x/124379
endif # HP-UX
endif # Linux
endif # SunOS

patches_311x:
ifeq ($(OS_VERSION),SunOS)
# SunOS 5.8 patches
	./build_security_patch -p nss310x/119209
# SunOS SunOS 5.9 patches
	./build_security_patch -p nss310x/119211
# SunOS 5.10 patches
	./build_security_patch -p nss310x/119213
# JSS point patch for SunOS 8/9
#	./build_security_patch -p nss310x/120778
# JSS point patch for SunOS 10
#	./build_security_patch -p nss310x/120785
# JES5 patch for SunOS
	./build_security_patch -p nss311x/125358 -f
else
ifeq ($(OS_VERSION),Linux)
# Linux patch
	./build_linux_patch -p nss310x/121656
# JSS point patch for Linux
#	./build_linux_patch -p nss310x/121648
else
ifeq ($(OS_VERSION),HP-UX)
# HP-UX patch
	./build_hpux_patch -p nss310x/124379
else
ifeq ($(OS_VERSION),WINNT)
# Windows patch
	./build_win_patch -p nss311x/125923
endif # WINNT
endif # HP-UX
endif # Linux
endif # SunOS

patches_312x:
ifeq ($(OS_VERSION),SunOS)
# SunOS 5.8 patches
	./build_security_patch -p nss310x/119209
# SunOS SunOS 5.9 patches
	./build_security_patch -p nss310x/119211
# SunOS 5.10 patches
	./build_security_patch -p nss310x/119213
# JES5 patch for SunOS
	./build_security_patch -p nss311x/125358 -f
else
ifeq ($(OS_VERSION),Linux)
# Linux patch
ifeq ($(OS_RELEASE),2.4)
	./build_linux_patch -p nss312x/142506
else
	./build_linux_patch -p nss310x/121656
endif
else
ifeq ($(OS_VERSION),HP-UX)
# HP-UX patch
	./build_hpux_patch -p nss310x/124379
else
ifeq ($(OS_VERSION),WINNT)
# Windows patch
	./build_win_patch -p nss311x/125923
endif # WINNT
endif # HP-UX
endif # Linux
endif # SunOS

# JSS jar file signature
ifeq ($(BUILD_OPT),1)
  SIGN_FROM_JAR_FILE=$(MDIST)/dist/xpclass.jar
  SIGN_TO_JAR_FILE=$(MDIST)/dist/release/no-policy/classes/jss4.jar
  NEW_XPCLASS_JAR=$(MDIST)/dist/release/no-policy/classes/xpclass.jar
else
  SIGN_FROM_JAR_FILE=$(MDIST)/dist/xpclass_dbg.jar
  SIGN_TO_JAR_FILE=$(MDIST)/dist/release/no-policy/classes_DBG/jss4_dbg.jar
  NEW_XPCLASS_JAR=$(MDIST)/dist/release/no-policy/classes_DBG/xpclass_dbg.jar
endif

sign_jss:
	@echo "Signing $(SIGN_TO_JAR_FILE) with `pwd`/JSS_sign/keystore"
	mkdir -p `dirname $(SIGN_TO_JAR_FILE)`/org
	cp $(SIGN_FROM_JAR_FILE) $(SIGN_TO_JAR_FILE)
	$(JAVA_HOME)/bin/jarsigner -keystore JSS_sign/keystore -storepass jssJCEsign -internalsf $(SIGN_TO_JAR_FILE) jssdsa
	$(JAVA_HOME)/bin/jarsigner -verify $(SIGN_TO_JAR_FILE)
	# create xpclass.jar for tests
	if [ -f $(NEW_XPCLASS_JAR) ] ; \
	then \
		rm $(NEW_XPCLASS_JAR) ;\
	fi
	ln -s $(SIGN_TO_JAR_FILE) $(NEW_XPCLASS_JAR)

