#========================================================================
# This is a Makefile for the xmlsysd proc/stat monitor, part of the
# overall warewulf package.  It has a very abbreviated set of targets.
#
# make          alone should build the application.
#
# make clean    deletes the application and all object files
# make install  strips and installs application and a man page
# make printout prints out all source and include files
#
# These two commands execute in the toplevel project directory only.
# make svn      does a svn commit and creates the timestamp $(SVN)
# make sync     does a svn commit and rsyncs to list of hosts
#========================================================================
PROJECT = wulfware
PROGRAM = xmlsysd
DIR = $(PROGRAM)

#========================================================================
# This is essential.  The rpmbuild overrides it, but we have to make
# build "work" when executed only in the source tree directory itself.
# This isn't easy, since the dependences are more than a bit scattered.
# We therefore point to the directory one level up, where we should
# find a ./lib, ./include, ./share and ./bin directory tree from which
# the various dieharder files will actually be assembled into an rpm
# with PREFIX=/usr (for example).
#========================================================================
BUILDROOT=../buildroot
PREFIX=$(BUILDROOT)/usr
ETCDIR=$(BUILDROOT)/etc
SBINDIR=$(PREFIX)/sbin

SVNTREE = $(HOME)/Src/svn-tree
SVNPATH = $(SVNTREE)/$(PROJECT)
SVNTIME = $(DIR:=.svn.time)

# This is automagically set in the toplevel build.  Do not edit by
# hand.
VERSION_MAJOR=2
VERSION_MINOR=5.0
RELEASE=1

#========================================================================
# Define all sources.  Note that we choose to depend on ALL the includes
# in the include directory, which cannot be empty or this will barf.
# We also have dependencies that extend to the libdieharder source
# directory, as we need to force a build if any of them change.
#========================================================================
SRCSOURCES = $(shell ls *.c  2>&1 | sed -e "/\/bin\/ls:/d")
SRCINCLUDES = $(shell ls *.h  2>&1 | sed -e "/\/bin\/ls:/d")

SOURCES = $(LIBSOURCES) $(SRCSOURCES)
INCLUDES = $(LIBINCLUDES) $(SRCINCLUDES)
OBJECTS = $(SRCSOURCES:.c=.o)

DEFINES = -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) \
          -DRELEASE=$(RELEASE)

#========================================================================
# Define parameters and directives needed in compile/link steps.
#========================================================================
# C Compiler
CC = gcc

# Compile flags
CFLAGS = -O3 -I/usr/include/libxml2 $(DEFINES)

# Linker flags
LDFLAGS =

# Libraries
LIBS = -lxml2 -lz -lm

#========================================================================
# List of variants one can make.  all is the default.  We always
# presume the simplest of dependencies and remake if includes change
# for example.
#========================================================================
all: $(PROGRAM) 

$(PROGRAM): $(OBJECTS) $(INCLUDES)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(LIBS) $(OBJECTS)

#========================================================================
# The only safe place to do commits is in the toplevel directory
#========================================================================
svn:
	(cd ..;make svn)

sync:
	(cd ..;make sync)

#========================================================================
# printout makes an enscript -2r printout of SOURCES and
# and INCFILES.  Use lpr if you don't have enscript
#========================================================================
LPR = enscript -2r
# LPR = lpr
printout:
	$(LPR) $(SRCSOURCES) $(SRCINCLUDES)

#========================================================================
#  A standard cleanup target
#========================================================================
clean:
	rm -f core $(PROGRAM) $(OBJECTS) $(PROGRAM).1.gz

#========================================================================
# Application installation.
#========================================================================
install :  $(PROGRAM)
	(strip $(PROGRAM); \
	install -d $(SBINDIR); \
	install -m 755 $(PROGRAM) $(SBINDIR); \
	install -d $(ETCDIR); \
	install -d $(ETCDIR)/xinetd.d; \
	install -m 755 xmlsysd.xinetd $(ETCDIR)/xinetd.d/xmlsysd; \
	install -d $(PREFIX)/share/man/man8; \
	gzip -c $(PROGRAM).8 > $(PROGRAM).8.gz; \
	install -m 644 $(PROGRAM).8.gz $(PREFIX)/share/man/man8; \
	)

#========================================================================
# We give all generic rules below.  Currently we only need a rule for 
# objects.
#========================================================================
%.o:%.c $(INCLUDES)
	$(CC) -c $(CFLAGS) $<

