#
# Makefile for customs agent and its clients.
#
# $Id: Makefile,v 1.33 1992/10/05 04:43:11 stolcke Exp $ SPRITE (Berkeley)
#
# If your machine is on more than 10 networks, on all of which you want to
# run the customs service, you should redefine MAXNETS in the CFLAGS variable.
# The multi-network code has never actually been tested, however.
#
# Copyright (c) 1988, 1989 by the Regents of the University of California
# Copyright (c) 1988, 1989 by Adam de Boor
# Copyright (c) 1989 by Berkeley Softworks
#
# Permission to use, copy, modify, and distribute this
# software and its documentation for any non-commercial purpose
# and without fee is hereby granted, provided that the above copyright
# notice appears in all copies.  The University of California,
# Berkeley Softworks and Adam de Boor make no representations about
# the suitability of this software for any purpose.  It is provided
# "as is" without express or implied warranty.
#

#
# Owner of the various binaries
#
OWNER		= $$USER
GROUP		= system

#
# BINDIR is where the various user-accessible clients go.
# ETCDIR is where the system-control clients and the daemon itself go.
#
DESTDIR		=
BINDIR		= $(DESTDIR)/usr/local/bin
ETCDIR		= $(DESTDIR)/usr/local/etc
#
# Install flags for user clients that can do no harm.
#
BINMODE		= -m 755 -o $(OWNER) -g $(GROUP) -s
#
# Install flags for exporting clients.  If USE_RESERVED_PORTS was enabled in
# config.h, these need to be suid root.
#
XBINMODE	= -m 4755 -o root -g $(GROUP) -s
#
# Install flags the deamon and for control programs in $(ETCDIR).
# The latter can be usefully run by normal users, but critical operations
# will only be allowed if run as root.  Therefore they should probably NOT
# be suid root.
#
ETCMODE		= -m 755 -o root -g $(GROUP) -s
#
# Shell commands run after binary installation is complete.  May be used
# to fix up anything that needs special handling.
#
POSTINSTALL	= true

#
# Include PMake common description
#
DEPFILE		?= dependencies.mk
#include	"../common.mk"

#
# Local changes to installation, etc.
#
#if exists(../config.mk)
#include	"../config.mk"
#endif

#
# Additional path definitions
#
.PATH.h		: $(INCLUDE)
.PATH.a		: $(LSTDIR) $(SPRITEDIR) 
.PATH.c		: test

CC		?= cc
MACHFLAGS	?=
OPTIMIZE	?= -g -O

CFLAGS		+= $(OPTIMIZE) $(MACHFLAGS) -I.

#ifdef SYSV
CFLAGS		+= -DSYSV
#endif

#
# If there's no /dev/kbd from which Customs can derive the user's idle time
# (this is intended for workstations only), don't do idle time checking.
#
#CFLAGS		+= -DNO_IDLE

#
# If making a profiled version, give the proper flags and configure pieces of
# code properly. Note this isn't done on a sun because the last time I profiled
# pmake on a sun, it died in the profiling code...
#
#if make(prof) && !defined(sun)
CFLAGS		+= -pg -DPROF
#endif

#
# Objects that make up the Customs daemon itself. OSOBJ is the object file
# that contains the OS functions to determine the state of the machine.
#	os-sunos4 should be used for suns running SunOS4.0 or higher
#	os-bsd can be used for all unixes that are based on the Berkeley
#	    Software Distribution.
# I have no System V machine around, so I can't write the interface for it.
#
OSOBJ		= os-bsd.o
EXTRAOBJS	= xmalloc.o xlog.o
CUSTOMSOBJS 	= customs.o mca.o avail.o import.o election.o rpc.o xdr.o \
		  log.o swap.o $(OSOBJ) $(EXTRAOBJS)

#
# System dependencies
#
#ifdef sun
# if exists(/usr/lib/libkvm.a)
OSOBJ		:= os-sunos4.o -lkvm
# endif
STATICFLAG	?= -Bstatic
PUREFLAG	?= -n -Bdynamic
#elifdef ultrix
CUSTOMSOBJS	+= getusershell.o
#elifdef NeXT
CFLAGS		+= -bsd -DSIGRET=int
#elifdef hpux
CFLAGS		+= -D_BSD # needed to get <sys/wait.h> to work
CFLAGS		+= -I/usr/include -I/etc/conf/h # so we can use -D_KERNEL
CUSTOMSOBJS	+= gethostid.o
EXTRAOBJS	+= random.o
#elifdef SYSV
# generic System V
CUSTOMSOBJS	+= getusershell.o
EXTRAOBJS	+= random.o
#else
# generic BSD
#endif

#
# Files required by all clients to talk to the Customs agent
#
RPCOBJS		= customslib.o rpc.o $(EXTRAOBJS)

#
# All clients made by this makefile
#
USERCLIENTS	= reginfo
XUSERCLIENTS	= export pmake
SYSCLIENTS	= importquota cctrl logd
CLIENTS		= $(USERCLIENTS) $(XUSERCLIENTS) $(SYSCLIENTS)

#
# Add the rpc objects to the list of objects, as the customs rmt module needs
# them.
#
OBJS		+= $(RPCOBJS)

#
all		: customs $(CLIENTS) 

#
# Simple installation rule -- not all systems allow one to install many files
# at once, so do it in a 'for' loop
#
install		:: installbin

installbin	:: .NOEXPORT all
#if !empty(.MAKEFLAGS:M*C*)
	-mv $(ETCDIR)/customs $(ETCDIR)/customs~
	for i in $(SYSCLIENTS) customs; do
	    $(INSTALL) $(ETCMODE) $i $(ETCDIR)/`basename $i`
	done
	-if [ ! -f $(BINDIR)/pmake.nocustoms ]; then \
	   mv $(BINDIR)/pmake $(BINDIR)/pmake.nocustoms; \
	fi
	for i in $(USERCLIENTS); do
	    $(INSTALL) $(BINMODE) $i $(BINDIR)/`basename $i`
	done
	for i in $(XUSERCLIENTS); do
	    $(INSTALL) $(XBINMODE) $i $(BINDIR)/`basename $i`
	done
	$(POSTINSTALL)
#endif

#
# Bump version number whenever any part of customs is updated.
# version.h is included in customs.c.  We cannot let version.h depend
# on customs.c because that would create a cyclic dependency.
# Don't export to get the hostname in the version string right.
#
customs.o	: customs.c $(CUSTOMSOBJS:Ncustoms.o) $(LISTLIB) $(SPRITELIB) \
		  .NOEXPORT
	sh newvers.sh
	$(CC) $(CFLAGS) -c customs.c 
	
#
# Rule to link all clients. Very simple
#
LINK		: .USE .EXPORTSAME
	$(CC) $(CFLAGS) $(LDFLAGS) $(.ALLSRC) -o $(.TARGET)
SLINK		: .USE .EXPORTSAME
	$(CC) $(CFLAGS) $(LDFLAGS) $(STATICFLAG) $(.ALLSRC) -o $(.TARGET)
PLINK		: .USE .EXPORTSAME
	$(CC) $(CFLAGS) $(LDFLAGS) $(PUREFLAG) $(.ALLSRC) -o $(.TARGET)

# link customs as pure executable so doesn't depend on server binary
customs		: $(CUSTOMSOBJS) $(LISTLIB) $(SPRITELIB)	PLINK
host		: host.o $(RPCOBJS) $(SPRITELIB)		LINK
# link export statically to speed up programs that use it in slave mode
export		: export.o $(RPCOBJS) $(SPRITELIB)		SLINK
importquota	: importquota.o $(RPCOBJS) $(SPRITELIB)		LINK
reginfo		: reginfo.o $(RPCOBJS) $(SPRITELIB)		LINK
logd		: logd.o $(RPCOBJS) xdr.o $(SPRITELIB)		LINK
prof		: pmake
pmake		: $(OBJS) $(LISTLIB) $(SPRITELIB) 		LINK
cctrl		: cctrl.o $(RPCOBJS) $(SPRITELIB)		LINK

# These were used for testing and benchmarking only.
#
#nulltest	: test/nulltest.c $(RPCOBJS)			LINK
#rpctest	: test/rpctest.c rpc.o				LINK
#rpcsrv		: test/rpcsrv.c rpc.o				LINK
#pmake.ideal	: $(OBJS:s/job/idealJob/) -llst -lwild LINKCOMPAT

clean		:: .NOEXPORT
	rm -f customs host $(CLIENTS) *.o *.bak
#
# Declare additional sources for which dependencies should be generated
#
depend		:
	touch $(DEPFILE)

depend		: $(CUSTOMSOBJS:S/.o$/.c/g) host.c export.c importquota.c \
		reginfo.c logd.c cctrl.c customslib.c

#ifdef exists($(DEPFILE))
#include "$(DEPFILE)"
#endif

#
# Some targets for testing
#
#TESTCMD	= hostname
TESTCMD		= sleep 10
TESTTARGETS = 0 1 2 3 4 5 6 7 8 9
test:	$(TESTTARGETS)
	:

$(TESTTARGETS):
	$(TESTCMD)

testrm: test.c
	touch testrm
	$(TESTCMD)
