CFLAGS=	  @CFLAGS@
CC=	  @CC@
LD=       @LD@
SMP=	  @SMP@
MODVERSIONS=@MODVERSIONS@
KVER=	  @KVER@
KDIR=     @KTOPDIR@
prefix=   @prefix@
SBINDIR=  $(prefix)/sbin
LKCDSIALDIR=$(prefix)/share/sial/lcrash

ifdef	SMP
CFLAGS+=-D__SMP__
endif
ifdef	MODVERSIONS
CFLAGS+=-DMODVERSIONS -include /usr/include/linux/modversions.h $(MODS)
endif

CFLAGS+=-I$(KDIR)/include
CFLAGS_LKCD=-I/usr/include/lkcd

SUB_DIRS  = man
PROGS=lkst lkstm lkstbuf lksteh lkstlogger lkstlogd
PROGS_PL=logformat.pl
PROGS_LKCDLIB=lkstdumplib.so
PROGS_LKCDSIAL=lkstdump.sial

LKSTLOGD_LOGDIR=/var/log/lkst

all: $(PROGS) @LKSTDUMP@ subdirs_make

subdirs_make: 
	for dir in $(SUB_DIRS); do \
		$(MAKE) -C $$dir; \
	done

install: $(PROGS) $(PROGS_PL) @INSTDUMP@
	[ -d $(LKSTLOGD_LOGDIR) ] || mkdir -p $(LKSTLOGD_LOGDIR)
	for dir in $(SUB_DIRS); do \
		( $(MAKE) -C $$dir install; ); \
	done
	[ -d $(SBINDIR) ] || mkdir -p $(SBINDIR)
	for p in $(PROGS) $(PROGS_PL); do \
	  install -c -m 755 $$p $(SBINDIR)/$$p; \
	done
	[ -c /dev/lkst ] || /bin/mknod --mode=600 /dev/lkst c 231 0

install-lkstdump: $(PROGS_LKCDLIB) $(PROGS_LKCDSIAL)
	install -c -m 644 $(PROGS_LKCDSIAL) $(LKCDSIALDIR)/
	install -c -m 755 $(PROGS_LKCDLIB) $(LKCDSIALDIR)/

uninstall:
	for dir in $(SUB_DIRS); do \
		( $(MAKE) -C $$dir uninstall; ); \
	done
	for p in $(PROGS) $(PROGS_PL); do \
	  rm -f $(SBINDIR)/$$p; \
	done
	rmdir $(LKSTLOGD_LOGDIR)
	rm -f $(LKCDSIALDIR)/$(PROGS_LKCDSIAL)
	rm -f $(LKCDSIALDIR)/$(PROGS_LKCDLIB)

clean :
	for dir in $(SUB_DIRS); do \
		( $(MAKE) -C $$dir clean; ); \
	done
	-rm -f $(PROGS) *.o *.so core

distclean: clean
	for dir in $(SUB_DIRS); do \
		( $(MAKE) -C $$dir distclean; ); \
	done
	-rm -rf autom*.cache
	-rm -f config.* *~ Makefile configure .config

distdir= lkstutils-@LKSTVER@

dist: distclean
	mkdir .tmpdir
	cp -r * .tmpdir
	mv .tmpdir $(distdir)
	tar chozf $(distdir).tar.gz $(distdir)
	rm -rf $(distdir)

lkst : lkst.c command.c buffer.c maskset.c evhandler.c
lkstbuf : lkstbuf.c command.c buffer.c
lkstm : lkstm.c command.c maskset.c
lksteh : lksteh.c command.c evhandler.c
lkstlogger : lkstlogger.c command.c
lkstlogd : lkstlogd.c
lkstdumplib.so : lkstdumplib.o
	$(CC) -shared -o lkstdumplib.so lkstdumplib.o
lkstdumplib.o :
	$(CC) $(CFLAGS_LKCD) $(CFLAGS) -O -c -fPIC -o $@ lkstdumplib.c

