# 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@
HELPER_SPLINT=@HELPER_SPLINT@
ENABLE_PTHREADED_UBIK=@ENABLE_PTHREADED_UBIK@

CCRULE=${MT_CC} ${CFLAGS} ${MT_CFLAGS} -c

RX=../rx
UTIL=../util
PTSERVER=../ptserver

RXOBJS= rx_pthread.o rxkad_errs.o

UTILOBJS=assert.o uuid.o serverLog.o fileutil.o netutils.o dirpath.o \
     volparse.o flipbase64.o softsig.o hostparse.o snprintf.o pthread_glock.o

INCLS=${TOP_INCDIR}/ubik.h \
	${TOP_INCDIR}/lock.h  \
	${TOP_INCDIR}/lwp.h \
	${TOP_INCDIR}/rx/rx.h \
	${TOP_INCDIR}/rx/xdr.h \
	${TOP_INCDIR}/afs/keys.h \
	${TOP_INCDIR}/afs/cellconfig.h \
	ptint.h \
	pterror.h

LIBS=${RXOBJS} ${UTILOBJS}\
	${TOP_LIBDIR}/libubik_pthread.a \
	${TOP_LIBDIR}/libafsauthent.a \
	${TOP_LIBDIR}/libafsrpc.a \
	${TOP_LIBDIR}/libcmd.a \
	${TOP_LIBDIR}/libafsutil.a 

PTOBJS=ptuser.o pterror.o ptint.cs.o ptint.xdr.o

all: ptserver pts pt_util db_verify \
	depinstall readgroup readpwd testpt

depinstall: 

#
# Build targets
#

rx_pthread.o: ${RX}/rx_pthread.c
	${CCRULE} ${srcdir}/${RX}/rx_pthread.c

rxkad_errs.o: ../rxkad/rxkad_errs.c
	${CCRULE} ../rxkad/rxkad_errs.c

#
# get the pthreaded util stuff compiled here.  we do this instead of
# using the non-pthreaded libutil.a.  There probably should be a
# pthreaded version of this library, as we are doing with ubik itself, but...
#

assert.o: ${UTIL}/assert.c
	${CCRULE} ${srcdir}/${UTIL}/assert.c

uuid.o: ${UTIL}/uuid.c
	${CCRULE} ${srcdir}/${UTIL}/uuid.c

serverLog.o: ${UTIL}/serverLog.c
	${CCRULE} ${srcdir}/${UTIL}/serverLog.c

fileutil.o: ${UTIL}/fileutil.c
	${CCRULE} ${srcdir}/${UTIL}/fileutil.c

volparse.o: ${UTIL}/volparse.c
	${CCRULE} ${srcdir}/${UTIL}/volparse.c

flipbase64.o: ${UTIL}/flipbase64.c
	${CCRULE} ${srcdir}/${UTIL}/flipbase64.c

netutils.o: ${UTIL}/netutils.c
	${CCRULE} ${srcdir}/${UTIL}/netutils.c

dirpath.o: ${UTIL}/dirpath.c
	${CCRULE} ${srcdir}/${UTIL}/dirpath.c

softsig.o: ${UTIL}/softsig.c
	${CCRULE} ${srcdir}/${UTIL}/softsig.c

hostparse.o: ${UTIL}/hostparse.c
	${CCRULE} ${srcdir}/${UTIL}/hostparse.c

snprintf.o: ${UTIL}/snprintf.c
	${CCRULE} ${srcdir}/${UTIL}/snprintf.c

pthread_glock.o: ${UTIL}/pthread_glock.c
	${CCRULE} ${srcdir}/${UTIL}/pthread_glock.c


ptserver.o: ${PTSERVER}/ptserver.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/ptserver.c

# When supergroups is enabled, some parts of ptserver cast between
# # incompatible structs.
ptutils.o: ${PTSERVER}/ptutils.c ${INCLS}
	${CCRULE} @CFLAGS_NOSTRICT@ ${srcdir}/${PTSERVER}/ptutils.c

ptprocs.o: ${PTSERVER}/ptprocs.c ${INCLS}
	${CCRULE} @CFLAGS_NOSTRICT@ ${srcdir}/${PTSERVER}/ptprocs.c

utils.o: ${PTSERVER}/utils.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/utils.c

map.o: ${PTSERVER}/map.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/map.c

ptint.ss.o: ptint.ss.c
	${CCRULE} ptint.ss.c

ptint.cs.o: ptint.cs.c
	${CCRULE} ptint.cs.c

ptint.xdr.o: ptint.xdr.c
	${CCRULE} ptint.xdr.c

ptint.cs.c: ${PTSERVER}/ptint.xg
	${RXGEN} -A -x -C -u -o $@ ${srcdir}/${PTSERVER}/ptint.xg

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

ptint.xdr.c: ${PTSERVER}/ptint.xg
	${RXGEN} -A -x -c -o $@ ${srcdir}/${PTSERVER}/ptint.xg

ptint.h: ${PTSERVER}/ptint.xg
	${RXGEN} -A -x -h -u -o $@ ${srcdir}/${PTSERVER}/ptint.xg

ptint.cs.c: ptint.h
ptint.ss.c: ptint.h
ptint.xdr.c: ptint.h

Kptint.cs.c: ${PTSERVER}/ptint.xg Kptint.h
	${RXGEN} -A -x -k -C -o Kptint.cs.c ${srcdir}/${PTSERVER}/ptint.xg
 
Kptint.xdr.c: ${PTSERVER}/ptint.xg
	${RXGEN} -A -x -k -c -o Kptint.xdr.c ${srcdir}/${PTSERVER}/ptint.xg
 
Kptint.h: ${PTSERVER}/ptint.xg
	${RXGEN} -A -x -k -h -o Kptint.h ${srcdir}/${PTSERVER}/ptint.xg

display.o: ${PTSERVER}/display.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/display.c

ptserver: ptserver.o ptutils.o ptprocs.o ptint.ss.o ptint.xdr.o utils.o $(LIBS) ${TOP_LIBDIR}/libaudit.a map.o
	${CC} ${LDFLAGS} -o ptserver ptserver.o ptutils.o ptprocs.o ptint.ss.o ptint.xdr.o utils.o map.o $(LIBS) ${MT_LIBS} ${XLIBS} ${TOP_LIBDIR}/libaudit.a

db_verify.o: ${PTSERVER}/db_verify.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/db_verify.c

db_verify: db_verify.o pterror.o display.o $(LIBS)
	$(CC) ${LDFLAGS} -o db_verify db_verify.o display.o pterror.o $(LIBS) ${MT_LIBS} ${XLIBS}

ptclient: ptclient.o display.o ptuser.o pterror.o ptint.cs.o ptint.xdr.o AFS_component_version_number.o $(LIBS)
	$(CC) ${LDFLAGS} -o ptclient ptclient.o display.o $(PTOBJS) $(LIBS) ${MT_LIBS} ${XLIBS}

ptclient.o: ${PTSERVER}/ptclient.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/ptclient.c

ptuser.o: ${PTSERVER}/ptuser.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/ptuser.c

pterror.h pterror.c: ${PTSERVER}/pterror.et
	$(RM) -f pterror.h pterror.c
	${COMPILE_ET} -p ${srcdir}/${PTSERVER} pterror

pts: pts.o $(PTOBJS) ${TOP_LIBDIR}/libcmd.a $(LIBS) ${INCLS}
	$(CC) ${LDFLAGS} -o pts pts.o ${TOP_LIBDIR}/libcmd.a $(PTOBJS) ${LIBS} ${MT_LIBS} ${XLIBS}

pts.o: ${PTSERVER}/pts.c
	${CCRULE} ${srcdir}/${PTSERVER}/pts.c

readgroup: readgroup.o $(PTOBJS) $(LIBS)
	$(CC) ${CFLAGS} -o readgroup readgroup.o $(PTOBJS) ${LIBS} ${MT_LIBS} ${XLIBS}

readgroup.o: ${PTSERVER}/readgroup.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/readgroup.c

readpwd: readpwd.o $(PTOBJS) $(LIBS)
	$(CC) ${CFLAGS} -o readpwd readpwd.o $(PTOBJS) ${LIBS} ${MT_LIBS} ${XLIBS}

readpwd.o: ${PTSERVER}/readpwd.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/readpwd.c

testpt: testpt.o $(PTOBJS) ${TOP_LIBDIR}/libcmd.a $(LIBS)
	$(CC) ${CFLAGS} -o testpt testpt.o -lm ${TOP_LIBDIR}/libcmd.a $(PTOBJS) $(LIBS) ${MT_LIBS} ${XLIBS}

testpt.o: ${PTSERVER}/testpt.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/testpt.c

pt_util: pt_util.o ptutils.o ubik.o utils.o map.o $(PTOBJS) $(LIBS)
	$(CC) ${CFLAGS} -o pt_util pt_util.o ptutils.o ubik.o utils.o map.o $(PTOBJS) ${TOP_LIBDIR}/libcmd.a $(LIBS) ${MT_LIBS} ${XLIBS}

pt_util.o: ${PTSERVER}/pt_util.c
	${CCRULE} ${srcdir}/${PTSERVER}/pt_util.c

ubik.o: ${PTSERVER}/ubik.c ${INCLS}
	${CCRULE} ${srcdir}/${PTSERVER}/ubik.c

prerror.h: pterror.h
	${INSTALL} $? $@
	echo '#define ERROR_TABLE_BASE_pr ERROR_TABLE_BASE_PT' >> $@

#
# Installation targets
#
install: ptserver pts pt_util db_verify
	if [ "x$(ENABLE_PTHREADED_UBIK)" = "xyes" ] ; then \
		${INSTALL_PROGRAM} ptserver ${DESTDIR}${afssrvlibexecdir}/ptserver; \
		${INSTALL_PROGRAM} pts ${DESTDIR}${afssrvbindir}/pts; \
		${INSTALL_PROGRAM} pts ${DESTDIR}${bindir}/pts; \
		${INSTALL_PROGRAM} pt_util ${DESTDIR}${afssrvsbindir}/pt_util; \
		${INSTALL_PROGRAM} db_verify ${DESTDIR}${afssrvsbindir}/prdb_check; \
	fi

dest: ptserver pts pt_util db_verify
	if [ "x$(ENABLE_PTHREADED_UBIK)" = "xyes" ] ; then \
		${INSTALL_PROGRAM} ptserver ${DEST}/root.server/usr/afs/bin/ptserver; \
		${INSTALL_PROGRAM} pts ${DEST}/root.server/usr/afs/bin/pts; \
		${INSTALL_PROGRAM} pts ${DEST}/bin/pts; \
		${INSTALL_PROGRAM} pt_util ${DEST}/root.server/usr/afs/bin/pt_util; \
		${INSTALL_PROGRAM} db_verify ${DEST}/etc/prdb_check; \
	fi

#
# Misc. targets
#
clean:
	$(RM) -f *.a *.o ptserver ptint.cs.c ptint.ss.c ptclient ptint.xdr.c ptint.h \
	pts readgroup readpwd db_verify testpt pt_util pterror.h pterror.c \
	core AFS_component_version_number.c Kptint.cs.c Kptint.h Kptint.xdr.c

include ../config/Makefile.version
