# 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

srcdir=@srcdir@
include @TOP_OBJDIR@/src/config/Makefile.config
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
KRB5CFLAGS = @KRB5_CPPFLAGS@
KRB5LIBS = @KRB5_LDFLAGS@ @KRB5_LIBS@

INCLS=${TOP_INCDIR}/afs/afsint.h \
	${TOP_INCDIR}/afs/cmd.h \
	${TOP_INCDIR}/afs/afsutil.h

AFSIO_INCLS=${TOP_INCDIR}/afs/afsint.h \
	${TOP_INCDIR}/afs/cmd.h \
	${TOP_INCDIR}/afs/auth.h \
	${TOP_INCDIR}/afs/vlserver.h \
	${TOP_INCDIR}/afs/ihandle.h \
	${TOP_INCDIR}/afs/com_err.h \
	${TOP_INCDIR}/afs/afscp.h \
	${TOP_INCDIR}/afs/afsutil.h

AFSIO_LIBS=${TOP_LIBDIR}/libafscp.a \
	${TOP_LIBDIR}/libafsauthent.a \
	${TOP_LIBDIR}/libafsrpc.a \
	${TOP_LIBDIR}/libcmd.a \
	$(TOP_LIBDIR)/libafsutil.a

FSLIBS=${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/libvldb.a \
	 ${TOP_LIBDIR}/libubik.a \
	 ${TOP_LIBDIR}/vlib.a \
	 ${TOP_LIBDIR}/libauth.a \
	 ${TOP_LIBDIR}/librxkad.a \
	 ${TOP_LIBDIR}/libafscom_err.a \
	 ${TOP_LIBDIR}/libcmd.a \
	 ${TOP_LIBDIR}/libkauth.a \
	 ${TOP_LIBDIR}/libdes.a \
	 ${TOP_LIBDIR}/librx.a \
	 ${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/liblwp.a \
	 ${TOP_LIBDIR}/libaudit.a \
	 $(TOP_LIBDIR)/libafsutil.a

CMLIBS=${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/libafsint.a \
	 ${TOP_LIBDIR}/librxkad.a \
	 ${TOP_LIBDIR}/libauth.a \
	 ${TOP_LIBDIR}/libafscom_err.a \
	 ${TOP_LIBDIR}/libcmd.a \
	 ${TOP_LIBDIR}/librx.a \
	 ${TOP_LIBDIR}/libsys.a \
	 ${TOP_LIBDIR}/liblwp.a \
	 $(TOP_LIBDIR)/libafsutil.a

LIBS = ${FSLIBS} 

all: fs up fstrace cmdebug livesys kdump-build cacheout afsio

#
# Build targets
#
cacheout: cacheout.o
	$(CC) ${CFLAGS} -o cacheout cacheout.o ${LIBS} ${XLIBS} ${CMLIBS} ${KRB5_LIBS}

cacheout.o: cacheout.c

PTH_CFLAGS=$(COMMON_CFLAGS) $(MT_CFLAGS)
PTH_LDFLAGS=$(COMMON_LDFLAGS) $(MT_LDFLAGS)
PTH_CCRULE=$(MT_CC) $(PTH_CFLAGS) -o $@ -c

up.o: up.c AFS_component_version_number.c

up: up.o
	${CC} ${CFLAGS} -o up up.o ${LIBS} ${XLIBS}

fs.o: fs.c ${INCLS} AFS_component_version_number.c

fs: fs.o $(LIBS)
	${CC} ${CFLAGS} -o fs fs.o ${TOP_LIBDIR}/libprot.a $(LIBS) ${XLIBS} ${KRB5_LIBS}

afsio.o: ${srcdir}/afsio.c ${AFSIO_INCLS} AFS_component_version_number.c
	${PTH_CCRULE} ${srcdir}/afsio.c
vldbint.cs.o: ../vlserver/vldbint.cs.c
	${PTH_CCRULE} ../vlserver/vldbint.cs.c
vldbint.xdr.o: ../vlserver/vldbint.xdr.c
	${PTH_CCRULE}  ../vlserver/vldbint.xdr.c
afscbint.ss.o: ../fsint/afscbint.ss.c
	${PTH_CCRULE} ../fsint/afscbint.ss.c

afsio: afsio.o vldbint.cs.o afscbint.ss.o vldbint.xdr.o ${AFSIO_LIBS}
	$(MT_CC) @KRB5_CPPFLAGS@ $(PTH_LDFLAGS) $(PTH_CFLAGS) -o afsio afsio.o vldbint.cs.o afscbint.ss.o vldbint.xdr.o ${AFSIO_LIBS} \
		${MT_LIBS} ${XLIBS} ${KRB5LIBS}

livesys.o: livesys.c ${INCLS} AFS_component_version_number.c

livesys: livesys.c $(LIBS)
	${CC} ${CFLAGS} -o livesys ${srcdir}/livesys.c $(LIBS) ${XLIBS} ${KRB5_LIBS}

twiddle: twiddle.c $(LIBS)
	${CC} ${CFLAGS} -o twiddle ${srcdir}/twiddle.c $(LIBS) ${XLIBS} ${KRB5_LIBS}

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

whatfid.o: whatfid.c ${INCLS} AFS_component_version_number.c

whatfid: whatfid.o ${LIBS}
	${CC} ${CFLAGS} -o whatfid whatfid.o ${LIBS} ${XLIBS} ${KRB5_LIBS}

fstrace.o: fstrace.c AFS_component_version_number.c
	case ${SYS_NAME} in \
		sun4_411 | sun4c_411 | sun4m_412 ) \
			${CCXPG2} ${DBG} ${OPTMZ} -I${TOP_OBJDIR}/src/config -I${TOP_INCDIR} -I${TOP_INCDIR} -c ${srcdir}/fstrace.c ;; \
		sun*_4* ) \
			${CC} ${DBG} ${OPTMZ} -I/usr/xpg2include -I/usr/5include -g -I${TOP_SRCDIR}/config -I${TOP_INCDIR} -I${TOP_INCDIR} -c fstrace.c ;; \
		* ) \
			${CC} ${CFLAGS} -I${TOP_OBJDIR}/src/config -I${TOP_INCDIR} -I${TOP_INCDIR} -c ${srcdir}/fstrace.c ;; \
	esac

fstrace: fstrace.o
	case ${SYS_NAME} in \
		sun4_411 | sun4c_411 | sun4m_412 ) \
			${CCXPG2} ${CFLAGS} -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ;; \
		sun*_4* ) \
			${CC} -L/usr/xpg2lib -L/usr/5lib ${CFLAGS} -g -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a -lxpg ;; \
		hp700_ux100 | hp800_ux100 | hp?00_ux10? | hp_ux10? | hp_ux11?) \
			${CC} -I${TOP_OBJDIR}/src/config  -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ;; \
		* ) \
			${CC} ${CFLAGS} -o fstrace fstrace.o ${TOP_LIBDIR}/libsys.a ${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ${XLIBS} ;; \
	esac

cmdebug.o: cmdebug.c ${INCLS} AFS_component_version_number.c

cmdebug: cmdebug.o ${CMLIBS}
	$(CC) -o cmdebug cmdebug.o ${CFLAGS} ${CMLIBS} ${XLIBS} ${KRB5_LIBS}

dedebug.o: dedebug.c ${INCLS} AFS_component_version_number.c

dedebug: dedebug.o ${CMLIBS}
	$(CC) -o dedebug dedebug.o ${CFLAGS} ${CMLIBS} ${XLIBS} ${KRB5_LIBS}



#
# Kernel info dumper - these are done with submakes so that
# the build process does not attempt to rebuild them every time it runs.
#

#
# Branching target, run the actual build depending on sysname
#
kdump-build: kdump.c ${INCLS} AFS_component_version_number.c
	$(MAKE) kdump kdump64
	touch kdump-build

#
# Build targets - one for each type of kdump build process we have
#
kdump: kdump.o
	-set -x; \
	case ${SYS_NAME} in \
	sun4c_51 | sun4c_52 | sun4m_51 | sun4m_52 | sun4c_53 | sun4m_53 | sun4_53 | sun4_54 | sun4c_54 | sun4m_54 | sunx86_5? ) \
		${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a /usr/lib/libkvm.a -lelf ${XLIBS} ;; \
	sun*_5? | sun*_5?? ) \
		${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a  ${XLIBELFA} ${XLIBKVM} ${XLIBS} ;; \
	sgi_6? ) \
		for f in ../libafs/STATIC.IP*/CPU_KDEFS; \
		do	IP=`expr "$$f" : '../libafs/STATIC.\(IP..\)'`; \
			CPU_KDEFS=`sed 's/-mips.//' $$f`; \
			echo IP = $$IP; \
			echo CPU_KDEFS = $$CPU_KDEFS; \
			case $$CPU_KDEFS in \
			*-64*)	${CC} ${XCFLAGS64} \
					$$CPU_KDEFS \
					-o kdump.$$IP kdump.$$IP.o \
					${TOP_LIBDIR}/libcmd64.a -lelf \
				;; \
			*) 	${CC} ${XCFLAGS} ${ARCHFLAGS} \
					$$CPU_KDEFS \
					-o kdump.$$IP kdump.$$IP.o \
					${TOP_LIBDIR}/libcmd.a -lelf \
				;; \
			esac || exit $$? ; \
		done ;; \
	*linux26* | *linux_26* ) \
		touch kdump ;; \
	*alpha_linux* ) \
		$(MAKE) kdump-alpha_linux-@LINUX_VERSION@ ;; \
	*linux* ) \
		$(MAKE) kdump-linux-@LINUX_VERSION@ ;; \
	alpha_osf1 | alpha_osf20 |  alpha_osf30 | alpha_osf32 | alpha_osf32c | alpha_dux??) \
		${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a ${XLIBS} -ll -lmld;; \
	ncrx86_* ) ${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a -lelf ${XLIBS} ;; \
	*bsd*) touch kdump ;; \
	* )     ${CC} -o kdump kdump.o ${TOP_LIBDIR}/libcmd.a  ${TOP_LIBDIR}/util.a ${XLIBS} ;; \
	esac

kdump64: 
	-set -x; \
	case ${SYS_NAME} in \
	sun4x_5[789] | sun4x_510 | hp_ux11* | sunx86_5[789] | sunx86_510 )  \
		$(MAKE) kdump64.o ; \
		${CC} ${XCFLAGS64} -o kdump64 kdump64.o ${TOP_LIBDIR}/libcmd64.a ${XLIBELFA} ${XLIBKVM} ${XLIBS} ;; \
	esac


kdump.o: kdump.c ${INCLS} AFS_component_version_number.c
	-set -x; \
	case ${SYS_NAME} in \
		*linux26* | *linux_26* ) \
			touch kdump.o ;; \
		alpha_linux* ) \
			$(MAKE) kdump-alpha_linux-${LINUX_VERSION}.o ;; \
		*linux* ) \
			$(MAKE) kdump-linux-${LINUX_VERSION}.o ;; \
		alpha_osf1 | alpha_osf20 |  alpha_osf30 | alpha_osf32 | alpha_osf32c| alpha_dux?? ) \
			${CC} ${CFLAGS} -I/usr/sys/include -I/usr/sys/BINARY -I/usr/sys/AFS -DDEBUGGER -c ${srcdir}/kdump.c ;;\
		sgi_6? ) \
			for f in ../libafs/STATIC.IP*/CPU_KDEFS; \
			do	IP=`expr "$$f" : '../libafs/STATIC.\(IP..\)'`; \
				CPU_KDEFS=`sed 's/-mips.//' $$f`; \
				echo IP = $$IP; \
				echo CPU_KDEFS = $$CPU_KDEFS; \
				case $$CPU_KDEFS in \
				*-64*)	${CC} -D_KMEMUSER -woff 1178 \
						${KERN_DBG} ${KERN_OPTMZ} -I${TOP_INCDIR} \
						-I${TOP_OBJDIR}/src/config \
						$$CPU_KDEFS \
						${XCFLAGS64} \
						-c ${srcdir}/kdump.c -o kdump.$$IP.o \
					;; \
				*)	${CC} -D_KMEMUSER -woff 1178 \
						${KERN_DBG} ${KERN_OPTMZ} -I${TOP_INCDIR} \
						-I${TOP_OBJDIR}/src/config \
						$$CPU_KDEFS \
						${XCFLAGS} ${ARCHFLAGS} -DAFS_32BIT_KERNEL_ENV \
						-c ${srcdir}/kdump.c -o kdump.$$IP.o \
					;; \
				esac || exit $$?; \
			done \
			;; \
		*bsd*) \
			touch kdump.o ;; \
		*) \
			${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${TOP_SRCDIR} -I${TOP_INCDIR}/afs \
			-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src -I${TOP_INCDIR} ${XCFLAGS} ${ARCHFLAGS} \
			-o kdump.o -c ${srcdir}/kdump.c ;; \
	esac ;

kdump64.o : kdump.c ${INCLS} AFS_component_version_number.c
	-set -x; \
	case ${SYS_NAME} in \
	sun4x_5[789] | sun4x_510 | hp_ux11* | sunx86_5[789] | sunx86_510 ) \
		${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${TOP_SRCDIR} -I${TOP_INCDIR}/afs \
		-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src -I${TOP_INCDIR} ${XCFLAGS64} \
		-o kdump64.o -c ${srcdir}/kdump.c ;; \
	esac

# *linux* - Builds kdump-X.Y.Z according to kernel version
kdump-linux-@LINUX_VERSION@.o: kdump.c ${INCLS} AFS_component_version_number.c
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${LINUX_KERNEL_PATH}/include -I${TOP_INCDIR}/afs \
		-I${TOP_OBJDIR}/src \
		-I${TOP_OBJDIR}/src/afs/${MKAFS_OSTYPE} \
		-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src/libafs/afs \
		-I${TOP_SRCDIR} -I${TOP_SRCDIR}/afs/${MKAFS_OSTYPE} \
		-I${TOP_INCDIR} ${XCFLAGS} ${ARCHFLAGS} -o kdump-linux-${LINUX_VERSION}.o \
		-c ${srcdir}/kdump.c

kdump-linux-@LINUX_VERSION@: kdump-linux-@LINUX_VERSION@.o
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -o kdump-linux-${LINUX_VERSION} kdump-linux-${LINUX_VERSION}.o \
		${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ${XLIBS}

# *alpha_linux* - Builds kdump-X.Y.Z according to kernel version
kdump-alpha_linux-@LINUX_VERSION@.o: kdump.c ${INCLS} AFS_component_version_number.c
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -I${LINUX_KERNEL_PATH}/include -I${TOP_INCDIR}/afs \
		-I${TOP_OBJDIR}/src \
		-I${TOP_OBJDIR}/src/afs/${MKAFS_OSTYPE} \
		-I${TOP_OBJDIR}/src/config -I${TOP_OBJDIR}/src/libafs/afs \
		-I${TOP_SRCDIR} -I${TOP_SRCDIR}/afs/${MKAFS_OSTYPE} \
		-I${TOP_INCDIR} ${XCFLAGS} ${ARCHFLAGS} -mno-fp-regs -ffixed-8 \
		-o kdump-alpha_linux-${LINUX_VERSION}.o -c ${srcdir}/kdump.c ;;

kdump-alpha_linux-@LINUX_VERSION@: kdump-alpha_linux-@LINUX_VERSION@.o
	${CC} ${KERN_DBG} ${KERN_OPTMZ} -o kdump-alpha_linux-${LINUX_VERSION} kdump-alpha_linux-${LINUX_VERSION}.o \
		${TOP_LIBDIR}/libcmd.a ${TOP_LIBDIR}/util.a ${XLIBS}

#
# Install targets
#
install: kdump-build fs livesys up fstrace cmdebug afsio
	${INSTALL} -d ${DESTDIR}${bindir}
	${INSTALL} -d ${DESTDIR}${afssrvbindir}
	${INSTALL} -d ${DESTDIR}${sbindir}
	${INSTALL_PROGRAM} fs ${DESTDIR}${bindir}/fs
	${INSTALL_PROGRAM} livesys ${DESTDIR}${bindir}/livesys
	${INSTALL_PROGRAM} fs ${DESTDIR}${afssrvbindir}/fs
	${INSTALL_PROGRAM} up ${DESTDIR}${bindir}/up
	${INSTALL_PROGRAM} fstrace ${DESTDIR}${sbindir}/fstrace
	${INSTALL_PROGRAM} cmdebug ${DESTDIR}${bindir}/cmdebug
	${INSTALL_PROGRAM} afsio ${DESTDIR}${bindir}/afsio ;
	-set -x; \
	case ${SYS_NAME} in \
	sgi_6? ) \
		${INSTALL_SCRIPT} kdump.sh.sgi_ipnos ${DESTDIR}${sbindir}/kdump; \
		ln -fs kdump ${DESTDIR}${sbindir}/kdump32; \
		ln -fs kdump.IP20 ${DESTDIR}${sbindir}/kdump.IP22; \
		ln -fs kdump.IP20 ${DESTDIR}${sbindir}/kdump.IP32; \
		for f in kdump.IP??; \
			do ${INSTALL_PROGRAM} $$f ${DESTDIR}${sbindir}/$$f || exit $$? ; \
		done ;; \
	sun*_5[789] | sun*_510 ) \
		${INSTALL_SCRIPT} kdump.sh.solaris7 ${DESTDIR}${sbindir}/kdump32; \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump;; \
	*linux26* | *linux_26* ) \
		;; \
	*linux* ) \
		${INSTALL_SCRIPT} kdump.sh.linux ${DESTDIR}${sbindir}/kdump; \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump ;; \
	hp_ux11* ) \
		${INSTALL_SCRIPT} kdump.sh.hp_ux11 ${DESTDIR}${sbindir}/kdump; \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump ;; \
	*_fbsd* ) \
		;; \
	*) \
		${INSTALL_PROGRAM} kdump-build ${DESTDIR}${sbindir}/kdump ;; \
	esac
	if [ -f kdump64 ]; then \
		${INSTALL_PROGRAM} kdump64 ${DESTDIR}${sbindir}/kdump; \
	fi

dest: kdump-build fs livesys up fstrace cmdebug afsio
	${INSTALL} -d ${DEST}/bin
	${INSTALL} -d ${DEST}/etc
	${INSTALL} -d ${DEST}/root.server/usr/afs/bin
	${INSTALL_PROGRAM} fs ${DEST}/bin/fs
	${INSTALL_PROGRAM} livesys ${DEST}/bin/livesys
	${INSTALL_PROGRAM} fs ${DEST}/root.server/usr/afs/bin/fs
	${INSTALL_PROGRAM} up ${DEST}/bin/up
	${INSTALL_PROGRAM} fstrace ${DEST}/etc/fstrace
	${INSTALL_PROGRAM} cmdebug ${DEST}/bin/cmdebug
	${INSTALL_PROGRAM} afsio ${DEST}/bin/afsio
	-set -x; \
	case ${SYS_NAME} in \
	sgi_6? ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.sgi_ipnos ${DEST}/etc/kdump; \
		ln -fs kdump ${DEST}/etc/kdump32; \
		ln -fs kdump.IP20 ${DEST}/etc/kdump.IP22; \
		ln -fs kdump.IP20 ${DEST}/etc/kdump.IP32; \
		for f in kdump.IP??; \
			do ${INSTALL_PROGRAM} $$f ${DEST}/etc/$$f || exit $$? ; \
		done ;; \
	sun*_5[789] | sun*_510 ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.solaris7 ${DEST}/etc/kdump; \
		${INSTALL_PROGRAM} ${srcdir}/kdump ${DEST}/etc/kdump32;; \
	*linux26* | *linux_26* ) \
		;; \
	*alpha_linux* ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.linux ${DEST}/etc/kdump; \
		${INSTALL_PROGRAM} kdump-alpha_linux-${LINUX_VERSION} ${DEST}/etc/kdump-${LINUX_VERSION} ;; \
	*linux* ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.linux ${DEST}/etc/kdump; \
		${INSTALL_PROGRAM} kdump-linux-${LINUX_VERSION} ${DEST}/etc/kdump-${LINUX_VERSION} ;; \
	hp_ux11* ) \
		${INSTALL_SCRIPT} ${srcdir}/kdump.sh.hp_ux11 ${DEST}/etc/kdump32; \
		${INSTALL_PROGRAM} kdump-build ${DEST}/etc/kdump ;; \
	*bsd*) \
		;; \
	*) \
		${INSTALL_PROGRAM} kdump-build ${DEST}/etc/kdump ;; \
	esac
	if [ -f kdump64 ]; then \
		${INSTALL_PROGRAM} kdump64 ${DEST}/etc/kdump; \
	fi

#
# Misc targets
#

clean:
	$(RM) -f *.o *.a up fs kdump-* kdump kdump64 core cmdebug AFS_component_version_number.c fstrace gcpags livesys dedebug afsio

test:
	cd test; $(MAKE)

include ../config/Makefile.version
