# API version. When something changes, increment as appropriate. 
# Ignore at your own risk.
LIBAFSSETPAGMAJOR=1
LIBAFSSETPAGMINOR=0

# 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
#
# Portions Copyright (c) 2003 Apple Computer, Inc.

srcdir=@srcdir@
include @TOP_OBJDIR@/src/config/Makefile.config

CFLAGS=-I. -I${srcdir} ${DBUG} -I${TOP_OBJDIR}/src/config -I${TOP_INCDIR}  ${XCFLAGS} ${ARCHFLAGS}
SFLAGS=-I${TOP_INCDIR}
LIBS=libsys.a ${TOP_LIBDIR}/librx.a libsys.a ${TOP_LIBDIR}/liblwp.a ${TOP_LIBDIR}/util.a ${XLIBS}

OBJECTS= afssyscalls.o setpag.o glue.o
SHLIBOBJS = picobj/setpag.o picobj/glue.o
RMTOBJS=rmtsysnet.o rmtsysc.o rmtsys.cs.o rmtsys.xdr.o rmtsys.ss.o rmtsyss.o 

LIBAFSSETPAG = libafssetpag.${SHLIB_SUFFIX}.${LIBAFSSETPAGMAJOR}.${LIBAFSSETPAGMINOR}
LIBAFSSETPAG_DARWIN = libafssetpag.${LIBAFSSETPAGMAJOR}.${SHLIB_SUFFIX}

all: \
    rmtsysd \
    pagsh \
    pagsh.krb \
    ${TOP_INCDIR}/afs/afssyscalls.h \
    ${TOP_INCDIR}/afs/afs.exp \
    ${TOP_INCDIR}/afs/xfsattrs.h \
    ${TOP_LIBDIR}/afs.exp \
    ${TOP_LIBDIR}/libsys.a \
    ${KERNELDIR}/afs \
    ${KERNELDIR}/afs/xfsattrs.h \
    ${UKERNELDIR}/afs \
    ${UKERNELDIR}/afs/afsl.exp
	@set -x; case ${SYS_NAME} in \
	*_darwin_*) \
	    ${MAKE} '${TOP_LIBDIR}/${LIBAFSSETPAG_DARWIN}';; \
	*) \
	    ${MAKE} '${TOP_LIBDIR}/${LIBAFSSETPAG}';; \
	esac

libsys.a: ${OBJECTS} ${RMTOBJS} syscall.o afsl.exp AFS_component_version_number.o
	-$(RM) -f $@
	@set -x; case "${SYS_NAME}" in                   	\
		hp_ux11*) \
			$(AR) crv $@ ${OBJECTS} ${RMTOBJS} \
				AFS_component_version_number.o ${LIBSYS_AIX_EXP} ;; \
		*) \
			$(AR) crv $@ ${OBJECTS} ${RMTOBJS} syscall.o \
				AFS_component_version_number.o ${LIBSYS_AIX_EXP} ;; \
	esac
	$(RANLIB) $@
	@set -x; case "${SYS_NAME}" in                   	\
		rs_aix*) 			        \
			$(AR) crv $@ afsl.exp;;         \
	esac

${LIBAFSSETPAG}: ${SHLIBOBJS} syscall.o
	@set -x; case ${SYS_NAME} in \
	*_linux*) \
	    ${SHLIB_LINKER} -Wl,-h,libafssetpag.so.${LIBAFSSETPAGMAJOR} \
	        -o ${LIBAFSSETPAG} -Wl,--version-script=${srcdir}/mapfile \
	        ${SHLIBOBJS} syscall.o ;; \
	rs_aix4*) \
	    ${SHLIB_LINKER} -o ${LIBAFSSETPAG} ${SHLIBOBJS} syscall.o \
	        -bE:afssetpag.exp;; \
	sun*_5*) \
	    ${SHLIB_LINKER} -h libafssetpag.so.${LIBAFSSETPAGMAJOR} \
	        -o ${LIBAFSSETPAG} ${SHLIBOBJS} syscall.o;; \
	hp_ux11*) \
	    ${SHLIB_LINKER} -o ${LIBAFSSETPAG} ${SHLIBOBJS};; \
	*) \
	    ${SHLIB_LINKER} -o ${LIBAFSSETPAG} ${SHLIBOBJS} syscall.o ;; \
	esac

${LIBAFSSETPAG_DARWIN}: ${SHLIBOBJS}
	${SHLIB_LINKER} -o ${LIBAFSSETPAG_DARWIN} ${ARCHFLAGS} -install_name ${libdir}/${LIBAFSSETPAG_DARWIN} -compatibility_version ${LIBAFSSETPAGMAJOR} -current_version ${LIBAFSSETPAGMAJOR}.${LIBAFSSETPAGMINOR} ${SHLIBOBJS}

tests:	pagsh pagsh.krb fixit iinc idec icreate iopen istat rmtsysd

syscall.o: syscall.s
	@set -x; case "$(SYS_NAME)" in \
	 sun4c_51 | sun4c_52 | sun4m_51 | sun4m_52 | sun4c_53 | sun4m_53  | sun4_53 | sun4_52 | sun4_54 | sun4c_54 | sun4m_54 | sun4x_5? | sun4x_5?? | sunx86_5? | sunx86_5?? ) \
		$(PATH_CPP) -P ${SFLAGS} ${srcdir}/syscall.s > syscall.ss; \
		as -o syscall.o syscall.ss;		\
		$(RM) syscall.ss;;				\
	 sgi_* |*_darwin* ) \
                ${CC} ${CFLAGS} -c ${srcdir}/syscall.s;;          \
	 alpha_osf1 | alpha_osf20 |  alpha_osf30 | alpha_osf32 | alpha_osf32c | alpha_dux?? ) \
		${AS} -P ${CFLAGS} -D_NO_PROTO -DMACH -DOSF -nostdinc -traditional -DASSEMBLER ${srcdir}/syscall.s; \
		${AS} -o syscall.o syscall.i; \
		$(RM) -f syscall.ss syscall.i;; \
	 hp_ux11? ) \
		touch syscall.o;; \
	 amd64_fbsd*|i386_*bsd*|*nbsd*) \
		${CCOBJ} -E ${SFLAGS} syscall.s > syscall.S ; \
		${AS} syscall.S -o syscall.o ;  \
		$(RM) -f syscall.S ;;\
	 *) \
		$(PATH_CPP) -P ${SFLAGS} ${srcdir}/syscall.s > syscall.ss; \
		as -o syscall.o syscall.ss;		\
		$(RM) syscall.ss;;				\
	esac

afssyscalls.o: afssyscalls.c afssyscalls.h
	${CCOBJ} ${CFLAGS} -c ${srcdir}/afssyscalls.c

glue.o: glue.c afssyscalls.h
	${CCOBJ} ${CFLAGS} -c ${srcdir}/glue.c

setpag.o: setpag.c afssyscalls.h
	${CCOBJ} ${CFLAGS} -c ${srcdir}/setpag.c

picobj: afssyscalls.h
	mkdir -p picobj
	cp ${srcdir}/afssyscalls.h picobj/afssyscalls.h

picobj/glue.o: picobj glue.c afssyscalls.h
	rm -f picobj/glue.c
	cp ${srcdir}/glue.c picobj/glue.c
	cd picobj && ${CCOBJ} ${CFLAGS} ${SHLIB_CFLAGS} -c glue.c

picobj/setpag.o: picobj setpag.c afssyscalls.h
	rm -f picobj/setpag.c
	cp ${srcdir}/setpag.c picobj/setpag.c
	cd picobj && ${CCOBJ} ${CFLAGS} ${SHLIB_CFLAGS} -c setpag.c

rmtsysnet.o rmtsysc.o rmtsyss.o rmtsysd.o: rmtsys.h
rmtsysd: AFS_component_version_number.o afs.exp afsl.exp

rmtsys.cs.c: rmtsys.xg
	${RXGEN} -A -C -o $@ ${srcdir}/rmtsys.xg

rmtsys.ss.c: rmtsys.xg
	${RXGEN} -A -S -o $@ ${srcdir}/rmtsys.xg

rmtsys.xdr.c: rmtsys.xg
	${RXGEN} -A -c -o $@ ${srcdir}/rmtsys.xg

rmtsys.h: rmtsys.xg
	${RXGEN} -A -h -o $@ ${srcdir}/rmtsys.xg

Krmtsys.cs.c: rmtsys.xg Krmtsys.h
	${RXGEN} -A -k -C -o Krmtsys.cs.c ${srcdir}/rmtsys.xg

Krmtsys.xdr.c: rmtsys.xg
	${RXGEN} -A -k -c -o Krmtsys.xdr.c ${srcdir}/rmtsys.xg

Krmtsys.h: rmtsys.xg
	${RXGEN} -A -k -h -o Krmtsys.h ${srcdir}/rmtsys.xg

rmtsysd: rmtsysd.o libsys.a
	${CC} ${CFLAGS} -o rmtsysd rmtsysd.o ${LIBS}

pagsh:	libsys.a AFS_component_version_number.o
	${CC} ${CFLAGS} -c ${srcdir}/pagsh.c
	${CC} ${CFLAGS} -o pagsh pagsh.o ${LIBS}

pagsh.krb: libsys.a
	${CC} ${CFLAGS} -c ${srcdir}/pagsh.c -DAFS_KERBEROS_ENV 
	${CC} ${CFLAGS} -o pagsh.krb pagsh.o ${LIBS}

#
# Test programs.
#

iinc:	iinc.c 
	$(CC) -o iinc ${CFLAGS} ${srcdir}/iinc.c ${LIBS}

idec:	idec.c  AFS_component_version_number.c
	$(CC) -o idec ${CFLAGS} ${srcdir}/idec.c ${LIBS}

icreate:icreate.c  AFS_component_version_number.c
	$(CC) -o icreate ${CFLAGS} ${srcdir}/icreate.c ${LIBS}

iopen:	iopen.c  AFS_component_version_number.c
	$(CC) -o iopen ${CFLAGS} ${srcdir}/iopen.c ${LIBS}

iread:	iread.c  AFS_component_version_number.c
	${CC} -o iread ${CFLAGS} ${srcdir}/iread.c ${LIBS}

iwrite:	iwrite.c  AFS_component_version_number.c
	${CC} -o iwrite ${CFLAGS} ${srcdir}/iwrite.c ${LIBS}

istat:	istat.c  AFS_component_version_number.c
	$(CC) -o istat ${CFLAGS} ${srcdir}/istat.c ${LIBS}

fixit:	fixit.c AFS_component_version_number.c
	$(CC) -o fixit ${CFLAGS} ${srcdir}/fixit.c ${LIBS}


xfsinode: xfsinode.c  AFS_component_version_number.c
	@set -x; case "${SYS_NAME}" in \
		sgi_62 | sgi_64 ) \
		$(CC) -o xfsinode ${CFLAGS} ${srcdir}/xfsinode.c ${LIBS}
	esac

${TOP_LIBDIR}/${LIBAFSSETPAG}: ${LIBAFSSETPAG}
	${INSTALL} ${LIBAFSSETPAG} ${TOP_LIBDIR}/${LIBAFSSETPAG}
	-ln -f -s ${LIBAFSSETPAG} ${TOP_LIBDIR}/libafssetpag.${SHLIB_SUFFIX}
	-ln -f -s ${LIBAFSSETPAG} ${TOP_LIBDIR}/libafssetpag.${SHLIB_SUFFIX}.${LIBAFSSETPAGMAJOR}

${TOP_LIBDIR}/${LIBAFSSETPAG_DARWIN}: ${LIBAFSSETPAG_DARWIN}
	${INSTALL} ${LIBAFSSETPAG_DARWIN} ${TOP_LIBDIR}/${LIBAFSSETPAG_DARWIN}
	-ln -f -s ${LIBAFSSETPAG_DARWIN} ${TOP_LIBDIR}/libafssetpag.${SHLIB_SUFFIX}
	-ln -f -s ${LIBAFSSETPAG_DARWIN} ${TOP_LIBDIR}/libafssetpag.${LIBAFSSETPAGMAJOR}.${LIBAFSSETPAGMINOR}.${SHLIB_SUFFIX}
 

afs.exp: ${srcdir}/afs4.exp ${srcdir}/afs5.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix[56]* ) \
			cp -p ${srcdir}/afs5.exp afs.exp ;; \
		rs_aix* ) \
			cp -p ${srcdir}/afs4.exp afs.exp ;; \
		* ) \
			touch afs.exp ;; \
	esac

afsl.exp: ${srcdir}/afsl4.exp ${srcdir}/afsl5.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix[56]* ) \
			cp -p ${srcdir}/afsl5.exp afsl.exp ;; \
		rs_aix* ) \
			cp -p ${srcdir}/afsl4.exp afsl.exp ;; \
		* ) \
			touch afsl.exp ;; \
	esac

#
# Installation targets
#

${TOP_INCDIR}/afs/afssyscalls.h: afssyscalls.h
	${INSTALL} $? $@

${TOP_INCDIR}/afs/rmtsys.h: rmtsys.h
	${INSTALL} $? $@

${TOP_INCDIR}/afs/afs.exp: afs.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL} $? $@ ;; \
	esac

${TOP_LIBDIR}/afs.exp: afs.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL} $? $@ ;; \
	esac

${TOP_INCDIR}/afs/xfsattrs.h: xfsattrs.h
	${INSTALL} $? $@

${TOP_LIBDIR}/libsys.a: libsys.a
	${INSTALL} $? $@

${UKERNELDIR}/afs:
	mkdir -p $@

${UKERNELDIR}/afs/afsl.exp: afsl.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL} $? $@ ;; \
	esac

${KERNELDIR}/afs:
	mkdir -p $@

${KERNELDIR}/afs/xfsattrs.h: xfsattrs.h
	${INSTALL} $? $@

depinstall: \
	${TOP_INCDIR}/afs/afssyscalls.h \
	${TOP_INCDIR}/afs/rmtsys.h \
	${TOP_INCDIR}/afs/afs.exp \
	${TOP_INCDIR}/afs/xfsattrs.h \
	${KERNELDIR}/afs/xfsattrs.h \
	${UKERNELDIR}/afs/afsl.exp \
	Krmtsys.cs.c Krmtsys.h Krmtsys.xdr.c rmtsys.h 

install: \
    ${DESTDIR}${libdir}/afs/libsys.a \
    ${DESTDIR}${sbindir}/rmtsysd \
    ${DESTDIR}${includedir}/afs/afssyscalls.h \
    ${DESTDIR}${bindir}/pagsh \
    ${DESTDIR}${bindir}/pagsh.krb \
    ${DESTDIR}${includedir}/afs/afs.exp \
    ${DESTDIR}${includedir}/afs/xfsattrs.h
	@set -x; case ${SYS_NAME} in \
	*_darwin_*) \
	    ${MAKE} '${DESTDIR}${libdir}/${LIBAFSSETPAG_DARWIN}';; \
	*) \
	    ${MAKE} '${DESTDIR}${libdir}/${LIBAFSSETPAG}';; \
	esac

${DESTDIR}${libdir}/afs/libsys.a: libsys.a
	${INSTALL} $? $@

${DESTDIR}${libdir}/${LIBAFSSETPAG}: ${LIBAFSSETPAG}
	${INSTALL} ${LIBAFSSETPAG} ${DESTDIR}${libdir}/${LIBAFSSETPAG}
	-ln -f -s ${LIBAFSSETPAG} \
	    ${DESTDIR}${libdir}/libafssetpag.${SHLIB_SUFFIX}
	-ln -f -s ${LIBAFSSETPAG} \
	    ${DESTDIR}${libdir}/libafssetpag.${SHLIB_SUFFIX}.${LIBAFSSETPAGMAJOR}

${DESTDIR}${libdir}/${LIBAFSSETPAG_DARWIN}: ${LIBAFSSETPAG_DARWIN}
	${INSTALL} ${LIBAFSSETPAG_DARWIN} ${DESTDIR}${libdir}/${LIBAFSSETPAG_DARWIN}
	-ln -f -s ${LIBAFSSETPAG_DARWIN} \
		${DESTDIR}${libdir}/libafssetpag.${SHLIB_SUFFIX} ; \
	-ln -f -s ${LIBAFSSETPAG_DARWIN} \
		${DESTDIR}${libdir}/libafssetpag.${LIBAFSSETPAGMAJOR}.${LIBAFSSETPAGMINOR}.${SHLIB_SUFFIX} ;; \

${DESTDIR}${sbindir}/rmtsysd: rmtsysd
	${INSTALL} $? $@

${DESTDIR}${includedir}/afs/afssyscalls.h: afssyscalls.h
	${INSTALL} $? $@

${DESTDIR}${bindir}/pagsh: pagsh
	${INSTALL} $? $@

${DESTDIR}${bindir}/pagsh.krb: pagsh.krb
	${INSTALL} ${NO_STRIP_KRB} $? $@

${DESTDIR}${includedir}/afs/afs.exp: afs.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL} $? $@ ;; \
	esac

${DESTDIR}${includedir}/afs/xfsattrs.h: xfsattrs.h
	${INSTALL} $? $@

dest: \
    ${DEST}/lib/afs/libsys.a \
    ${DEST}/etc/rmtsysd \
    ${DEST}/bin/pagsh \
    ${DEST}/bin/pagsh.krb \
    ${DEST}/include/afs/afssyscalls.h \
    ${DEST}/include/afs/afs.exp \
    ${DEST}/include/afs/xfsattrs.h
	@set -x; case ${SYS_NAME} in \
	*_darwin_*) \
	    ${MAKE} '${TOP_LIBDIR}/${LIBAFSSETPAG_DARWIN}';; \
	*) \
	    ${MAKE} '${TOP_LIBDIR}/${LIBAFSSETPAG}';; \
	esac

${DEST}/lib/afs/libsys.a: libsys.a
	${INSTALL} $? $@

${DEST}/include/afs/afs.exp: afs.exp
	@set -x; case ${SYS_NAME} in \
		rs_aix* ) \
			${INSTALL} $? $@ ;; \
	esac

${DEST}/include/afs/xfsattrs.h: xfsattrs.h
	${INSTALL} $? $@

${DEST}/include/afs/afssyscalls.h: afssyscalls.h
	${INSTALL} $? $@

${DEST}/etc/rmtsysd: rmtsysd
	${INSTALL} $? $@

${DEST}/bin/pagsh: pagsh
	${INSTALL} $? $@

${DEST}/bin/pagsh.krb: pagsh.krb
	${INSTALL} ${NO_STRIP_KRB} $? $@

#
# Misc targets
#
include ../config/Makefile.version

clean:
	$(RM) -f *.o libsys.a xfsinode iinc idec icreate iopen istat core \
	rmtsysc rmtsyss *.o rmtsys.ss.c rmtsys.cs.c rmtsys.xdr.c rmtsys.h \
	rmtsysd AFS_component_version_number.c pagsh pagsh.krb \
	afs.exp afsl.exp libafssetpag.* Krmtsys.cs.c Krmtsys.h Krmtsys.xdr.c
	$(RM) -rf picobj
