DISTNAME = $(pkgname)-$(pkgversion)
INSTALL = install
INSTALL_PROGRAM = $(INSTALL) -m 755
INSTALL_SCRIPT = $(INSTALL) -m 755
INSTALL_DATA = $(INSTALL) -m 644
INSTALL_DIR = $(INSTALL) -d -m 755
SHELL = /bin/sh
CAN_RUN_INSTALLINFO = $(SHELL) -c "install-info --version" > /dev/null 2>&1

objs = arg_parser.o rc.o zutils.o \
       zcat.o zcmp.o zdiff.o zgrep.o ztest.o zupdate.o
zcat_objs = arg_parser.o rc.o zutils.o zcat.o
zcmp_objs = arg_parser.o rc.o zutils.o zcmp.o
zdiff_objs = arg_parser.o rc.o zutils.o zdiff.o
zgrep_objs = arg_parser.o rc.o zutils.o zgrep.o
ztest_objs = arg_parser.o rc.o zutils.o ztest.o
zupdate_objs = arg_parser.o rc.o zupdate.o
programs = zcat zcmp zdiff zgrep ztest zupdate
scripts = zegrep zfgrep


.PHONY : all install install-bin install-info install-man \
         install-strip install-compress install-strip-compress \
         install-bin-strip install-info-compress install-man-compress \
         uninstall uninstall-bin uninstall-info uninstall-man \
         doc info man check dist clean distclean

all : $(programs) $(scripts)

zcat : $(zcat_objs)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(zcat_objs)

zcmp : $(zcmp_objs)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(zcmp_objs)

zdiff : $(zdiff_objs)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(zdiff_objs)

zegrep : zegrep.in
	cat $(VPATH)/zegrep.in > $@
	chmod a+x zegrep

zfgrep : zfgrep.in
	cat $(VPATH)/zfgrep.in > $@
	chmod a+x zfgrep

zgrep : $(zgrep_objs)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(zgrep_objs)

ztest : $(ztest_objs)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(ztest_objs)

zupdate : $(zupdate_objs)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(zupdate_objs)

rc.o : rc.cc
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -DPROGVERSION=\"$(pkgversion)\" -DSYSCONFDIR=\"$(sysconfdir)\" -c -o $@ $<

zdiff.o : zdiff.cc
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -DDIFF=\"$(DIFF)\" -c -o $@ $<

zgrep.o : zgrep.cc
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -DGREP=\"$(GREP)\" -c -o $@ $<

%.o : %.cc
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<

$(objs)      : Makefile
$(scripts)   : Makefile
arg_parser.o : arg_parser.h
rc.o         : arg_parser.h rc.h
zcat.o       : arg_parser.h rc.h zutils.h recursive.cc zcatgrep.cc
zcmp.o       : arg_parser.h rc.h zutils.h zcmpdiff.cc
zdiff.o      : arg_parser.h rc.h zutils.h zcmpdiff.cc
zgrep.o      : arg_parser.h rc.h zutils.h recursive.cc zcatgrep.cc
ztest.o      : arg_parser.h rc.h zutils.h recursive.cc
zupdate.o    : arg_parser.h rc.h recursive.cc
zutils.o     : rc.h zutils.h


doc : info man

info : $(VPATH)/doc/$(pkgname).info

$(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texi
	cd $(VPATH)/doc && makeinfo $(pkgname).texi

man : $(VPATH)/doc/zcat.1 $(VPATH)/doc/zcmp.1 $(VPATH)/doc/zdiff.1 \
      $(VPATH)/doc/zgrep.1 $(VPATH)/doc/ztest.1 $(VPATH)/doc/zupdate.1

$(VPATH)/doc/zcat.1 : zcat
	help2man -n 'decompress and concatenate files to standard output' \
	  -o $@ --info-page=$(pkgname) ./zcat

$(VPATH)/doc/zcmp.1 : zcmp
	help2man -n 'decompress and compare two files byte by byte' \
	  -o $@ --info-page=$(pkgname) ./zcmp

$(VPATH)/doc/zdiff.1 : zdiff
	help2man -n 'decompress and compare two files line by line' \
	  -o $@ --info-page=$(pkgname) ./zdiff

$(VPATH)/doc/zgrep.1 : zgrep
	help2man -n 'search compressed files for a regular expression' \
	  -o $@ --info-page=$(pkgname) ./zgrep

$(VPATH)/doc/ztest.1 : ztest
	help2man -n 'verify the integrity of compressed files' \
	  -o $@ --info-page=$(pkgname) ./ztest

$(VPATH)/doc/zupdate.1 : zupdate
	help2man -n 'recompress bzip2, gzip, xz, zstd files to lzip format' \
	  -o $@ --info-page=$(pkgname) ./zupdate

Makefile : $(VPATH)/configure $(VPATH)/Makefile.in
	./config.status

check : all
	@$(VPATH)/testsuite/check.sh $(VPATH)/testsuite $(pkgversion)

install : install-bin install-info install-man
install-strip : install-bin-strip install-info install-man
install-compress : install-bin install-info-compress install-man-compress
install-strip-compress : install-bin-strip install-info-compress install-man-compress

install-bin : all
	if [ ! -d "$(DESTDIR)$(bindir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(bindir)" ; fi
	$(INSTALL_PROGRAM) ./zcat "$(DESTDIR)$(bindir)/zcat"
	$(INSTALL_PROGRAM) ./zcmp "$(DESTDIR)$(bindir)/zcmp"
	$(INSTALL_PROGRAM) ./zdiff "$(DESTDIR)$(bindir)/zdiff"
	$(INSTALL_SCRIPT) ./zegrep "$(DESTDIR)$(bindir)/zegrep"
	$(INSTALL_SCRIPT) ./zfgrep "$(DESTDIR)$(bindir)/zfgrep"
	$(INSTALL_PROGRAM) ./zgrep "$(DESTDIR)$(bindir)/zgrep"
	$(INSTALL_PROGRAM) ./ztest "$(DESTDIR)$(bindir)/ztest"
	$(INSTALL_PROGRAM) ./zupdate "$(DESTDIR)$(bindir)/zupdate"
	if [ ! -e "$(DESTDIR)$(sysconfdir)/$(pkgname)rc" ] ; then \
	  if [ ! -d "$(DESTDIR)$(sysconfdir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(sysconfdir)" ; fi ; \
	  $(INSTALL_DATA) $(VPATH)/$(pkgname)rc "$(DESTDIR)$(sysconfdir)/$(pkgname)rc" ; \
	fi

install-bin-strip : all
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install-bin

install-info :
	if [ ! -d "$(DESTDIR)$(infodir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(infodir)" ; fi
	-rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"*
	$(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info "$(DESTDIR)$(infodir)/$(pkgname).info"
	-if $(CAN_RUN_INSTALLINFO) ; then \
	  install-info --info-dir="$(DESTDIR)$(infodir)" "$(DESTDIR)$(infodir)/$(pkgname).info" ; \
	fi

install-info-compress : install-info
	lzip -v -9 "$(DESTDIR)$(infodir)/$(pkgname).info"

install-man :
	if [ ! -d "$(DESTDIR)$(mandir)/man1" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1" ; fi
	-rm -f "$(DESTDIR)$(mandir)/man1/zcat.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zcmp.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zdiff.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zgrep.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/ztest.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zupdate.1"*
	$(INSTALL_DATA) $(VPATH)/doc/zcat.1 "$(DESTDIR)$(mandir)/man1/zcat.1"
	$(INSTALL_DATA) $(VPATH)/doc/zcmp.1 "$(DESTDIR)$(mandir)/man1/zcmp.1"
	$(INSTALL_DATA) $(VPATH)/doc/zdiff.1 "$(DESTDIR)$(mandir)/man1/zdiff.1"
	$(INSTALL_DATA) $(VPATH)/doc/zgrep.1 "$(DESTDIR)$(mandir)/man1/zgrep.1"
	$(INSTALL_DATA) $(VPATH)/doc/ztest.1 "$(DESTDIR)$(mandir)/man1/ztest.1"
	$(INSTALL_DATA) $(VPATH)/doc/zupdate.1 "$(DESTDIR)$(mandir)/man1/zupdate.1"

install-man-compress : install-man
	lzip -v -9 "$(DESTDIR)$(mandir)/man1/zcat.1"
	lzip -v -9 "$(DESTDIR)$(mandir)/man1/zcmp.1"
	lzip -v -9 "$(DESTDIR)$(mandir)/man1/zdiff.1"
	lzip -v -9 "$(DESTDIR)$(mandir)/man1/zgrep.1"
	lzip -v -9 "$(DESTDIR)$(mandir)/man1/ztest.1"
	lzip -v -9 "$(DESTDIR)$(mandir)/man1/zupdate.1"

uninstall : uninstall-man uninstall-info uninstall-bin

uninstall-bin :
	-rm -f "$(DESTDIR)$(bindir)/zcat"
	-rm -f "$(DESTDIR)$(bindir)/zcmp"
	-rm -f "$(DESTDIR)$(bindir)/zdiff"
	-rm -f "$(DESTDIR)$(bindir)/zegrep"
	-rm -f "$(DESTDIR)$(bindir)/zfgrep"
	-rm -f "$(DESTDIR)$(bindir)/zgrep"
	-rm -f "$(DESTDIR)$(bindir)/ztest"
	-rm -f "$(DESTDIR)$(bindir)/zupdate"
	-rm -f "$(DESTDIR)$(sysconfdir)/$(pkgname)rc"

uninstall-info :
	-if $(CAN_RUN_INSTALLINFO) ; then \
	  install-info --info-dir="$(DESTDIR)$(infodir)" --remove "$(DESTDIR)$(infodir)/$(pkgname).info" ; \
	fi
	-rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"*

uninstall-man :
	-rm -f "$(DESTDIR)$(mandir)/man1/zcat.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zcmp.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zdiff.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zgrep.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/ztest.1"*
	-rm -f "$(DESTDIR)$(mandir)/man1/zupdate.1"*

dist : doc
	ln -sf $(VPATH) $(DISTNAME)
	tar -Hustar --owner=root --group=root -cvf $(DISTNAME).tar \
	  $(DISTNAME)/AUTHORS \
	  $(DISTNAME)/COPYING \
	  $(DISTNAME)/ChangeLog \
	  $(DISTNAME)/INSTALL \
	  $(DISTNAME)/Makefile.in \
	  $(DISTNAME)/NEWS \
	  $(DISTNAME)/README \
	  $(DISTNAME)/configure \
	  $(DISTNAME)/doc/*.1 \
	  $(DISTNAME)/doc/$(pkgname).info \
	  $(DISTNAME)/doc/$(pkgname).texi \
	  $(DISTNAME)/$(pkgname)rc \
	  $(DISTNAME)/*.h \
	  $(DISTNAME)/*.cc \
	  $(DISTNAME)/z*.in \
	  $(DISTNAME)/testsuite/check.sh \
	  $(DISTNAME)/testsuite/test.txt \
	  $(DISTNAME)/testsuite/test.txt.tar \
	  $(DISTNAME)/testsuite/zcat_vs.dat \
	  $(DISTNAME)/testsuite/test_bad_crc.lz \
	  $(DISTNAME)/testsuite/zero_bad_crc.lz \
	  $(DISTNAME)/testsuite/zero_bad_crc.gz
	rm -f $(DISTNAME)
	lzip -v -9 $(DISTNAME).tar

clean :
	-rm -f $(programs) $(scripts) $(objs)

distclean : clean
	-rm -f Makefile config.status *.tar *.tar.lz