## There is no need to have DESTDIR separately since no file has paths coded
## in them. Just "make prefix=/my/path/usr install" when creating a package.

prefix = /usr/local
bindir = $(prefix)/bin
mandir = $(prefix)/man
docdir = $(prefix)/doc
docdir_name = lzma-4.27.1

INSTALL = install
RM = rm -f
LN = ln -s

all: utils sdk

utils:
	cd utils && $(MAKE)

sdk:
	cd sdk/SRC/7zip/Compress/LZMA_Alone && $(MAKE)

install: all
	$(INSTALL) -dm 0755 $(bindir) $(mandir)/man1 \
			$(docdir)/$(docdir_name)
	$(INSTALL) -pm 0755 utils/lzmadec utils/lzmainfo \
			utils/lzmash utils/lzdiff utils/lzmore utils/lzgrep \
			sdk/SRC/7zip/Compress/LZMA_Alone/lzma \
			$(bindir)
	$(INSTALL) -pm 0644 man/lzma.1 man/lzmadec.1 man/lzmainfo.1 \
			man/lzmash.1 man/lzdiff.1 man/lzgrep.1 man/lzmore.1 \
			$(mandir)/man1
	$(INSTALL) -pm 0644 BENCHMARKS COPYING.GPL COPYING.LGPL INSTALL \
			README README.LICENSES THANKS TODO sdk/lzma.txt \
			$(docdir)/$(docdir_name)
	$(LN) lzmash $(bindir)/lunzmash
	$(LN) lzmash $(bindir)/lzcat
	$(LN) lzdiff $(bindir)/lzcmp
	$(LN) lzmore $(bindir)/lzless
	$(LN) lzgrep $(bindir)/lzegrep
	$(LN) lzgrep $(bindir)/lzfgrep
	$(LN) lzmash.1 $(mandir)/man1/lunzmash.1
	$(LN) lzmash.1 $(mandir)/man1/lzcat.1
	$(LN) lzdiff.1 $(mandir)/man1/lzcmp.1
	$(LN) lzmore.1 $(mandir)/man1/lzless.1
	$(LN) lzgrep.1 $(mandir)/man1/lzegrep.1
	$(LN) lzgrep.1 $(mandir)/man1/lzfgrep.1


clean:
	-cd utils && $(MAKE) clean
	-cd sdk/SRC/7zip/Compress/LZMA_Alone && $(MAKE) clean
	$(RM) tests/testfile.*

distclean: clean
	find . -name '*~' -exec rm -vf {} \;

dist: distclean
	find . -perm +0111 -exec chmod 0755 {} \;
	find . \! -perm +0111 -exec chmod 0644 {} \;
	SOURCEDIR="$$(basename "$$PWD")" \
			&& cd .. \
			&& tar cf - --owner=root --group=root "$$SOURCEDIR" \
			| gzip -9 > "$$SOURCEDIR".tar.gz

test: all
	cd tests && sh tests.sh

.PHONY: all configure utils sdk clean distclean dist test
