# Copyright 2000, International Business Machines Corporation and others.
# All Rights Reserved.
# 
# This software has been released under the terms of the IBM Public
# License.  For details, see the LICENSE file in the top-level source
# directory or online at http://www.openafs.org/dl/license10.html

DEST=@DEST@
TOP_SRCDIR=@TOP_SRCDIR@
TOP_INCDIR=@TOP_INCDIR@
TOP_LIBDIR=@TOP_LIBDIR@
TOP_JLIBDIR=@TOP_JLIBDIR@
JAVA_HOME=@JAVA_HOME@
JNI_INC=@JNI_INC@
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
sbindir=@sbindir@
libexecdir=@libexecdir@
libdir=@libdir@
includedir=@includedir@
mandir=@mandir@
afssrvbindir=@afssrvbindir@
afssrvsbindir=@afssrvsbindir@
afssrvlibexecdir=@afssrvlibexecdir@
COMPILE_ET=${TOP_SRCDIR}/comerr/compile_et
RXGEN=${TOP_SRCDIR}/rxgen/rxgen
SYS_NAME=@AFS_SYSNAME@

include ../../config/Makefile.${SYS_NAME}

CC = ${MT_CC}
SHARED_FLAGS = -shared
OBJECT_FLAGS = -fPIC -c

ifeq "$(BUILD_TYPE)" "admin"
	INC := -I${TOP_INCDIR} -I${TOP_INCDIR}/afs/ ${JNI_INC}
	CFLAGS := ${INC} ${DBG} ${OPTMZ} -I${TOP_SRCDIR}/config ${MT_CFLAGS}
else
	INC := -I${TOP_SRCDIR}/libuafs -I${TOP_INCDIR} -I${TOP_SRCDIR} ${JNI_INC}
	CFLAGS := ${INC} ${DBG} ${OPTMZ} ${FSINCLUDES} -D_REENTRANT -DLIBJUAFS ${MT_CFLAGS}
endif

ifeq "$(INCREMENT_BUILD)" "false"
	INCREMENT_CMD :=
else
	INCREMENT_CMD := ${RM} -f ${LIBJAFSADMDIR}VersionInfo.o; perl buildinfo.pl ${LIBJAFSADMDIR}VersionInfo.h -i;
endif

LIBJAFSADMDIR = ./
ROOTPACKAGEDIR = ../classes
RELPACKAGEDIR = org/openafs/jafs/
PACKAGEDIR = ${ROOTPACKAGEDIR}/${RELPACKAGEDIR}
JAVADOCSDIR = ../javadocs/
BUILD_VERSION = `perl buildinfo.pl ${LIBJAFSADMDIR}VersionInfo.h -version`

JAVAH = ${JAVA_HOME}/bin/javah -classpath ${ROOTPACKAGEDIR} -jni -d ${LIBJAFSADMDIR}
JAVAC = ${JAVA_HOME}/bin/javac -classpath ${ROOTPACKAGEDIR}
JAVADOC = ${JAVA_HOME}/bin/javadoc

J_NATIVE_PREFIX = org.openafs.jafs.
C_NATIVE_PREFIX = org_openafs_jafs_

PACKAGE =\
	${PACKAGEDIR}ACL.class \
	${PACKAGEDIR}AFSException.class \
	${PACKAGEDIR}AFSFileException.class \
	${PACKAGEDIR}AFSSecurityException.class \
	${PACKAGEDIR}Cell.class \
	${PACKAGEDIR}File.class \
	${PACKAGEDIR}FileInputStream.class \
	${PACKAGEDIR}FileOutputStream.class \
	${PACKAGEDIR}Group.class \
	${PACKAGEDIR}Key.class \
	${PACKAGEDIR}Partition.class \
	${PACKAGEDIR}Process.class \
	${PACKAGEDIR}Server.class \
	${PACKAGEDIR}Token.class \
	${PACKAGEDIR}User.class \
	${PACKAGEDIR}VersionInfo.class \
	${PACKAGEDIR}Volume.class

LIBJAFS_OBJS =\
	${LIBJAFSADMDIR}ACL.o \
	${LIBJAFSADMDIR}File.o \
	${LIBJAFSADMDIR}FileInputStream.o \
	${LIBJAFSADMDIR}FileOutputStream.o \
	${LIBJAFSADMDIR}Internal.o \
	${LIBJAFSADMDIR}UserToken.o \
	${LIBJAFSADMDIR}VersionInfo.o

LIBJAFSADM_OBJS =\
	${LIBJAFSADMDIR}AdminToken.o \
	${LIBJAFSADMDIR}Cell.o \
	${LIBJAFSADMDIR}Group.o \
	${LIBJAFSADMDIR}Internal.o \
	${LIBJAFSADMDIR}Key.o \
	${LIBJAFSADMDIR}Partition.o \
	${LIBJAFSADMDIR}Process.o \
	${LIBJAFSADMDIR}Server.o \
	${LIBJAFSADMDIR}User.o \
	${LIBJAFSADMDIR}VersionInfo.o \
	${LIBJAFSADMDIR}Volume.o

CORRELATING_SOURCE_FILES =\
	${LIBJAFSADMDIR}ACL.c \
	${LIBJAFSADMDIR}Cell.c \
	${LIBJAFSADMDIR}File.c \
	${LIBJAFSADMDIR}FileInputStream.c \
	${LIBJAFSADMDIR}FileOutputStream.c \
	${LIBJAFSADMDIR}Group.c \
	${LIBJAFSADMDIR}Key.c \
	${LIBJAFSADMDIR}Partition.c \
	${LIBJAFSADMDIR}Process.c \
	${LIBJAFSADMDIR}Server.c \
	${LIBJAFSADMDIR}User.c \
	${LIBJAFSADMDIR}VersionInfo.c \
	${LIBJAFSADMDIR}Volume.c

JAVA_HEADERS = ${PACKAGE:${PACKAGEDIR}%.class=${C_NATIVE_PREFIX}%.h}

BOSADMINLIB = ${TOP_LIBDIR}/libbosadmin.a
VOSADMINLIB = ${TOP_LIBDIR}/libvosadmin.a
PTSADMINLIB = ${TOP_LIBDIR}/libptsadmin.a
KASADMINLIB = ${TOP_LIBDIR}/libkasadmin.a
CFGADMINLIB = ${TOP_LIBDIR}/libcfgadmin.a
UTILADMINLIB = ${TOP_LIBDIR}/libafsadminutil.a
CLIENTADMINLIB = ${TOP_LIBDIR}/libclientadmin.a

LIBJAFS_LIBS =\
	${TOP_LIBDIR}/libjuafs.a \
	${TOP_LIBDIR}/libdes.a \
	-lresolv \
	-lpthread

LIBJAFSADM_LIBS =\
	${CLIENTADMINLIB} \
	${VOSADMINLIB} \
	${BOSADMINLIB} \
	${PTSADMINLIB} \
	${KASADMINLIB} \
	${CFGADMINLIB} \
	${UTILADMINLIB} \
	${TOP_LIBDIR}/libafsauthent.a \
	${TOP_LIBDIR}/libafsrpc.a \
	${TOP_LIBDIR}/libcmd.a \
	${TOP_LIBDIR}/util.a \
	-lresolv \
	-lpthread


JARFILE = jafs.jar

all:  ${TOP_JLIBDIR} libjafs libjafsadm ${PACKAGE} jar

install:  all ${DESTDIR}${libdir}/libjafs.so ${DESTDIR}${libdir}/libjafsadm.so ${PACKAGE} install_jar
	if [ ! -e /usr/afswsp ]; then \
	  mkdir -p /usr/afswsp/; \
	fi; \
	if [ ! -e /usr/afswsp/etc ]; then \
	  mkdir -p /usr/afswsp/etc/; \
	  cp ./etc/CacheConfig /usr/afswsp/etc/; \
	fi; \
	if [ ! -e /usr/afswsp/log ]; then \
	  mkdir -p /usr/afswsp/log/; \
	fi; \
	if [ ! -e /usr/afswsp/cache ]; then \
	  mkdir -p /usr/afswsp/cache/; \
	fi; \
	if [ ! -L /usr/vice/etc/CellServDB ]; then \
	  ln -s /usr/vice/etc/CellServDB /usr/afswsp/etc/; \
	fi; \
	if [ ! -L /usr/vice/etc/ThisCell ]; then \
	  ln -s /usr/vice/etc/ThisCell /usr/afswsp/etc/; \
	fi

### Clean "C" and Java objects
clean:
	${RM} -f ${PACKAGEDIR}*.class ${LIBJAFSADMDIR}*.o ${LIBJAFSADMDIR}${C_NATIVE_PREFIX}*.h ${LIBJAFSADMDIR}acltest
	${RM} -fR ${JAVADOCSDIR}

### Clean just "C" objects
cleanc:
	${RM} -f ${LIBJAFSADMDIR}*.o ${LIBJAFSADMDIR}acltest

increment-build:
	${INCREMENT_CMD} \
        export INCREMENT_BUILD=false;

setup:	FORCE

${TOP_JLIBDIR}:
	mkdir -p $@

FORCE: ;

############# Test program   ###############################

acltest: ${LIBJAFSADMDIR}/acltest.c
	${CC} ${CFLAGS} -o $@ $^ ${LIBJAFS_LIBS}

############# Shared library ###############################

libjafs: setup increment-build
	${RM} -f ${LIBJAFSADMDIR}Internal.o; \
	export BUILD_TYPE=user; \
	${MAKE} ${TOP_LIBDIR}/libjafs.so

libjafsadm: increment-build
	${RM} -f ${LIBJAFSADMDIR}Internal.o; \
	export BUILD_TYPE=admin; \
	${MAKE} ${TOP_LIBDIR}/libjafsadm.so

${TOP_LIBDIR}/libjafs.so: ${LIBJAFS_OBJS}
	${CC} ${CFLAGS} ${SHARED_FLAGS} -o $@ $^ ${LIBJAFS_LIBS}

${DESTDIR}${libdir}/libjafs.so: ${LIBJAFS_OBJS}
	${CC} ${CFLAGS} ${SHARED_FLAGS} -o $@ $^ ${LIBJAFS_LIBS}

${TOP_LIBDIR}/libjafsadm.so: ${LIBJAFSADM_OBJS}
	${CC} ${CFLAGS} ${SHARED_FLAGS} -o $@ $^ ${LIBJAFSADM_LIBS}

${DESTDIR}${libdir}/libjafsadm.so: ${LIBJAFSADM_OBJS}
	${CC} ${CFLAGS} ${SHARED_FLAGS} -o $@ $^ ${LIBJAFSADM_LIBS}

############## Object files ################################

${LIBJAFSADM_OBJS}: %.o: %.c
	${CC} ${CFLAGS} ${OBJECT_FLAGS} -o $@ $<

############## C files #####################################

${CORRELATING_SOURCE_FILES}: ${LIBJAFSADMDIR}%.c: ${LIBJAFSADMDIR}${C_NATIVE_PREFIX}%.h ${LIBJAFSADMDIR}Internal.h ${LIBJAFSADMDIR}VersionInfo.h

${LIBJAFSADMDIR}AdminToken.c: ${LIBJAFSADMDIR}${C_NATIVE_PREFIX}Token.h ${LIBJAFSADMDIR}${C_NATIVE_PREFIX}Cell.h

${LIBJAFSADMDIR}Internal.c: ${LIBJAFSADMDIR}Internal.h

${LIBJAFSADMDIR}UserToken.c: ${LIBJAFSADMDIR}${C_NATIVE_PREFIX}Token.h

${LIBJAFSADMDIR}VersionInfo.c: ${LIBJAFSADMDIR}VersionInfo.h ${LIBJAFSADMDIR}${C_NATIVE_PREFIX}VersionInfo.h

############## Package javac section #########################

${PACKAGEDIR}%.class: ${PACKAGEDIR}%.java
	${JAVAC} $<

############## Javah section ###############################

${JAVA_HEADERS}: ${C_NATIVE_PREFIX}%.h: ${PACKAGEDIR}%.class
	${JAVAH} ${J_NATIVE_PREFIX}$*

############## Javadoc section ###############################

javadocs:
	echo "Preparing Javadoc API documentation..."
	${JAVADOC} -version -breakiterator \
	-link http://java.sun.com/j2se/1.4/docs/api/ \
	-windowtitle "Java AFS API" -header "<B>JAFS API v${BUILD_VERSION}</B>" \
	-doctitle "<B>JAFS</B> API v${BUILD_VERSION}" \
	-use -d ${JAVADOCSDIR} \
	-sourcepath ${ROOTPACKAGEDIR} \
	-classpath ${ROOTPACKAGEDIR} \
	-package org.openafs.jafs

############# JAR file #####################################

jar: clean_jar
	cd ${ROOTPACKAGEDIR}; ${JAVA_HOME}/bin/jar -cMf ${TOP_JLIBDIR}/${JARFILE} *.properties ${RELPACKAGEDIR}*.class

install_jar: clean_jar
	cd ${ROOTPACKAGEDIR}; ${JAVA_HOME}/bin/jar -cMf ${JAVA_HOME}/lib/${JARFILE} *.properties ${RELPACKAGEDIR}*.class

clean_jar:
	${RM} -f ${TOP_JLIBDIR}/${JARFILE}

clean_libs:
	${RM} -f ${TOP_LIBDIR}/libjafs.so ${TOP_LIBDIR}/libjafsadm.so

