SHELL = /bin/sh
.SUFFIXES:
.SUFFIXES: .c .o

CC=gcc

#stuff related to the executables (suck rpost testhost)
INSTALLDIR=$(DESTDIR)/usr/bin	# location for the executables
INSTALL_OWNER=root		# user of the installed files
INSTALL_GROUP=root		# group for the installed files
INSTALL_MODE=755		# mode for the installed files

#stuff related to the man pages 
MANDIR=$(DESTDIR)/usr/man/man1	# location for the man pages
MAN_OWNER=root			# owner of the man pages
MAN_GROUP=root			# group for the man pages
MAN_MODE=644			# mode for the man pages

#XXGDB=1			# uncomment this to get it ready for use by xxgdb or gdb
#GPROF=1			# uncomment this to use with gprof

#IF you use a db history file, uncomment ONE of the following three entries
# If you just use a flat file leave these alone
#USE_DBM=1
#USE_NDBM=1
#USE_DBZ=1
#USE_INNDBZ=1	# if your DBZ routines are part of libinn.a, also check INN_ stuff

# If the INNDBZ stuff is not in the following locations, change this
INN_DBINC=-I/usr/src/inn/include
INN_LIBDIR=-L/usr/src/inn/lib
INN_DBLIBS=-linn

#---------------DON'T TOUCH BELOW HERE--------------------------------------
VERSION_MAJOR=2
VERSION_MINOR=6
VERSION_PATCH=3
VERSION=$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
DVERSION=-DSUCKVER=$(VERSION_MAJOR) -DSUCKVER_MINOR=$(VERSION_MINOR) -DSUCKVER_PATCH=$(VERSION_PATCH)

ifdef GPROF
XXGDB=1		# required for gprof output
GPFLAG=-pg
GPROFLIB=-lgmon
else
GPFLAG=
GPROFLIB=
endif

ifdef XXGDB
CFLAGS=-g $(GPFLAG)
LDFLAGS=-g $(GPFLAG)
else
CFLAGS=-O2 $(GPFLAG)
LDFLAGS=-s $(GPFLAG)
endif

DBOBJ = chkhistory.o		# flat file history database routines

ifdef USE_DBM
DBINC=
DBLIB = -ldbm
DBOPTION = -DUSE_DBM
DBOBJ = chkhistory_db.o
endif
ifdef USE_NDBM
DBINC=
DBLIB = -lndbm
DBOPTION = -DUSE_NDBM
DBOBJ = chkhistory_db.o
endif
ifdef USE_DBZ
DBINC=
DBLIB = -ldbz
DBOPTION = -DUSE_DBZ
DBOBJ = chkhistory_db.o
endif
ifdef USE_INNDBZ
DBINC = $(INN_DBINC)
DBLIB = $(INN_DBLIBS) $(INN_LIBDIR)
DBOPTION = -DUSE_DBZ
DBOBJ = chkhistory_db.o
endif

LINK = $(CC)
LIBS = -lm $(DBLIB) $(GPROFLIB)
OPTS = $(CFLAGS) $(GCC_FLAGS) $(DVERSION) $(DBOPTION) $(DBINC)
INSTALL = install

MAN = ./man
SAMPLE = ./sample

TARGETS = suck rpost testhost
MANPGS = $(MAN)/suck.1 $(MAN)/rpost.1 $(MAN)/testhost.1

LPOST_TARGET = lpost
LPOST_MANPGS = $(MAN)/lpost.1

SUCKOBJS=suck.o suckutils.o both.o killfile.o timer.o $(DBOBJ)

GCC_FLAGS =  -fno-strength-reduce -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wtraditional -Wshadow\
  -Waggregate-return -Wmissing-prototypes -Wnested-externs -Wstrict-prototypes
# -Wconversion


all: $(TARGETS)
	@echo make done.

suck: $(SUCKOBJS)
	$(LINK) $(LDFLAGS) -o $@ $(SUCKOBJS) $(LIBS)

lpost: lpost.o
	$(LINK) $(LDFLAGS) -o $@ lpost.o $(LIBS)

rpost: rpost.o both.o
	$(LINK) $(LDFLAGS) -o $@ rpost.o both.o $(LIBS)

testhost: testhost.o both.o
	$(LINK) $(LDFLAGS) -o $@ testhost.o both.o $(LIBS)

install: installbin installman

installbin: $(TARGETS)
	mkdir -p $(INSTALLDIR)	
	$(INSTALL) -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m $(INSTALL_MODE) $(TARGETS) $(INSTALLDIR)

installman: $(MANPGS)
	- $(INSTALL) -o $(MAN_OWNER) -g $(MAN_GROUP) -m $(MAN_MODE) $(MANPGS) $(MANDIR)

installall: install lpost
	$(INSTALL) -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m $(INSTALL_MODE) $(LPOST_TARGET) $(INSTALLDIR)
	- $(INSTALL) -o $(MAN_OWNER) -g $(MAN_GROUP) -m $(MAN_MODE) $(LPOST_MANPGS) $(MANDIR)
	
clean:
	rm -f `find . \( -name "*.o" -o -name "*[~%]" \) -print`

cleanrun: 
	rm -rf Msgs
	rm -f suck.errlog suck.status suck.newrc suck.index suck.sorted suck.restart suck.lock suck.killlog debug.* gmon.out

distclean: clean cleanrun
	rm -f suck rpost lpost testhost sucknewsrc suckkillfile suckothermsgs batch

maketar: distclean
	tar -czv -f ../suck-${VERSION}.tar.gz -C .. suck-${VERSION}
		
# One of these days I have to get this to work without any error msgs
#depend:		
#	makedepend $(SRC)

.c.o: $(SUCKOBJS) config.h Makefile
	$(CC) $(OPTS) -c $<
