#!/usr/bin/make
# $AMSEL: Makefile,v 1.13 2004/07/02 16:18:51 reyk Exp $

# Copyright (c) 2004 .vantronix | secure systems GmbH
# Copyright (c) 2004 SSV Embedded Systems GmbH
#
# All rights reserved.
#
# Written by Reyk Floeter <reyk@vantronix.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version. 
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

AMSEL_INCLUDE	= /usr/local/include/amsel
AMSEL_CFLAGS	= -I$(AMSEL_INCLUDE) -I$(AMSEL_INCLUDE)/compat -D_GNU_SOURCE=1

ifeq ($(strip $(LANG)),de)
TARGET		= amwall_de
LANG		= de
else
TARGET		= amwall
LANG		= en
endif
ifneq ($(strip $(DEBUG)),)
DEBUG_CFLAGS	= -g3 -ggdb -DDEBUG=1
endif
ifeq ($(strip $(VERBOSE)),)
V		= @
endif

# The build's version number (date for CVS-builds)
#VERSION		= $(shell date +"%Y%m%d")-cvs

SOURCES		+= main.c \
			tables.c \
			ipvs.c \
			grammar_$(LANG).y \
			parser.c
OBJECTS 	= $(subst .l,.o,$(subst .y,.o,$(subst .c,.o,$(SOURCES))))
HEADERS		= $(LDIR)/main.h $(LDIR)/version.h $(LDIR)/tables.h $(LDIR)/ipvs.h
LDIR		= $(PWD)
CC		= gcc
LD		= $(CC)
INSTALL		= install
OPT_CFLAGS	=
CFLAGS		= -Wall -I$(LDIR) -DLANG=\"$(LANG)\" \
		    $(AMSEL_CFLAGS) $(OPT_CFLAGS) $(DEBUG_CFLAGS)
ifneq ($(strip $(VERSION)),)
CFLAGS		+= -DVERSION_BUILD=\"$(VERSION)\"
endif

OPT_LDFLAGS	=
LDFLAGS		= $(OPT_LDFLAGS) -lamsel
YACC		= bison
YFLAGS		= -d
PREFIX		= /usr/local
CONFIG		= $(LDIR)/etc/amsel

all: $(TARGET)

$(TARGET): $(OBJECTS)
	@echo -e "  LD\t$@ ($(OBJECTS))"
	$(V) $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@

$(OBJECTS): $(HEADERS)

main.o: main.c

parser.o: parser.c

grammar_$(LANG).o: grammar_$(LANG).y

install: 
	$(INSTALL) -d \
	    $(DESTDIR)$(PREFIX)/sbin \
	    $(DESTDIR)$(PREFIX)/bin \
	    $(DESTDIR)/etc/amsel \
	    $(DESTDIR)$(PREFIX)/share/man/man1 \
	    $(DESTDIR)$(PREFIX)/share/man/man5
	$(INSTALL) -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/
	$(INSTALL) -m 750 $(TARGET)_commit $(DESTDIR)$(PREFIX)/sbin/
	$(INSTALL) -m 660 $(CONFIG)/*.conf $(DESTDIR)/etc/amsel/
	$(INSTALL) -m 644 $(TARGET).1 $(DESTDIR)/$(PREFIX)/share/man/man1
	$(INSTALL) -m 644 *.conf.5 $(DESTDIR)/$(PREFIX)/share/man/man5

dist:
	@mkdir /tmp/$(TARGET)-$(VERSION)
	@cp -ar * /tmp/$(TARGET)-$(VERSION)/
	@tar \
		-C /tmp \
		--exclude='$(TARGET)-*.tar.bz2' \
		--exclude='$(TARGET)' \
		--exclude='*.o' \
		--exclude='*.so' \
		--exclude='*~' \
		--exclude='\#*' \
		--exclude='.\#*' \
		--exclude='CVS*' \
		-jcvf $(TARGET)-$(VERSION).tar.bz2 $(TARGET)-$(VERSION)
	@rm -fr /tmp/$(TARGET)-$(VERSION)

info:
	@echo -e "CC\t= $(CC)"
	@echo -e "LD\t= $(LD)"
	@echo -e "LDFLAGS\t= $(LDFLAGS)"
	@echo -e "CFLAGS\t= $(CFLAGS)"

clean:
	$(shell find $(LDIR) -name "*~" -exec rm {} \;)
	$(RM) -f $(TARGET) $(OBJECTS) *.y.? grammar.h

.y.o:
	@echo -e \
	"#ifndef _H_GRAMMAR\n#define _H_GRAMMAR\n#include \"$<.h\"\n#endif" \
		> grammar.h
	@echo -e "  YACC\t$<"
	$(V) $(YACC) $(YFLAGS) -o $<.c $<
	@echo -e "  CC\t$@"
	$(V) ${COMPILE.c} -o $@ $<.c

%.o : %.c
	@echo -e "  CC\t$@"
	$(V) $(CC) $(CFLAGS) -c -o $@ $<
