# Makerules.in - Makefile for Xaric
# Copyright (c) 2000 Rex Feany (laeos@xaric.org)
#
# This file is a part of Xaric, a silly IRC client.
# You can find Xaric at http://www.xaric.org/.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# @(#)Makerules.in 1.9
#

.DELETE_ON_ERROR:

# Paths
srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@

bindir = @bindir@
sbindir = @sbindir@
libexecdir = @libexecdir@
datadir = @datadir@
sysconfdir = @sysconfdir@
sharedstatedir = @sharedstatedir@
localstatedir = @localstatedir@
libdir = @libdir@
infodir = @infodir@
mandir = @mandir@
includedir = @includedir@
oldincludedir = /usr/include

pkgdatadir = $(datadir)/xaric
pkglibdir = $(libdir)/xaric
pkgincludedir = $(includedir)/xaric

ACLOCAL = @ACLOCAL@
AUTOCONF = @AUTOCONF@
AUTOHEADER = @AUTOHEADER@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@ 
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@

# Where do we install the help files?
INSTALL_HELP = $(pkgdatadir)/help

# Programs
SHELL	 := @SHELL@
CC 	 := @CC@
CFLAGS 	 := @CFLAGS@ @REP_CFLAGS@ @DEFS@ -Iinclude 
CPP 	 := @CPP@
LDFLAGS  := @LDFLAGS@
LIBS	 := @LIBS@
REP_LIBS := @REP_LIBS@
SED	 := sed

# Extra stuff we need to compile (referse to source files under extra/)
EXTRA   := @EXTRA_SRC@ @ALLOCA@

# yes, we're including makefiles not asking them to run.
# this is so makefiles in directories work. 
REALLY_INCLUDE := 1

# Header files that are generated automagicly
AUTOHDR := include/build.h include/fset_gen.h include/vars_gen.h

# This is the default target, which compiles the xaric binary
all:  xaric

# Include the data from the subdirectories
include ${srcdir}/source/Makefile

# and our dependancy info
-include .depend

# Figure out ...
# ... what source files we have
O_SRC += $(addprefix extra/, $(sort $(EXTRA)))
# ... what we should depend on
O_OBJ += $(O_SRC:.c=.o) 

# Build xaric!
xaric: Makerules $(AUTOHDR) $(O_OBJ)
	$(CC) -o xaric $(CFLAGS) $(O_OBJ) $(LIBS) $(REP_LIBS)

dep: $(O_SRC)
	@touch $(AUTOHDR) # so it works ...
	$(CC) $(CFLAGS) $(INCLUDES) -MM $(O_SRC) | $(SED) -e "s/\(^[^: ]\+\): \([^. ]\+\).c/\2.o: \2.c/"  > .depend
	@rm -f $(AUTOHDR)

# generate array indexes automagicly
include/fset_gen.h: source/fset.c
	cat $< | $(SED) -n -e "/\*--start--\*/,/\*--end--\*/ { s/^[^\"]*\"\([^\"]*\).*$$/\1_FSET,/; s/\/\*[^*]*\*\///; p; }" > $@

include/vars_gen.h: source/vars.c
	cat $< | $(SED) -n -e "/\*--start--\*/,/\*--end--\*/ { s/^[^\"]*\"\([^\"]*\).*$$/\1_VAR,/; s/\/\*[^*]*\*\///; p; }" > $@

install: installxaric installhelp installman
	@echo ""
	@echo "run 'make installdata' if you want the default datafiles installed"
	@echo ""

installxaric:
	$(SHTOOL) mkdir -f -p -m 755  $(bindir)
	$(SHTOOL) install -c xaric $(bindir)

installhelp:
	( $(SHTOOL) mkdir -f -p -m 755 $(INSTALL_HELP) && \
		cd help && tar -cf - . | ( cd $(INSTALL_HELP); tar -xf - ) )

installman:
	( $(SHTOOL) mkdir -f -p -m 755 $(mandir)/man1 && \
		for i in xaric.1; do $(SHTOOL) install -c doc/$$i $(mandir)/man1; done )

installdata:
	for i in servers; do $(SHTOOL) install -c doc/$$i $(pkgdatadir); done
 
# rebuild makefile and configure
${srcdir}/configure: configure.in aclocal.m4 acinclude.m4
	cd ${srcdir} && $(AUTOCONF)

${srcdir}/include/config.h.in: ${srcdir}/include/stamp-h.in

${srcdir}/include/stamp-h.in: configure.in aclocal.m4 acinclude.m4 acconfig.h include/config.h.in
	cd ${srcdir} && autoheader
	echo timestamp > ${srcdir}/include/stamp-h.in

config.h: stamp-h
stamp-h: config.h.in config.status
	./config.status

Makerules: Makerules.in config.status
	./config.status

config.status: configure
	./config.status --recheck


# Supply some more information to the build
include/build.h: Makerules
	@echo \#define XARIC_DATA_PATH \"$(pkgdatadir)\" >> .ver
	@mv .ver $@

clean:
	$(RM) $(O_OBJ)

distclean: clean
	$(RM) Makerules config.status config.log include/config.h .depend include/stamp-h
	$(RM) config.cache $(AUTOHDR)
	$(RM) xaric

mtclean: distclean
	$(RM) aclocal.m4 configure include/config.h.in

.c.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<

.o.so:
	$(LD) -shared -o $@ $<
