#  Copyright 1996-2004,2006-2010 Alain Knaff.
#  This file is part of mtools.
#
#  Mtools 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 3 of the License, or
#  (at your option) any later version.
#
#  Mtools 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 Mtools.  If not, see <http://www.gnu.org/licenses/>.
#
#       Makefile for Mtools
#
# check the Configure file for some examples of device-specific setups
# Berkeley flavors of Unix should include -DBSD in the CFLAGS.  Pick
# a lock method... either -DLOCKF, -DFLOCK, or -DFCNTL and put that
# string in the CFLAGS line below.

# User specified flags
USERCFLAGS = 
USERLDFLAGS =
USERLDLIBS =

MAKEINFO = @MAKEINFO@
TEXI2DVI = @TEXI2DVI@
TEXI2PDF = @TEXI2PDF@
TEXI2HTML = @TEXI2HTML@
DVI2PS = @DVI2PS@


# do not edit below this line
# =============================================================================

SHELL = /bin/sh

top_srcdir=@top_srcdir@
srcdir=@srcdir@
VPATH=@srcdir@

prefix      = @prefix@
exec_prefix = @exec_prefix@
bindir      = @bindir@
infodir     = @infodir@
mandir      = @mandir@
sysconfdir  = @sysconfdir@
datarootdir = @datarootdir@

CC         = @CC@
CXX        = @CXX@
MYCFLAGS   = @CFLAGS@
MYCXXFLAGS = @CXXFLAGS@
CPPFLAGS   = @CPPFLAGS@
HOST_ID    = @HOST_ID@
DEFS       = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)\" $(HOST_ID)

LDFLAGS     = @LDFLAGS@
LIBS        = @LIBS@
SHLIB       = @SHLIB@
MACHDEPLIBS = @MACHDEPLIBS@	
LN_S        = @LN_S@

INSTALL         = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA    = @INSTALL_DATA@
INSTALL_INFO	= @INSTALL_INFO@

.SUFFIXES:
.SUFFIXES: .o .c
.SUFFIXES: .o .c

MAN1 = floppyd.1 floppyd_installtest.1 mattrib.1 mbadblocks.1 mcat.1	\
mcd.1 mcopy.1 mdel.1 mdeltree.1 mdir.1 mdu.1 mformat.1	\
minfo.1 mkmanifest.1 mlabel.1 mmd.1 mmount.1 mmove.1 mpartition.1	\
mrd.1 mren.1 mshortname.1 mshowfat.1 mtoolstest.1 mtools.1 mtype.1	\
mzip.1

MAN1EXT = 1
MAN1DIR	= $(DESTDIR)$(mandir)/man${MAN1EXT}
MAN5	= mtools.5
MAN5EXT	= 5
MAN5DIR	= $(DESTDIR)$(mandir)/man${MAN5EXT}

# objects for building mtools
OBJS_MTOOLS = buffer.o charsetConv.o codepages.o config.o copyfile.o	\
device.o devices.o dirCache.o directory.o direntry.o dos2unix.o		\
expand.o fat.o fat_free.o file.o file_name.o force_io.o hash.o init.o	\
lba.o llong.o lockdev.o match.o mainloop.o mattrib.o mbadblocks.o	\
mcat.o mcd.o mcopy.o mdel.o mdir.o mdoctorfat.o mdu.o	\
mformat.o minfo.o misc.o missFuncs.o mk_direntry.o mlabel.o mmd.o	\
mmount.o mmove.o mpartition.o mshortname.o mshowfat.o mzip.o mtools.o	\
offset.o old_dos.o open_image.o patchlevel.o partition.o plain_io.o	\
precmd.o privileges.o remap.o scsi_io.o scsi.o signal.o stream.o	\
streamcache.o swap.o unix2dos.o unixdir.o tty.o vfat.o		\
strtonum.o @FLOPPYD_IO_OBJ@ @XDF_IO_OBJ@

# objects for building mkmanifest
OBJS_MKMANIFEST = missFuncs.o mkmanifest.o misc.o patchlevel.o

# objects for building floppyd
OBJS_FLOPPYD = floppyd.o llong.o lockdev.o

# objects for building floppyd_installtest
OBJS_FLOPPYD_INSTALLTEST = floppyd_installtest.o misc.o expand.o	\
privileges.o strtonum.o

SRCS = buffer.c codepages.c config.c copyfile.c device.c devices.c	\
dirCache.c directory.c direntry.c dos2unix.c expand.c fat.c		\
fat_free.c file.c file_name.c file_read.c force_io.c hash.c init.c	\
lba.c lockdev.c match.c mainloop.c mattrib.c mbadblocks.c mcat.c	\
mcd.c mcopy.c mdel.c mdir.c mdu.c mdoctorfat.c		\
mformat.c minfo.c misc.c missFuncs.c mk_direntry.c mlabel.c mmd.c	\
mmount.c mmove.c mpartition.c mshortname.c mshowfat.c mzip.c mtools.c	\
offset.c old_dos.c open_image.c partition.c plain_io.c precmd.c		\
privileges.c remap.c scsi_io.c scsi.c signal.c stream.c streamcache.c	\
swap.c unix2dos.s unixdir.c tty.c vfat.c mkmanifest.c			\
@FLOPPYD_IO_SRC@ @XDF_IO_SRC@

SRCS-@USE_FLOPPYD@ += floppyd_io.c
SRCS-@USE_XDF@ += xdf_io.c

SCRIPTS = mcheck mxtar uz tgz mcomp amuFormat.sh

LINKS=mattrib mcat mcd mcopy mdel mdeltree mdir mdu	\
mformat minfo mlabel mmd mmount mmove mpartition mrd mren mtype	\
mtoolstest mshortname mshowfat mbadblocks mzip

X_CFLAGS = @X_CFLAGS@
X_LIBS = @X_LIBS@
X_EXTRA_LIBS = @X_EXTRA_LIBS@
X_PRE_LIBS = @X_PRE_LIBS@
CFLAGS = $(CPPFLAGS) $(DEFS) $(MYCFLAGS) -fno-strict-aliasing -I. @extraincludedir@ -I@srcdir@ $(USERCFLAGS)
CXXFLAGS  = $(CPPFLAGS) $(DEFS) $(MYCXXFLAGS) -I. @extraincludedir@ -I@srcdir@ $(USERCFLAGS)
LINK      = $(CC) $(LDFLAGS) $(USERLDFLAGS) @extralibdir@
ALLLIBS   = $(USERLDLIBS) $(MACHDEPLIBS) $(SHLIB) $(LIBS)
X_LDFLAGS = $(X_EXTRA_LIBS) $(X_LIBS) @FLOPPYD_LIBS@ $(LIBS)
X_CCFLAGS = $(X_CFLAGS) $(CFLAGS)

all:    mtools $(LINKS) mkmanifest @FLOPPYD@ mtools.1 mtools.5

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

#%.o: %.cpp
#	$(CXX) $(CXXFLAGS) -c $<

mtools: $(OBJS_MTOOLS)
	$(LINK) $(OBJS_MTOOLS) -o $@ $(ALLLIBS)

mkmanifest: $(OBJS_MKMANIFEST)
	$(LINK) $(OBJS_MKMANIFEST) -o $@ $(ALLLIBS)

floppyd.o: floppyd.c
	$(CC) $(X_CCFLAGS) -c $?

floppyd: $(OBJS_FLOPPYD)
	$(LINK) $(OBJS_FLOPPYD) -o $@ $(X_LDFLAGS)
floppyd_installtest: $(OBJS_FLOPPYD_INSTALLTEST)
	$(LINK) $(OBJS_FLOPPYD_INSTALLTEST) -o $@ $(ALLLIBS)


$(LINKS): mtools
	rm -f $@ && $(LN_S) mtools $@

mostlyclean:
	-rm -f *~ *.orig *.o a.out core 2>/dev/null

clean:	mostlyclean
	-rm -f mtools $(LINKS) floppyd floppyd_installtest mkmanifest *.info* *.dvi *.html 2>/dev/null


texclean:
	-rm -f mtools.aux mtools.toc mtools.log
	-rm -f mtools.cps mtools.pgs mtools.vrs
	-rm -f mtools.cp mtools.fn mtools.ky
	-rm -f mtools.pg mtools.tp mtools.vr

info: mtools.info
%.info: %.texi sysconfdir.texi
	$(MAKEINFO) -I$(srcdir) $< --no-split --output=$@

dvi: mtools.dvi
%.dvi: %.texi sysconfdir.texi
	$(TEXI2DVI) $<

ps: mtools.ps
%.ps: %.dvi
	$(DVI2PS) -f < $< > $@

pdf: mtools.pdf
%.pdf: %.texi sysconfdir.texi
	$(TEXI2PDF) $<

mtools.1: mtools.tmpl.1 Makefile
	sed "s%SYSCONFDIR%$(sysconfdir)/%g" $(srcdir)/mtools.tmpl.1 >mtools.1

mtools.5: mtools.tmpl.5 Makefile
	sed "s%SYSCONFDIR%$(sysconfdir)/%g" $(srcdir)/mtools.tmpl.5 >mtools.5

sysconfdir.texi:
	echo "@set SYSCONFDIR $(sysconfdir)/" >sysconfdir.texi

html: mtools.html mtools_toc.html
%.html %_toc.html: %.texi  sysconfdir.texi
	$(TEXI2HTML) $<

# Don't cd, to avoid breaking install-sh references.
install-info: info
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(infodir)
	if test -f mtools.info; then \
	  for i in mtools.info*; do \
	    $(INSTALL_DATA) $$i $(DESTDIR)$(infodir)/$$i; \
	  done; \
	else \
	  for i in $(srcdir)/mtools.info*; do \
	    $(INSTALL_DATA) $$i $(DESTDIR)$(infodir)/`echo $$i | sed 's|^$(srcdir)/||'`; \
	  done; \
	fi; \
	if [ -n "$(INSTALL_INFO)" ] ; then \
		$(INSTALL_INFO) --info-dir=$(DESTDIR)$(infodir) $(DESTDIR)$(infodir)/mtools.info; \
	fi

uninstall-info:
	cd $(DESTDIR)$(infodir) && rm -f mtools.info*

install:	$(DESTDIR)$(bindir)/mtools @BINFLOPPYD@ install-man install-links \
		$(DESTDIR)$(bindir)/mkmanifest install-scripts install-info

uninstall:	uninstall-bin uninstall-man uninstall-links \
		uninstall-scripts

distclean: clean texclean
	rm -f config.cache config.h config.status config.log Makefile
maintainer-clean: distclean


$(DESTDIR)$(bindir)/floppyd: floppyd
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(bindir)
	$(INSTALL_PROGRAM) floppyd $(DESTDIR)$(bindir)/floppyd

$(DESTDIR)$(bindir)/floppyd_installtest: floppyd_installtest
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(bindir)
	$(INSTALL_PROGRAM) floppyd_installtest $(DESTDIR)$(bindir)/floppyd_installtest

$(DESTDIR)$(bindir)/mtools: mtools
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(bindir)
	$(INSTALL_PROGRAM) mtools $(DESTDIR)$(bindir)/mtools

$(DESTDIR)$(bindir)/mkmanifest: mkmanifest
	$(top_srcdir)/mkinstalldirs $(DESTDIR)$(bindir)
	$(INSTALL_PROGRAM) mkmanifest $(DESTDIR)$(bindir)/mkmanifest

#$(ETCDIR)/mtools: mtools.etc
#	cp mtools.etc $(ETCDIR)/mtools

install-links: $(DESTDIR)$(bindir)/mtools
	@for j in $(LINKS); do \
		rm -f $(DESTDIR)$(bindir)/$$j ; \
		$(LN_S) mtools $(DESTDIR)$(bindir)/$$j ; \
		echo $(DESTDIR)$(bindir)/$$j ; \
	done

## "z" is the older version of "gz"; the name is just *too* short
install-scripts: $(DESTDIR)$(bindir)/mtools
	@$(top_srcdir)/mkinstalldirs $(DESTDIR)$(bindir)
	@for j in $(SCRIPTS) ; do \
		$(INSTALL_PROGRAM) $(srcdir)/scripts/$$j $(DESTDIR)$(bindir)/$$j ; \
		echo $(DESTDIR)$(bindir)/$$j ; \
	done
	rm -f $(DESTDIR)$(bindir)/lz
	cd $(DESTDIR)$(bindir) && $(LN_S) uz lz

install-man:
	@$(top_srcdir)/mkinstalldirs $(MAN1DIR)
	@for j in $(MAN1); do \
		$(INSTALL_DATA) $(srcdir)/$$j $(MAN1DIR)/$$j ; \
		echo $(MAN1DIR)/$$j ; \
	done
	@$(top_srcdir)/mkinstalldirs $(MAN5DIR)
	@for j in $(MAN5); do \
		$(INSTALL_DATA) $(srcdir)/$$j $(MAN5DIR)/$$j ; \
		echo $(MAN5DIR)/$$j ; \
	done

uninstall-bin:
	@for j in mtools mkmanifest; do \
		rm -f $(DESTDIR)$(bindir)/$$j ; \
		echo $(DESTDIR)$(bindir)/$$j ; \
	done

uninstall-scripts:
	@for j in $(SCRIPTS); do \
		rm -f $(DESTDIR)$(bindir)/$$j ; \
		echo $(DESTDIR)$(bindir)/$$j ; \
	done

uninstall-man:
	@for j in $(MAN1); do \
		rm -f $(MAN1DIR)/$$j ; \
		echo $(MAN1DIR)/$$j ; \
	done
	@for j in $(MAN5); do \
		rm -f $(MAN5DIR)/$$j ; \
		echo $(MAN5DIR)/$$j ; \
	done

uninstall-links:
	@for j in $(LINKS); \
		do rm -f $(DESTDIR)$(bindir)/$$j ; \
		echo $(DESTDIR)$(bindir)/$$j ; \
	done

depend: $(SRCS)
	makedepend -- $(CFLAGS) -- $^

check:
	echo No self tests included
# check target needed even if empty, in order to make life easier for
# automatic tools to install GNU soft


# DO NOT DELETE THIS LINE -- make depend depends on it.
