prefix    = $(DESTDIR)
package   = ucf
version   = $(shell dpkg-parsechangelog -Sversion)

ETCDIR    = $(prefix)/etc
BINDIR    = $(prefix)/usr/bin
USHAREDIR = $(prefix)/usr/share/$(package)
DEBLIBDIR = $(prefix)/var/lib/$(package)
DEBDOCDIR = $(prefix)/usr/share/doc/$(package)
MANDIR    = $(prefix)/usr/share/man/
MAN1DIR   = $(MANDIR)/man1
MAN5DIR   = $(MANDIR)/man5

# install commands
install_file    := /usr/bin/install -p    -o root -g root -m 644
install_program := /usr/bin/install -p    -o root -g root -m 755
make_directory  := /usr/bin/install -p -d -o root -g root -m 755

all: build check

build:
	sed -i "s#^pversion='.*'\$$#pversion='$(version)'#" ucf ucfr

check:
	sh -n  ucf
	sh -n  ucfr
	perl -wc ucfq
	sh -n  debian/ucf.preinst
	sh -n  debian/ucf.postinst
	sh -n  debian/ucf.postrm

	./t/run

shellcheck:
	# Ignored:
	#  SC3043 (warning): In POSIX sh, 'local' is undefined.
	#  SC2317 (info): Command appears to be unreachable. Check usage (or ignore if invoked indirectly).
	#  SC2004 (style): $$/$${} is unnecessary on arithmetic variables.

	shellcheck  --external-sources --exclude 3043,2317,2004 ucf ucfr

install:
	$(make_directory)   $(BINDIR)
	$(make_directory)   $(ETCDIR)
	$(make_directory)   $(MAN1DIR)
	$(make_directory)   $(MAN5DIR)
	$(make_directory)   $(DEBLIBDIR)
	$(make_directory)   $(USHAREDIR)
	$(make_directory)   $(DEBDOCDIR)/examples
	$(install_program)  ucf               $(BINDIR)
	$(install_file)     ucf.1             $(MAN1DIR)
	gzip -9fq           $(MAN1DIR)/ucf.1
	$(install_program)  ucfr              $(BINDIR)
	$(install_file)     ucfr.1            $(MAN1DIR)
	gzip -9fq           $(MAN1DIR)/ucfr.1
	$(install_program)  ucfq              $(BINDIR)
	$(install_file)     ucfq.1            $(MAN1DIR)
	gzip -9fq           $(MAN1DIR)/ucfq.1
	$(install_file)     ucf.conf.5        $(MAN5DIR)
	gzip -9fq           $(MAN5DIR)/ucf.conf.5
	$(install_file)     ucf_library.sh    $(USHAREDIR)
	$(install_file)     ucf.conf          $(ETCDIR)
	$(install_file)     debian/changelog  $(DEBDOCDIR)/changelog
	gzip -9frq          $(DEBDOCDIR)
# make sure the copyright file is not compressed
	$(install_file)     debian/copyright  $(DEBDOCDIR)/copyright
	$(install_file)     examples/postinst $(DEBDOCDIR)/examples/
	$(install_file)     examples/postrm   $(DEBDOCDIR)/examples/

clean distclean:
	@echo nothing to do for clean
