appmandir = $(APP_MAN_DIR)
#appman_PRE = list of application man page files set by calling Makefile.am
appman_DATA = $(appman_PRE:man=$(APP_MAN_SUFFIX))

drivermandir = $(DRIVER_MAN_DIR)
#driverman_PRE = list of driver man page files set by calling Makefile.am
driverman_DATA = $(driverman_PRE:man=$(DRIVER_MAN_SUFFIX))

filemandir = $(FILE_MAN_DIR)
#fileman_PRE = list of file man page files set by calling Makefile.am
fileman_DATA = $(fileman_PRE:man=$(FILE_MAN_SUFFIX))

# The calling Makefile should only contain man page targets
# Otherwise the following three global variables may conflict
EXTRA_DIST = $(appman_PRE) $(driverman_PRE) $(fileman_PRE)
CLEANFILES = $(appman_DATA) $(driverman_DATA) $(fileman_DATA)
SUFFIXES = .$(APP_MAN_SUFFIX) .$(DRIVER_MAN_SUFFIX) .$(FILE_MAN_SUFFIX) .man

# xorg-macros.m4 has these bracketed by double underscores, but meson
# wants ats.

MAN_SUBSTS +=   -e 's|@vendorversion@|"$(PACKAGE_STRING)" "$(XORG_MAN_PAGE)"|' \
                -e 's|@xorgversion@|"$(PACKAGE_STRING)" "$(XORG_MAN_PAGE)"|' \
                -e 's|@xservername@|Xorg|g' \
                -e 's|@xconfigfile@|xorg.conf|g' \
                -e 's|@projectroot@|$(prefix)|g' \
                -e 's|@apploaddir@|$(appdefaultdir)|g' \
                -e 's|@appmansuffix@|$(APP_MAN_SUFFIX)|g' \
                -e 's|@drivermansuffix@|$(DRIVER_MAN_SUFFIX)|g' \
                -e 's|@adminmansuffix@|$(ADMIN_MAN_SUFFIX)|g' \
                -e 's|@libmansuffix@|$(LIB_MAN_SUFFIX)|g' \
                -e 's|@miscmansuffix@|$(MISC_MAN_SUFFIX)|g' \
                -e 's|@filemansuffix@|$(FILE_MAN_SUFFIX)|g'

# Add server specific man pages string substitution from XORG_MANPAGE_SECTIONS
# 's|/,|/, |g' will add a space to help font path formatting
MAN_SUBSTS += 	-e 's|[@]logdir[@]|$(logdir)|g' \
		-e 's|[@]datadir[@]|$(datadir)|g' \
		-e 's|[@]mandir[@]|$(mandir)|g' \
		-e 's|[@]sysconfdir[@]|$(sysconfdir)|g' \
		-e 's|[@]xconfigdir[@]|$(XCONFIGDIR)|g' \
		-e 's|[@]xkbdir[@]|$(XKB_BASE_DIRECTORY)|g' \
		-e 's|[@]XKB_DFLT_RULES[@]|$(XKB_DFLT_RULES)|g' \
		-e 's|[@]XKB_DFLT_MODEL[@]|$(XKB_DFLT_MODEL)|g' \
		-e 's|[@]XKB_DFLT_LAYOUT[@]|$(XKB_DFLT_LAYOUT)|g' \
		-e 's|[@]XKB_DFLT_VARIANT[@]|$(XKB_DFLT_VARIANT)|g' \
		-e 's|[@]XKB_DFLT_OPTIONS[@]|$(XKB_DFLT_OPTIONS)|g' \
		-e 's|[@]bundle_id_prefix[@]|$(BUNDLE_ID_PREFIX)|g' \
		-e 's|[@]modulepath[@]|$(DEFAULT_MODULE_PATH)|g' \
		-e 's|[@]suid_wrapper_dir[@]|$(SUID_WRAPPER_DIR)|g' \
		-e 's|[@]default_font_path[@]|$(COMPILEDDEFAULTFONTPATH)|g' \
		-e '\|$(COMPILEDDEFAULTFONTPATH)| s|/,|/, |g'

.man.$(APP_MAN_SUFFIX):
	$(AM_V_GEN)$(SED) $(MAN_SUBSTS) < $< > $@
.man.$(DRIVER_MAN_SUFFIX):
	$(AM_V_GEN)$(SED) $(MAN_SUBSTS) < $< > $@
.man.$(FILE_MAN_SUFFIX):
	$(AM_V_GEN)$(SED) $(MAN_SUBSTS) < $< > $@
