# SPDX-FileCopyrightText: 2026 Didier Kryn <kryn@in2p3.fr>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Declare program name and installation location
# PGNAME sets the name of the following files: the excutable, the binary
# translation files and the manual pages, but it does not change the program
# name as written *inside* the man pages. This one can only be changed by
# editing all the files in subdirectory man.
export PGNAME := qmount
export INSTALL_ROOT := /usr/local

VERSION  := 1
COPYRIGHT_HOLDER := "Didier Kryn"

PACKAGE := '"'$(PGNAME)'"'
INSTALL_EXEC  := $(INSTALL_ROOT)/bin

SYSFUNS := canonpath devpath get_device_prop lookup_disk osuse lsmtab
FUNS    := main dmount install_target fsinfo

SYSSRCS := $(patsubst %, %.c, $(SYSFUNS))
SRCS    := $(patsubst %, %.c, $(FUNS))

VPATH  := slash_sys strlcpy po

# Add security protections:
# -fstack-protector-all: Protects against stack overflows.
# -D_FORTIFY_SOURCE=2: Replaces dangerous functions with secure versions at runtime.
CFLAGS := -O2 -Islash_sys -Istrlcpy -D PACKAGE=$(PACKAGE) \
          $(shell pkg-config --cflags libcap) \
          -fstack-protector-all -D_FORTIFY_SOURCE=2 \
          -Wformat -Wformat-security -Werror=format-security

# -Wl,-z,now -Wl,-z,relro: Protect the binary symbol table (ReadOnly Relocations).
LDFLAGS := -Wl,-z,relro -Wl,-z,now

########################## Make it all ############################
all: $(PGNAME)
	$(MAKE) po

######################### The executable ##########################
$(PGNAME): $(SRCS) strLcpy.c $(SYSSRCS) | strLcpy.h slash_sys.h
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lblkid -lcap

################# The messages translation files ##################
po:
	$(MAKE) -C po

install: $(PGNAME)
	# We changed 04755 to 0755 to eliminate the SUID bit risk
	sudo install -o root -m 0755 $(PGNAME) $(INSTALL_EXEC)
	# We assign granular capabilities to the installed binary
	sudo setcap cap_sys_admin,cap_dac_override,cap_chown+p $(INSTALL_EXEC)/$(PGNAME)
	$(MAKE) -C po install
	$(MAKE) -C man install

cleanall:
	@rm -fv *.o $(PGNAME)
	$(MAKE) -C po cleanall

uninstall:
	sudo rm -fv /usr/local/bin/$(PGNAME)
	$(MAKE) -C po uninstall
	$(MAKE) -C man uninstall

debug:
	@echo "PACKAGE=" $(PACKAGE)
	@echo "CFLAGS=" $(CFLAGS)
	@echo "SYSOBJS=" $(SYSOBJS)
	@echo "OBJS=" $(OBJS)
	$(MAKE) -C po debug
	$(MAKE) -C man debug

.PHONY: install cleanall uninstall debug po

#---------------------------------------------------------------------------
# Create pot file. Beware that you must then edit some fields afterwards,
# notably SPDX license fields, but some others as well
# If you are sure, then 'make po/messages.pot'
XGETTEXT_FLAGS := -C -F --copyright-holder=$(COPYRIGHT_HOLDER) \
                  --package-name=$(PGNAME) --package-version=$(VERSION) \
                  --from-code=UTF-8 '--add-comments=Translators:' '-k_' \
                  --foreign-user
po/messages.pot: main.c dmount.c install_target.c
	xgettext $(XGETTEXT_FLAGS) '--output-dir=po' $^
#---------------------------------------------------------------------------
