build_mode=normal

# You can call this Makefile in a number of useful ways.
# Firstly, you can specify additional include directories like so:
#
#    make INCLUDES='-I/opt/libowfat/include -I/tmp/another/include'
#
# ...secondly, you can specify lib directories in much the same 
# way:
#
#    make LIBDIRS='-L/opt/libowfat-glibc/lib'
#
# You can also specify a destination directory for packaging purposes:
#
#    make DESTDIR='/tmp/build' install
#
# Finally, you can build it in "diet" or "debug" modes
# I build with:
# make build_mode=diet INCLUDES=-I/opt/libowfat/include/ LIBDIRS=-L/opt/libowfat/lib clean all

prefix=/usr
sbindir=${prefix}/sbin
mandir=${prefix}/share/man/man8

normal_cflags=-pipe -Wall -Os -fomit-frame-pointer -DNDEBUG $(INCLUDES)
debug_cflags= -pipe -Wall -g $(INCLUDES)
diet_cflags=  -pipe -Wall -Os -fomit-frame-pointer -DNDEBUG -static $(INCLUDES)

ifeq ($(build_mode),normal)
	CFLAGS=$(normal_cflags)
	ALL=stripobjects slidentd honest_identd strip
endif
 
ifeq ($(build_mode),diet)
	CFLAGS=$(diet_cflags)
	CC:=diet -Os $(CC)
	ALL=stripobjects slidentd honest_identd strip
endif

ifeq ($(build_mode),debug)
	CFLAGS=$(debug_cflags)
	ALL=objects slidentd honest_identd
endif	

all: $(ALL)

MAINOBJECTS=slidentd.o honest_identd.o
SRC=$(wildcard *.c)
OBJECTS=$(filter-out $(MAINOBJECTS), $(SRC:.c=.o))
VERSION=1.0.0

objects: $(OBJECTS)

stripobjects: $(OBJECTS) $(MAINOBJECTS)
	strip -x -R .comment -R .note $(OBJECTS) $(MAINOBJECTS)

strip:
	strip slidentd
	strip honest_identd

slidentd: $(OBJECTS) slidentd.o
	$(CC) $(OBJECTS) -o slidentd slidentd.o $(LIBDIRS) -lowfat

honest_identd: $(OBJECTS) honest_identd.o
	$(CC) $(filter-out slid_random.o,$(OBJECTS)) -o honest_identd honest_identd.o $(LIBDIRS) -lowfat 

clean:
	rm -f slidentd honest_identd *.o core a.out *~

indent:
	./Lindent *.c *.h

tarball: indent clean
	(cd ..; tar czvf slidentd-$(VERSION).tar.gz slidentd-$(VERSION))

install: all
	install -d -m0755 $(DESTDIR)$(sbindir)
	install -d -m0755 $(DESTDIR)$(mandir)
	install -m0755 honest_identd $(DESTDIR)$(sbindir)
	install -m0755 slidentd $(DESTDIR)$(sbindir)
	install -m0644 slidentd.8 $(DESTDIR)$(mandir)
	install -m0644 honest_identd.8 $(DESTDIR)$(mandir)

