#
# Makefile : Utilities for the Linux Multiple Devices driver
#            Copyright (C) 1996 Marc Zyngier
#            Copyright (C) 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
#
# this is the main Makefile for the the MD tools
#
# This source is covered by the GNU GPL, the same as all Linux kernel
# sources.
#
VERS    = @VERS@

srcdir  = @srcdir@
VPATH   = @srcdir@

SBIN    = @sbindir@
USR     = @prefix@
MAN     = $(USR)/man

SOURCES = 	mdadd.c mdcreate.c mdparse.c crc.c \
		ckraid.c parser.c raid_io.c mkraid.c \
		configure.in Makefile.in 

HEADERS = 	common.h config.h parser.h raid_io.h

MANUALS = 	mdadd.8 mdcreate.8 mdtab.5

MISC	= 	COPYING configure ChangeLog.old raid1.conf.sample \
		raid4.conf.sample raid5.conf.sample \
		QuickStart.RAID crc32.tab README md_FAQ mdtab.sample \
		INSTALL

LINKS   = mdrun mdstop

DISTFILES = $(SOURCES) $(HEADERS) $(MANUALS) $(MISC)

BINARIES = mdadd mdcreate @newtools@
CC = @CC@
LINUXSRC = @LINUXSRC@
CFLAGS = @CFLAGS@ -I$(LINUXSRC)/include -DMD_VERSION=\""raidtools-@VERS@"\"
LDFLAGS = @LDFLAGS@
LDFLAGS_THREADED = @LDFLAGS_THREADED@

all: mdadd mdcreate @newtools@

mdcreate : mdcreate.o mdparse.o crc.o Makefile $(HEADERS)
	$(CC) -o mdcreate mdcreate.o mdparse.o crc.o $(LDFLAGS)

mdadd : mdadd.o mdparse.o crc.o Makefile $(HEADERS)
	$(CC) -o mdadd mdadd.o mdparse.o crc.o $(LDFLAGS)

mkraid: mkraid.o parser.o raid_io.o Makefile $(HEADERS)
	$(CC) -o mkraid mkraid.o parser.o raid_io.o $(LDFLAGS_THREADED)

ckraid: ckraid.o parser.o raid_io.o Makefile $(HEADERS)
	$(CC) -o ckraid ckraid.o parser.o raid_io.o $(LDFLAGS_THREADED)

clean:
	rm -f $(BINARIES) $(LINKS) *.o core *~

configure: configure.in 
	autoconf --localdir=$(srcdir) $(srcdir)/configure.in > configure
	chmod 755 configure

install: install_bin install_doc install_dev @install_new@ 

install_dev:
	maj=`grep MD_MAJOR $(LINUXSRC)/include/linux/major.h | tr -s ' ' | cut -d' ' -f3`; for i in 0 1 2 3; do if [ ! -e /dev/md$$i ]; then mknod -m 0600 /dev/md$$i b $$maj $$i; fi; done

install_new:
	for i in @newtools@; do install -m 755 -o root -g root -s $$i $(SBIN)/$$i; done

install_doc:
	install -m 644 -o root -g root $(srcdir)/mdadd.8    $(MAN)/man8
	install -m 644 -o root -g root $(srcdir)/mdcreate.8 $(MAN)/man8
	install -m 644 -o root -g root $(srcdir)/mdtab.5    $(MAN)/man5
	#for i in $(LINKS); do ln -sf $(MAN)/man8/mdadd.8 $(MAN)/man8/$$i.8; done

install_bin: 
	install -m 755 -o root -g root -s mdadd    $(SBIN)
	install -m 755 -o root -g root -s mdcreate $(SBIN)
	#for i in $(LINKS); do if [ ! -e $(SBIN)/$$i ]; then ln -s $(SBIN)/mdadd $(SBIN)/$$i; fi; done

dep depend:
	$(CPP) -M $(CPPFLAGS) $(CFLAGS) $(srcdir)/*.c > .depend

dist: 
	-mkdir ../raidtools-$(VERS)
	cp $(DISTFILES) ../raidtools-$(VERS)
	cd ..; tar czvf raidtools-$(VERS).tar.gz raidtools-$(VERS)
