# Copyright 2007, 2009, 2010, 2011, 2018 Kevin Ryde.

# This file is part of pngtextadd.
#
# pngtextadd 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, or (at your option) any later
# version.
#
# pngtextadd 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 pngtextadd; see the file COPYING.  Failing that, goto
# <http://www.gnu.org/licenses/>.


VERSION = 4

# override these on the make command line if desired
prefix = /usr/local
mandir = $(prefix)/share/man
DESTDIR =

# or recent libpng uses pkg-config too
OPTIMIZE = -O2 -Wall
CFLAGS = $(OPTIMIZE) `libpng-config --cflags`
LOADLIBES = `libpng-config --libs`

all: pngtextadd

pngtextadd: pngtextadd.c Makefile
	$(CC) $(CFLAGS) `libpng-config --cflags` pngtextadd.c \
	  $(LDFLAGS) $(LOADLIBES) -o pngtextadd

clean distclean:
	rm -f pngtextadd

install: pngtextadd
	install -m 755 -d           $(DESTDIR)$(prefix)/bin
	install -m 755 pngtextadd   $(DESTDIR)$(prefix)/bin/pngtextadd
	install -m 755 -d           $(DESTDIR)$(mandir)/man1
	install -m 644 pngtextadd.1 $(DESTDIR)$(mandir)/man1/pngtextadd.1


#------------------------------------------------------------------------------
# dist

version:
	@echo $(VERSION)

DISTNAME = pngtextadd
DISTVNAME = pngtextadd-$(VERSION)
DEBNAME = pngtextadd
DEBFILE = $(DEBNAME)_$(VERSION)-1_i386.deb
echo-DISTNAME:
	@echo $(DISTNAME)
echo-DISTVNAME:
	@echo $(DISTVNAME)

DISTFILES = COPYING NEWS Makefile pngtextadd.c pngtextadd.1 \
  debian/changelog debian/compat debian/control debian/copyright \
  debian/rules \
  debian/source/format \
  debian/watch

distdir:
	rm -rf $(DISTVNAME)
	mkdir $(DISTVNAME)
	mkdir $(DISTVNAME)/debian
	mkdir $(DISTVNAME)/debian/source
	set -e; for i in $(DISTFILES); do \
	  cp -p $$i $(DISTVNAME)/$$i; \
	done
$(DISTVNAME).tar.gz: $(DISTFILES)
	$(MAKE) -$(MAKEFLAGS) distdir
	tar cf - --sort=name $(DISTVNAME) |gzip -9 >$(DISTVNAME).tar.gz
	rm -rf $(DISTVNAME)
dist: $(DISTVNAME).tar.gz

# man page as html
# "-P -l" suppresses table of contents at the start
pngtextadd.html: pngtextadd.1
	groff -man -Thtml -P -l pngtextadd.1 >pngtextadd.html || rm -f pngtextadd.html

deb: $(DEBFILE)
$(DEBFILE): $(DISTVNAME).tar.gz
	rm -rf $(DISTVNAME)
	tar xfz $(DISTVNAME).tar.gz \
	  && cd $(DISTVNAME) \
	  && dpkg-checkbuilddeps debian/control \
	  && fakeroot debian/rules binary
	rm -rf $(DISTVNAME)

#------------------------------------------------------------------------------
# development

# find files in the dist with mod times this year, but without this year in
# the copyright line
check-copyright-years:
	year=`date +%Y`; \
	tar tvfz $(DISTVNAME).tar.gz \
	| egrep "$$year-|debian/copyright" \
	| sed 's:^.*$(DISTVNAME)/::' \
	| (result=0; \
	  while read i; do \
	    case $$i in \
	      '' | */ | COPYING \
	      | debian/compat | debian/changelog) \
	      continue ;; \
	    esac; \
	    if test -e "$(srcdir)/$$i"; then f="$(srcdir)/$$i"; \
	    else f="$$i"; fi; \
	    if ! grep -q "Copyright.*$$year" $$f; then \
	      echo "$$i:1: this file"; \
	      grep Copyright $$f; \
	      result=1; \
	    fi; \
	  done; \
	  exit $$result)

# only a non-zero number is bad, allow an expression to copy a debug from
# another package
check-debug-constants:
	if egrep -n 'DEBUG *(=>)? *[1-9]' $(DISTFILES); then exit 1; else exit 0; fi

diff-prev:
	rm -rf /tmp/$(DISTNAME)--diff-prev
	mkdir /tmp/$(DISTNAME)--diff-prev
	tar -C /tmp/$(DISTNAME)--diff-prev -xz -f $(DISTVNAME).tar.gz
	tar -C /tmp/$(DISTNAME)--diff-prev -xz -f $(DISTNAME)-`expr $(VERSION) - 1`.tar.gz
	cd /tmp/$(DISTNAME)--diff-prev; \
	  diff -Naur $(DISTNAME)-`expr $(VERSION) - 1` $(DISTNAME)-$(VERSION) \
	  | less

lintian-source:
	rm -rf temp-lintian; \
	mkdir temp-lintian; \
	cd temp-lintian; \
	cp ../$(DISTVNAME).tar.gz $(DISTNAME)_$(VERSION).orig.tar.gz; \
	tar xfz $(DISTNAME)_$(VERSION).orig.tar.gz; \
	echo 'empty-debian-diff' \
	     >$(DISTVNAME)/debian/source.lintian-overrides; \
	dpkg-source -b $(DISTNAME)-$(VERSION) $(DISTNAME)_$(VERSION).orig.tar.gz; \
	lintian -i *.dsc; \
	cd ..; \
	rm -rf temp-lintian
lintian-deb: $(DEBFILE)
	lintian -I -i --suppress-tags new-package-should-close-itp-bug $(DEBFILE)

my-list:
	make pngtextadd.html
	ls -l -U pngtextadd.c pngtextadd.1 *.tar.gz *.deb *.html
	@echo -n '$(DEBFILE) '
	@dpkg-deb -f $(DEBFILE) | grep Installed-Size 
	rm pngtextadd.html

.PHONY: all clean distclean install deb diff-prev dist distdir lintian-deb lintian-source version my-list
