#/*
# 
# Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
# 
# The contents of this file are subject to the Sun Community Source
# License v3.0/Jini Technology Specific Attachment v 1.0 (the
# "License"). You may not use this file except in compliance with the
# License. You may obtain a copy of the License at
# http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Reference Code is Jini Technology Core Platform code, v2.0. The
# Developer of the Reference Code is Sun Microsystems, Inc.
# 
# Contributor(s): Sun Microsystems, Inc.
# 
# The contents of this file comply with the Jini Technology Core
# Platform Compatibility Kit, v2.0A.
# 
# Tester(s): Sun Microsystems, Inc.
# 
# Test Platform(s):
# Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
# Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
# Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
# 
#*/

include ../../../../../GNUmakefile.allvobs

# Unix makefile to build keystore and password files, compile sources
# and build jars.

JAVA_bin	:= $(JAVA_HOME)/bin
JAVAC		:= $(JAVA_bin)/javac
KEYTOOL		:= $(JAVA_bin)/keytool
RMIC		:= $(JAVA_bin)/rmic
JAR		:= $(JAVA_bin)/jar
JAVA		:= $(JAVA_bin)/java
SDKTOOLS  = $(JAVA_HOME)/lib/tools.jar

JINILIB = ../../../../../../../../../lib
JINITOOLS = $(JINILIB)/tools.jar
JINICLASSES = $(JINILIB)/jini-core.jar:$(JINILIB)/jini-ext.jar:$(JINILIB)sun-util.jar

CLASSDEP_EXEC = $(JAVA) -cp ../../$(JINITOOLS):$(SDKTOOLS) com.sun.jini.tool.ClassDep
CLASSDEP = $(CLASSDEP_EXEC) -cp .

all: keys jars 

hello_jars: $(HELLO_JARS)
.PHONY: hello_jars

# Build keystore and password files
keys: keystores password truststore

# Build keystores
keystores: \
	lib/client.keystore \
	lib/server.keystore \
	lib/phoenix.keystore \
	lib/reggie.keystore

lib/client.keystore:
	@rm -f lib/client.keystore
	@mkdir -p lib
	$(KEYTOOL) -keypass clientpw -storepass clientpw \
		   -keystore lib/client.keystore \
		   -genkey -validity 1800 -alias client -dname CN=Client
	$(KEYTOOL) -keypass clientpw -storepass clientpw \
		   -keystore lib/client.keystore \
		   -export -alias client -file lib/client.cert

lib/server.keystore:
	@rm -f lib/server.keystore
	@mkdir -p lib
	$(KEYTOOL) -keypass serverpw -storepass serverpw \
		   -keystore lib/server.keystore \
		   -genkey -validity 1800 -alias server -dname CN=Server
	$(KEYTOOL) -keypass serverpw -storepass serverpw \
		   -keystore lib/server.keystore \
		   -export -alias server -file lib/server.cert

lib/phoenix.keystore:
	@rm -f lib/phoenix.keystore
	@mkdir -p lib
	$(KEYTOOL) -keypass phoenixpw -storepass phoenixpw \
		   -keystore lib/phoenix.keystore \
		   -genkey -validity 1800 -alias phoenix -dname CN=Phoenix
	$(KEYTOOL) -keypass phoenixpw -storepass phoenixpw \
		   -keystore lib/phoenix.keystore \
		   -export -alias phoenix -file lib/phoenix.cert

lib/reggie.keystore:
	@rm -f lib/reggie.keystore
	@mkdir -p lib
	$(KEYTOOL) -keypass reggiepw -storepass reggiepw \
		   -keystore lib/reggie.keystore \
		   -genkey -validity 1800 -alias reggie -dname CN=Reggie
	$(KEYTOOL) -keypass reggiepw -storepass reggiepw \
		   -keystore lib/reggie.keystore \
		   -export -alias reggie -file lib/reggie.cert

# Build password file
password: \
	lib/server.password	\
	lib/phoenix.password	\
	lib/reggie.password

lib/server.password:
	@rm -f lib/server.password
	@mkdir -p lib
	echo serverpw > lib/server.password

lib/phoenix.password:
	@rm -f lib/phoenix.password
	@mkdir -p lib
	echo phoenixpw > lib/phoenix.password

lib/reggie.password:
	@rm -f lib/reggie.password
	@mkdir -p lib
	echo reggiepw > lib/reggie.password

# Import certificates into truststore
truststore: lib/truststore

lib/truststore:
	@rm -f lib/truststore
	@mkdir -p lib
	$(KEYTOOL) -keypass trustpw -storepass trustpw \
		   -keystore lib/truststore \
		   -import -noprompt -alias client -file lib/client.cert
	$(KEYTOOL) -keypass trustpw -storepass trustpw \
		   -keystore lib/truststore \
		   -import -noprompt -alias server -file lib/server.cert
	$(KEYTOOL) -keypass trustpw -storepass trustpw \
		   -keystore lib/truststore \
		   -import -noprompt -alias phoenix -file lib/phoenix.cert
	$(KEYTOOL) -keypass trustpw -storepass trustpw \
		   -keystore lib/truststore \
		   -import -noprompt -alias reggie -file lib/reggie.cert

# Compile classes
classes:
	@mkdir -p lib/classes
	$(JAVAC) -d lib/classes \
		 -classpath lib/classes:$(JINICLASSES)	\
		ActivatableServer.java			\
		Client.java				\
		ConfirmingILFactory.java		\
		ConfirmingInvocationHandler.java	\
		Hello.java				\
		MdClassAnnotationProvider.java		\
		Proxy.java				\
		Server.java				\
		ServerPermission.java

# Run rmic to create JRMP stubs -- not used for Jini ERI
stubs: classes
	@rm -f lib/classes/com/sun/jini/example/hello/Server_Stub.class
	$(RMIC) -v1.2 -classpath lib/classes:$(JINICLASSES) -d lib/classes \
		com.sun.jini.example.hello.Server

# Create jars
jars: lib/client.jar lib/server.jar lib/server-act.jar \
		lib/server-dl.jar lib/mdprefld.jar 
.PHONY: jars

# We need to unjar the Jini world to use ClassDep
lib/classes/net : 
	cd lib/classes; jar -xf ../../$(JINILIB)/jini-core.jar
	cd lib/classes; jar -xf ../../$(JINILIB)/jini-ext.jar

lib/client.jar: stubs lib/classes/net
	cd lib/classes;							\
	$(JAR) cmf ../../client.mf ../../lib/client.jar 		\
              `$(CLASSDEP)                                              \
	  com.sun.jini.example.hello.Client				\
                -in net.jini                                            \
                -in com.sun.jini                                        \
                $(JSKPLATFORM.EXCLUDE) -files`

lib/server.jar: stubs lib/classes/net
	cd lib/classes;	\
	$(JAR) cmf ../../server.mf ../../lib/server.jar 		\
              `$(CLASSDEP)                                              \
	   com.sun.jini.example.hello.Server 				\
	   com.sun.jini.example.hello.Server_Stub 			\
	   com.sun.jini.example.hello.ServerPermission 			\
	   com.sun.jini.example.hello.ConfirmingILFactory 		\
	   com.sun.jini.example.hello.ConfirmingInvocationHandler	\
                -in net.jini                                            \
                -in com.sun.jini                                        \
                $(JSKPLATFORM.EXCLUDE) -files`

lib/server-act.jar: stubs lib/classes/net
	cd lib/classes; 						\
	$(JAR) cmf ../../server-act.mf ../server-act.jar 		\
              `$(CLASSDEP)                                              \
	   com.sun.jini.example.hello.ActivatableServer 		\
	   com.sun.jini.example.hello.ConfirmingILFactory 		\
	   com.sun.jini.example.hello.ConfirmingInvocationHandler 	\
	   com.sun.jini.example.hello.Server 				\
	   com.sun.jini.example.hello.Server_Stub 			\
	   com.sun.jini.example.hello.ServerPermission 			\
                -in net.jini                                            \
                -in com.sun.jini                                        \
                $(JSKPLATFORM.EXCLUDE) -files`

lib/server-dl.jar : stubs lib/classes/net
	cd lib/classes; 						\
	$(JAR) cf ../server-dl.jar 					\
              `$(CLASSDEP)                                              \
	   com.sun.jini.example.hello.ConfirmingInvocationHandler 	\
	   com.sun.jini.example.hello.Hello 				\
	   com.sun.jini.example.hello.Proxy 				\
	   com.sun.jini.example.hello.ServerPermission 			\
	   com.sun.jini.example.hello.Server_Stub 			\
                -in net.jini                                            \
                -in com.sun.jini                                        \
                $(JSKPLATFORM.EXCLUDE) -files`
	cd config; 							\
	$(JAR) uf ../lib/server-dl.jar 					\
	       META-INF/PREFERRED.LIST

lib/mdprefld.jar : stubs
	cd lib/classes; 						\
	$(JAR) cf ../mdprefld.jar 					\
	   com/sun/jini/example/hello/MdClassAnnotationProvider.class

# Clean
clean:
	rm -rf lib

