# File: Makefile for bgscripts
# Location: bgscripts source package
# Author: bgstack15
# Startdate: 2018-11-24
# Title: Makefile for bgscripts source package
# Purpose: To use traditional Unix make utility
# History:
#    2019-01-07 add with_pyver=2
#    2019-01-31 fix #24 list-vnc-sessions.sh needs to be in -core package, fix uninstall uvlib.py symlink
#    2020-03-05 add defaultdir, sbindir for monitor-resize
#    2022-05-03 fix #47 add stackrpms-nfs init script
#    2022-10-05 fix #50 grep warning \/
# Usage:
# Reference:
#    https://stackoverflow.com/questions/4219255/how-do-you-get-the-list-of-targets-in-a-makefile/26339924#26339924
#    https://stackoverflow.com/questions/19105241/how-do-you-conditionally-call-a-target-based-on-a-target-variable-makefile/19107231#19107231
#    https://stackoverflow.com/questions/5917576/sort-a-text-file-by-line-length-including-spaces
#    https://superuser.com/questions/352289/bash-scripting-test-for-empty-directory/667100#667100
# Improve:
# Document:
# Dependencies:
#    build-devuan: txt2man

APPNAME    = bgscripts
APPVERSION = 1.5.9
SRCDIR     = $(CURDIR)
prefix     = /usr
SYSCONFDIR = $(DESTDIR)/etc
DEFAULTDIR = $(DESTDIR)/etc/sysconfig # for debian use '$(DESTDIR)/etc/default'
BINDIR     = $(DESTDIR)$(prefix)/bin
LIBEXECDIR = $(DESTDIR)$(prefix)/libexec
SBINDIR    = $(DESTDIR)$(prefix)/sbin
SHAREDIR   = $(DESTDIR)$(prefix)/share
DOCDIR     = $(SHAREDIR)/doc/$(APPNAME)
APPDIR     = $(SHAREDIR)/$(APPNAME)
APPSDIR    = $(SHAREDIR)/applications
ICONSDIR   = $(SHAREDIR)/icons
MIMEDIR    = $(SHAREDIR)/mime
MANDIR     = $(SHAREDIR)/man
XDGAUTODIR = $(SYSCONFDIR)/xdg/autostart
SYSVDIR    = $(SYSCONFDIR)/init.d
SYSDDIR    = $(DESTDIR)$(prefix)/lib/systemd/system

# variables for deplist
DEPTYPE = dep
SEPARATOR = ,

awkbin     :=$(shell which awk)
chmodbin   :=$(shell which chmod)
cpbin      :=$(shell which cp)
echobin    :=$(shell which echo)
falsebin   :=$(shell which false)
findbin    :=$(shell which find)
grepbin    :=$(shell which grep)
gzipbin    :=$(shell which gzip)
installbin :=$(shell which install)
rmbin      :=$(shell which rm)
rmdirbin   :=$(shell which rmdir)
sedbin     :=$(shell which sed)
sortbin    :=$(shell which sort)
truebin    :=$(shell which true)
txt2manwrapper :=usr/bin/txt2man-wrapper
uniqbin    :=$(shell which uniq)
xargsbin   :=$(shell which xargs)

with_man ?= YES
with_sysv_monitor_resize ?= YES
with_sysd_monitor_resize ?= NO
with_sysd_dnskeepalive ?= NO
with_sysv_stackrpms_nfs ?= NO
# Warning! This service file does not exist because it is not needed.
with_sysd_stackrpms_nfs ?= NO

all: build_man

ifeq ($(with_man),YES)
install: build_man install_files
else
install: install_files
endif

.PHONY: clean install install_files build_man uninstall list deplist deplist_opts

list:
	@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | ${awkbin} -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | ${sortbin} | ${grepbin} -E -v -e '^[^[:alnum:]]' -e '^$@$$'

deplist:
	@# deplist 2020-04-18 input must be comma separated
	@# DEPTYPE( dep , rec , sug ) for depends, recommends, or suggests
	@if test -z "${DISTRO}" ; then ${echobin} "Please run \`make deplist\` with DISTRO= one of: `make deplist_opts 2>&1 1>/dev/null | ${xargsbin}`. Aborted." 1>&2 ; exit 1 ; fi
	@if ! ${echobin} "${DEPTYPE}" | grep -qE "^(dep|rec|sug)$$" ; then ${echobin} "Please run \`make deplist\` with DEPTYPE= one of: dep, rec, sug. Undefined will use \`dep\`. Aborted." 1>&2 ; exit 1; fi
	@${grepbin} -h --exclude-dir='doc' -riIE "\<${DEPTYPE}-" ${SRCDIR} | ${awkbin} -v "domain=${DISTRO}" -v "deptype=${DEPTYPE}" 'tolower($$2) ~ deptype"-"domain {$$1="";$$2="";print}' | tr ',' '\n' | ${sortbin} | ${uniqbin} | ${sedbin} -r -e 's/^\s*//' -e "s/\s*\$$/${SEPARATOR}/" | ${xargsbin}

deplist_opts:
	@# deplist_opts 2020-04-18 find all available dependency domains
	@${grepbin} -h -o -riIE '\<(dep|rec|sug)-[^\ :]+:' ${SRCDIR} | ${sedbin} -r -e 's/(dep|rec|sug)-//;' -e 's/:$$//;' | ${sortbin} | ${uniqbin} 1>&2

install_files: 
	@ls usr/share/man/man*/*gz 1>/dev/null 2>&1 && echo "Including man pages." || :
	@${echobin} Installing files to ${DESTDIR}
	for td in $$( ${findbin} ${SRCDIR} -type d ! -name '.*.swp' ! -name 'Makefile' -printf '%P\n' | ${sedbin} -r -e "s:etc/sysconfig:${DEFAULTDIR}:" -e "s:${DESTDIR}/?::" ) ; do ${installbin} -m0755 -d ${DESTDIR}/$${td} ; done
	for tf in $$( ${findbin} ${SRCDIR} ! -type d ! -name '.*.swp' ! -name 'Makefile' ! \( -path '*/man/*' -name '*.txt' \) ! -path '*/sysconfig/*' -printf '%P\n' ) ; do MODE=0644 ; echo "$${tf}" | grep -qE "(bin|libexec|deprecated)/" && MODE=0755 ; ${installbin} -m$${MODE} ${SRCDIR}/$${tf} ${DESTDIR}/$${tf} ; done
	@# sysconfig/default dir
	${installbin} -m0644 -t ${DEFAULTDIR} ${SRCDIR}/etc/sysconfig/*
ifeq ($(with_sysv_monitor_resize),YES)
	@${echobin} installing service monitor-resize
	${installbin} -m0755 -d ${SYSVDIR}
	${installbin} -m0755 -t ${SYSVDIR} ${SRCDIR}/usr/share/bgscripts/sysvinit/monitor-resize
endif
ifeq ($(with_sysd_monitor_resize),YES)
	@${echobin} installing systemd unit monitor-resize
	${installbin} -m0755 -d ${SYSDDIR}
	${installbin} -m0644 -t ${SYSDDIR} ${SRCDIR}/usr/share/bgscripts/systemd/monitor-resize.service
endif
ifeq ($(with_sysv_stackrpms_nfs),YES)
	@${echobin} installing stackrpms-nfs init script
	${installbin} -m0755 -d ${SYSVDIR}
	${installbin} -m0755 -t ${SYSVDIR} ${SRCDIR}/usr/share/bgscripts/sysvinit/stackrpms-nfs
endif
ifeq ($(with_sysd_stackrpms_nfs),YES)
	@${echobin} "Info: with_sysd_stackrpms_nfs is not implemented."
	@#@${echobin} installing stackrpms-nfs systemd unit
	@#${installbin} -m0755 -d ${SYSDDIR}
	@#${installbin} -m0644 -t ${SYSDDIR} ${SRCDIR}/usr/share/bgscripts/systemd/stackrpms-nfs.service
endif

MAN_TXT:=$(wildcard usr/share/man/man*/*.txt)
MAN_GZ:= $(subst .txt,.gz,$(MAN_TXT))

build_man: $(MAN_GZ)

$(MAN_GZ): %.gz: %.txt
	${txt2manwrapper} - < $< | ${gzipbin} > $@

uninstall:
	@${echobin} SRCDIR=${SRCDIR}
	${rmbin} -f $$( ${findbin} ${SRCDIR} -mindepth 1 ! -type d -printf '%p\n' | ${sedbin} -r -e "s:/etc/sysconfig:${DEFAULTDIR}:" -e "s:^${SRCDIR}:${DESTDIR}:" -e "s:${DESTDIR}${DESTDIR}:${DESTDIR}:" )
ifeq ($(with_sysv_monitor_resize),YES)
	${rmbin} ${SYSVDIR}/monitor-resize || :
endif
ifeq ($(with_sysd_monitor_resize),YES)
	${rmbin} ${SYSDDIR}/monitor-resize.service || :
endif
ifeq ($(with_sysv_stackrpms_nfs),YES)
	${rmbin} ${SYSVDIR}/stackrpms-nfs || :
endif

	# remove all installed directories that are now blank.
	${rmdirbin} ${DEFAULTDIR} ${SYSVDIR} 2>/dev/null || : ; for word in $$( ${findbin} ${SRCDIR} -mindepth 1 -type d -printf '%p\n' | ${sedbin} -r -e "s:^${SRCDIR}:${DESTDIR}:" | ${awkbin} '{ print length, $$0 }' | ${sortbin} -rn | ${awkbin} '{print $$2}' ) ; do ${findbin} $${word} -mindepth 1 1>/dev/null 2>&1 | read 1>/dev/null 2>&1 || { ${rmdirbin} "$${word}" 2>/dev/null || ${truebin} ; } ; done

clean:
	-@#${echobin} "target $@ not implemented yet! Gotta say unh." && ${falsebin}
	-${rmbin} -f usr/share/man/man*/*.gz || :
