# We don't want to touch the installed files, if the file content
# wasn't changed. 'install -C' is not sufficcient, because
# it touches the destination files even if they are not copied.
#
# If we did an install based on 'cmp' in the Makefile, the Makefile
# would get quite bloated. So instead we just call an install script.
#
# GNU default for prefix is /usr/local but we want to put some tools
# into /, some into /usr and some into /usr/local.
#
# This repository is not expected to be portable. So we take the
# freedom to set the default prefix to empty and interpret this to
# use different prefixes for individual tools. If prefix is overwritten
# in the command line, all tools will go into the same tree. Alternativly,
# root_prefix, usr_prefix and usrlocal_prefix may be overwritten on
# the command line to individual values.
#
#
# We honor the following conventional make variables
# and pass them via environment to the install script:
#   prefix
#   root_prefix usr_prefix usrlocal_prefix
#   root_exec_prefix root_bindir root_sbindir
#   usr_exec_prefix usr_bindir usr_sbindir
#   usrlocal_exec_prefix usrlocal_bindir usrlocal_sbindir
#   sysconfdir systemdunitdir
#   udev_rulesdir udev_helperdir
#   srcdir
#   INSTALL INSTALL_PROGRAM INSTALL_DATA
#   DESTDIR

prefix=
ifeq "$(prefix)" ""
root_prefix=
usr_prefix=/usr
usrlocal_prefix=/usr/local
else
root_prefix=$(prefix)
usr_prefix=$(prefix)
usrlocal_prefix=$(prefix)
endif

root_exec_prefix=$(root_prefix)
root_bindir=$(root_exec_prefix)/bin
root_sbindir=$(root_exec_prefix)/sbin

usr_exec_prefix=$(usr_prefix)
usr_bindir=$(usr_exec_prefix)/bin
usr_sbindir=$(usr_exec_prefix)/sbin

usrlocal_exec_prefix=$(usrlocal_prefix)
usrlocal_bindir=$(usrlocal_exec_prefix)/bin
usrlocal_sbindir=$(usrlocal_exec_prefix)/sbin

sysconfdir=$(prefix)/etc

systemdunitdir=$(sysconfdir)/systemd/system
udev_rulesdir=$(sysconfdir)/udev/rules.d
udev_helperdir=$(prefix)/lib/udev

srcdir=.

INSTALL=install -v
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644

manpages += prun/prun.1 prun/pman.1 prun/ptype.1

all: $(manpages)

clean:
	@rm -f $(manpages)

install: all
	@prefix="$(prefix)" usr_prefix="$(usr_prefix)" usrlocal_prefix="$(usrlocal_prefix)" \
	root_exec_prefix="$(root_exec_prefix)"         root_bindir="$(root_bindir)"    root_sbindir="$(root_sbindir)" \
	usr_exec_prefix="$(usr_exec_prefix)"           usr_bindir="$(usr_bindir)"      usr_sbindir="$(usr_sbindir)" \
	usr_mandir="$(usr_mandir)" \
	usrlocal_exec_prefix="$(usrlocal_exec_prefix)" usrlocal_bindir="$(usrlocal_bindir)" usrlocal_sbindir="$(usrlocal_sbindir)" \
	sysconfdir="$(sysconfdir)"                     systemdunitdir="$(systemdunitdir)" \
	udev_rulesdir="$(udev_rulesdir)"               udev_helperdir="$(udev_helperdir)" \
	srcdir="$(srcdir)" \
	INSTALL="$(INSTALL)" INSTALL_PROGRAM="$(INSTALL_PROGRAM)" INSTALL_DATA="$(INSTALL_DATA)" \
	DESTDIR="$(DESTDIR)" \
	./install.sh

.PHONY: all install clean

%.1: %.md
	@if [ "$$UID" = 0 ]; then echo "Please build (\`make\`) as non-root before running \`make install\` as root" >&2;exit 1;fi
	pandoc --standalone --to man $< -o $@