# $Id: Makefile.in 1097 2008-08-03 23:18:56Z geofft $

srcdir = @srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
datadir = @datadir@/barnowl
bindir = @bindir@
mandir = @mandir@

CC=@CC@
LIBS=@LIBS@ -L./libfaim -lfaim
CFLAGS=@CFLAGS@ -I./libfaim -DDATADIR=\"${datadir}\" -DOWL_SVN_REVNO=$(shell ./svkversion)
LDFLAGS=@LDFLAGS@
XSUBPPDIR=@XSUBPPDIR@
XSUBPPFLAGS=@XSUBPPFLAGS@
INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
INSTALL_DATA=@INSTALL_DATA@
ATHSTATIC=@ATHSTATIC@

BASE_SRCS=list.c message.c mainwin.c popwin.c zephyr.c messagelist.c \
     commands.c global.c text.c fmtext.c editwin.c util.c logging.c \
     perlconfig.c keys.c functions.c zwrite.c viewwin.c help.c filter.c \
     regex.c history.c view.c dict.c variable.c filterelement.c pair.c \
     keypress.c keymap.c keybinding.c cmd.c context.c zcrypt.c \
     aim.c buddy.c buddylist.c timer.c style.c errqueue.c \
     zbuddylist.c muxevents.c popexec.c obarray.c select.c wcwidth.c \
     glib_compat.c
OWL_SRC = owl.c
TESTER_SRC = tester.c

EXE = barnowl
PERL_MODULES = Jabber
MODULE_DIRS = $(PERL_MODULES:%=perl/modules/%)

BASE_OBJS = $(BASE_SRCS:.c=.o)

GEN_C = varstubs.c perlglue.c perlwrap.c
GEN_H = owl_prototypes.h 
GEN_O = $(GEN_C:.c=.o)

OBJS = $(BASE_OBJS) $(GEN_O)

AUTOGEN=$(GEN_C) $(GEN_H)
#AUTOGEN=$(GEN_C)

HEADERS=$(GEN_H) owl.h config.h test.h

all: $(EXE) $(MODULE_DIRS)

$(EXE): $(AUTOGEN) $(OBJS) owl.o libfaim
	$(ATHSTATIC) $(CC) -o $(EXE) owl.o $(OBJS) $(LDFLAGS) $(LIBS)

tester: $(AUTOGEN) $(OBJS) tester.o
	$(ATHSTATIC) $(CC) -o tester tester.o $(OBJS) $(LDFLAGS) $(LIBS)

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

# zcrypt is old crufty DES code I don't want to touch
# Disable all warnings when compiling it
zcrypt.o: zcrypt.c $(HEADERS)
	$(CC) -c $(CFLAGS) $< -w -o $@

test: tester
	./tester reg

clean: libfaimclean modules_clean
	$(RM) $(EXE) tester *.o $(AUTOGEN) owl_prototypes.h.new

distclean: clean libfaimdistclean
	$(RM) config.cache config.log config.status  Makefile config.h TAGS *~ core

.PHONY: $(MODULE_DIRS)

modules: $(MODULE_DIRS)
modules_clean:
	for i in $(MODULE_DIRS); do \
		cd $$i; test ! -f Makefile || make clean; \
	done

$(MODULE_DIRS): %: %/Makefile
	( cd $@ && make $(notdir $@).par )

$(MODULE_DIRS:=/Makefile): %/Makefile: %/Makefile.PL
	( cd $(dir $@) && perl -I../../lib Makefile.PL )

proto: owl_prototypes.h

perlglue.c: perlglue.xs Makefile 
	perl $(XSUBPPDIR)/xsubpp $(XSUBPPFLAGS) -prototypes perlglue.xs > perlglue.c

varstubs.c: variable.c stubgen.pl
	perl stubgen.pl > varstubs.c

perlwrap.c: perlwrap.pm encapsulate.pl
	perl encapsulate.pl perlwrap.pm owl_perlwrap_codebuff > perlwrap.c

# Only move owl_prototypes.h into place if the new one is different
owl_prototypes.h: codelist.pl varstubs.c $(BASE_SRCS)
	perl codelist.pl > owl_prototypes.h.new
	@cmp -s owl_prototypes.h.new $@ || { \
	 test -f $@ && echo 'Interfaces changed!'; \
	 echo mv -f owl_prototypes.h.new $@; \
	      mv -f owl_prototypes.h.new $@; }
#owl_prototypes.h:

.PHONY: tags clean distclean proto test check-syntax

tags: TAGS

TAGS: $(BASE_SRCS) $(OWL_SRC) $(TESTER_SRC) $(GEN_C) owl.h $(GEN_H)
	etags $(BASE_SRCS) $(OWL_SRC) $(TESTER_SRC) $(GEN_C) owl.h $(GEN_H)

$(BASE_OBJS) varstubs.h: owl.h config.h owl_prototypes.h

libfaim: libfaim/libfaim.a

libfaim/libfaim.a:
	(cd libfaim; $(MAKE))

libfaimclean:
	(cd libfaim; $(MAKE) clean)

libfaimdistclean:
	(cd libfaim; $(MAKE) distclean)

install: all installdirs
	${INSTALL_PROGRAM} $(EXE) ${DESTDIR}${bindir}/$(EXE)
	${INSTALL_DATA} doc/barnowl.1 ${DESTDIR}${mandir}/man1/barnowl.1
	${INSTALL} -d ${DESTDIR}${datadir}/lib
	${INSTALL} -d ${DESTDIR}${datadir}/modules
	tar -C perl/lib --exclude .svn -cf - . | tar -C ${DESTDIR}${datadir}/lib -xf -
	for i in $(PERL_MODULES); do \
		${INSTALL_DATA} perl/modules/$$i/$$i.par ${DESTDIR}${datadir}/modules/$$i.par; \
	done

installdirs: mkinstalldirs
	${srcdir}/mkinstalldirs ${DESTDIR}${bindir} ${DESTDIR}${mandir}/man1 ${DESTDIR}${datadir}

# For emacs flymake-mode
check-syntax: proto
	$(CC) $(CFLAGS) -Wall -Wextra -pedantic -fsyntax-only $(CHK_SOURCES)
