#
# Copyright (C) 2002-2006  Dmitry V. Levin <ldv@altlinux.org>
# 
# Makefile for the slocate project.
#
# This file 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

VERSION = $(shell sed '/^Version: */!d;s///;q' slocate.spec)
LFS_CFLAGS = $(shell getconf LFS_CFLAGS)
WARNINGS = -Wall -W -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings \
	-Wconversion -Waggregate-return -Wstrict-prototypes -Werror \
	-Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn \
	-Wmissing-format-attribute -Wredundant-decls -Wdisabled-optimization
CPPFLAGS = -std=gnu99 $(WARNINGS) -D_GNU_SOURCE -DPROJECT_VERSION=\"$(VERSION)\" \
	 $(LFS_CFLAGS)
CFLAGS = -pipe -Wall -Werror -W -O2
LDLIBS =
INSTALL = install
BINDIR = /usr/bin
SBINDIR = /usr/sbin
MANDIR = /usr/share/man
CONFDIR = /etc
CRONDIR = $(CONFDIR)/cron.daily
DESTDIR =

locate_SRC = glob.c ldb.c locate.c lstats.c main.c match.c nextelem.c readstr.c regex.c substr.c xmalloc.c
mklocatedb_SRC = frencode.c fstypes.c lists.c mklocatedb.c scantree.c updatedb.c xmalloc.c
SRC = $(shell echo $(locate_SRC) $(mklocatedb_SRC) |tr -s '[:space:]' '\n' |sort -u |tr '\n' ' ')
OBJ = $(SRC:.c=.o)
DEP = $(SRC:.c=.d)

TARGETS = locate mklocatedb
.PHONY:	all install clean indent

all: $(TARGETS)

scantree.d: LFS_CFLAGS =
scantree.o: LFS_CFLAGS =

locate: $(locate_SRC:.c=.o)

mklocatedb: LDLIBS = -lcap
mklocatedb: $(mklocatedb_SRC:.c=.o)

install: all
	$(INSTALL) -pD -m2711 locate $(DESTDIR)$(BINDIR)/slocate
	ln -s slocate $(DESTDIR)$(BINDIR)/locate
	$(INSTALL) -pD -m755 mklocatedb $(DESTDIR)$(SBINDIR)/mklocatedb
	$(INSTALL) -pD -m700 updatedb.sh $(DESTDIR)$(SBINDIR)/updatedb
	$(INSTALL) -pD -m700 updatedb.cron $(DESTDIR)$(CRONDIR)/updatedb
	$(INSTALL) -pD -m600 updatedb.conf $(DESTDIR)$(CONFDIR)/updatedb.conf
	$(INSTALL) -pD -m644 slocate.1 $(DESTDIR)$(MANDIR)/man1/slocate.1
	$(INSTALL) -pD -m644 locate.1 $(DESTDIR)$(MANDIR)/man1/locate.1
	$(INSTALL) -pD -m644 locatedb.5 $(DESTDIR)$(MANDIR)/man5/locatedb.5
	$(INSTALL) -pD -m644 mklocatedb.8 $(DESTDIR)$(MANDIR)/man8/mklocatedb.8
	$(INSTALL) -pD -m644 updatedb.8 $(DESTDIR)$(MANDIR)/man8/updatedb.8

clean:
	$(RM) $(DEP) $(OBJ) $(TARGETS) core *~

indent:
	indent *.h *.c

# We need dependencies only if goal isn't "indent" or "clean".
ifneq ($(MAKECMDGOALS),indent)
ifneq ($(MAKECMDGOALS),clean)

%.d:	%.c
	@echo Making dependences for $<
	@$(SHELL) -ec "$(CC) -MM $(CPPFLAGS) $< | sed 's|\($*\)\.o[ :]*|\1.o $@ : |g' > $@; [ -s $@ ] || $(RM) $@"

ifneq ($(DEP),)
-include $(DEP)
endif

endif # clean
endif # indent
