#	$Makronix$
#
# MCDE, the Makronix fork of CDE 2.5.3 (ext/src/mcde/IMPORT).  The source is
# ext/src/mcde in this repository -- the tree git tracks and a person edits, not
# an archive: no DISTFILES, no distinfo, and do-extract fills WRKSRC (the same
# way net/fetch and pkgtools/pkg_install build from their files/).  The recipe
# started as pkgsrc's wm/cde; the differences are MCDE's.
DISTNAME=	mcde-0.1.0
CATEGORIES=	wm x11
MASTER_SITES=	# empty
DISTFILES=	# empty

# Three levels above the directory pkgsrc builds in: the development tree, /mnt
# inside the build chroot and /usr/src on an installed system.  'pwd -P' because
# pkgsrc refuses a WRKDIR path that is not canonical and the mirror in pkgsrc/
# is reached through a symlink (tools/ext-sync.sh).
_MAKRONIX_TOP!=	cd ${.CURDIR}/../../.. && pwd -P
MCDESRC=	${_MAKRONIX_TOP}/ext/src/mcde

# The copy is made by a script, not by ${CP} -R: pkgsrc extracts once, and
# a commit after that never reached the work directory (tools/ext-extract.sh).
do-extract:
	${RUN}${SH} ${_MAKRONIX_TOP}/tools/ext-extract.sh ${MCDESRC} ${WRKSRC}

MAINTAINER=	konrad.lother@gmail.com
COMMENT=	Makronix Common Desktop Environment, a fork of CDE
LICENSE=	gnu-lgpl-v2

# MCDE installs the same files as CDE (${PREFIX}/dt): one or the other.
CONFLICTS+=	cde-[0-9]*

USE_LANGUAGES=	c c++
USE_LIBTOOL=	yes
USE_TOOLS+=	aclocal autoconf automake autoreconf
USE_TOOLS+=	awk bdftopcf cmp gettext gmake gzip ksh
USE_TOOLS+=	lex mkfontdir patch perl pkg-config yacc

MAKE_JOBS_SAFE=		no

PTHREAD_AUTO_VARS=		yes
BUILDLINK_TRANSFORM+=		rm:-pthread
BUILDLINK_PASSTHRU_DIRS+=	${PREFIX}/dt/lib

.include "../../mk/bsd.fast.prefs.mk"

DEPENDS+=	ksh93-[0-9]*:../../shells/ksh93
DEPENDS+=	font-adobe-75dpi-[0-9]*:../../fonts/font-adobe-75dpi
DEPENDS+=	font-adobe-100dpi-[0-9]*:../../fonts/font-adobe-100dpi
.if ${X11_TYPE} == "modular"
DEPENDS+=	xrdb-[0-9]*:../../x11/xrdb
DEPENDS+=	xset-[0-9]*:../../x11/xset
DEPENDS+=	sessreg-[0-9]*:../../x11/sessreg
.endif
TOOL_DEPENDS+=	gettext-m4-[0-9]*:../../devel/gettext-m4

GNU_CONFIGURE=		yes
GNU_CONFIGURE_PREFIX=	${PREFIX}/dt
CONFIGURE_ENV+=		ac_cv_path_KSH=${PREFIX}/bin/ksh93
# configure.ac: AC_PATH_PROG(XRDB, xrdb) finds the BUILD host's xrdb in a cross
# build (config.log: ac_cv_path_XRDB='/usr/bin/xrdb') and dtlogin, dtsession and
# dtimsstart run that path on the target.  NetBSD has no /usr/bin/xrdb, so
# dtlogin could not load its Xresources file: the CDE logo, colours and fonts
# showed instead of Makronix's (Konrad, 2026-11-17).  The target's xrdb is the
# X11 base's.
CONFIGURE_ENV+=		ac_cv_path_XRDB=${X11BASE}/bin/xrdb
# configure.ac: AC_CHECK_PROGS(SESSREG, sessreg) records the bare name (this
# build host has /usr/bin/sessreg), and programs/dtlogin/config/Xstartup ends
# with `exec sessreg': on NetBSD sessreg is in the X11 base, the login found
# no such program and went back to the greeter (2026-11-18, Xerrors).
CONFIGURE_ENV+=		ac_cv_prog_SESSREG=${X11BASE}/bin/sessreg
# The message catalogues must be in NetBSD's format (catopen(3) wants the
# magic 0xff88ff89).  configure found the build host's gencat; on Linux that
# writes glibc catalogues (magic 0x960408de) and every catgets() fell back to
# the built-in English text -- no translation ever showed (2026-11-18).  The
# NetBSD tooldir has nbgencat; a native NetBSD host's /usr/bin/gencat is right.
.if !empty(TOOLDIR:U) && exists(${TOOLDIR}/bin/nbgencat)
CONFIGURE_ENV+=		ac_cv_prog_GENCAT=${TOOLDIR}/bin/nbgencat
.endif
# configure.ac: AC_PATH_PROG(GO, go) finds the BUILD host's go, and
# programs/dtchat then builds the WhatsApp adapter with it: a host binary
# (no GOOS for the target), from modules that pkgsrc's build phase may not
# download (bsd.pkg.mk sets http_proxy=downloads-forbidden-except-during-fetch;
# build/logs/userland-2026-11-17c.log:19467).  The adapter is a package of its
# own (ext/src/mcde/AGENTS.md, dtchat), so PLIST.whatsapp stays unset and
# this package is built without Go.
CONFIGURE_ENV+=		ac_cv_path_GO=no
CONFIGURE_ENV+=		GZIP=${GZIP_CMD:Q}
CONFIGURE_ARGS+=	MAKE=${MAKE_PROGRAM:Q}
CONFIGURE_ARGS+=	--with-x
# tclConfig.sh is read by configure on the build host: in a cross build it is
# in the sysroot, as x11/tk90 does it (_CROSS_DESTDIR is empty when native)
CONFIGURE_ARGS+=	--with-tcl=${_CROSS_DESTDIR:U}${BUILDLINK_PREFIX.tcl}/lib
CONFIGURE_ARGS+=	--sysconfdir=${PKG_SYSCONFDIR}/dt
CONFIGURE_ARGS+=	--with-utmp-file=${VARBASE}/run/utmp
CONFIGURE_ARGS+=	--with-wtmp-file=${VARBASE}/log/wtmp
# MCDE: these were placeholders patched into configure.ac by pkgsrc
CONFIGURE_ARGS+=	--with-logfiles-dir=${VARBASE}/dt
CONFIGURE_ARGS+=	--with-config-dir=${PKG_SYSCONFDIR}/dt
CONFIGURE_ARGS+=	--with-local-bindir=${PREFIX}/bin
# where McdeSys finds the programs pkgsrc installed (t0081): no fixed /usr/pkg
CONFIGURE_ARGS+=	--with-pkg-prefix=${PREFIX}
CONFIGURE_ARGS+=	--with-pkg-sysconfdir=${PKG_SYSCONFDIR}
# MCDE: Motif is the system's (motif.buildlink3.mk below); MCDE builds its
# own motif/ only outside pkgsrc (configure.ac, --with-private-motif;
# Konrad, 2026-11-13: "mcde baut sein eigenes motif, wenn es nicht
# makronix ist")
CONFIGURE_ARGS+=	--without-private-motif

.include "options.mk"

REPLACE_PERL+=	programs/dtinfo/tools/bin/ccdate
# (no REPLACE_AWK: Xsession.src is a ksh script that calls /usr/bin/awk by
# its path, which NetBSD and Linux have; REPLACE_AWK changes #! lines only,
# and awk:run kept the package from being cross-built on a Linux host)

REPLACE_INTERPRETER+=	tclsh
REPLACE.tclsh.old=	.*/bin/tclsh
REPLACE.tclsh.new=	${PREFIX}/bin/tclsh
REPLACE_FILES.tclsh+=	programs/dtdocbook/tcl/docbook.tcl

CHECK_BUILTIN.iconv:=   yes
.include "../../converters/libiconv/builtin.mk"
CHECK_BUILTIN.iconv:=   no
.if ${USE_BUILTIN.iconv:tl} == no
SUBST_CLASSES+=		iconv
SUBST_STAGE.iconv=	pre-configure
SUBST_FILES.iconv+=	programs/dtmail/libDtMail/Common/Session.C
SUBST_FILES.iconv+=	programs/dtmail/libDtMail/RFC/RFCBodyPart.C
SUBST_SED.iconv=	-e 's!^[[:space:]]*const char .ip = (const char .) .bp!char *ip = *bp!'
SUBST_MESSAGE.iconv=	Fix for GNU iconv.
.endif

SUBST_CLASSES+=		paths
SUBST_STAGE.paths=	pre-configure
SUBST_FILES.paths+=	pkgsrc/cde.desktop pkgsrc/startcde
SUBST_FILES.paths+=	pkgsrc/README.*
SUBST_VARS.paths=	PKG_SYSCONFDIR PREFIX REAL_ROOT_GROUP VARBASE X11BASE
SUBST_MESSAGE.paths=	Replacing path placeholders.

MAKE_ENV+=	x_includes=${X11BASE}/include
MAKE_FLAGS+=	YACC=${YACC:Q}
MAKE_FLAGS+=	PERL=${PERL5:Q}
MAKE_FLAGS+=	LDFLAGS=${LDFLAGS:M*:Q}

# MCDE runs programs it builds while building: util/tradcpp preprocesses the
# *.cpp.in templates (configure.ac: GENCPP=$(top_builddir)/util/tradcpp/tradcpp).
# Built with the target compiler that is a NetBSD binary and cannot run here:
# "/bin/sh: ../../../../util/tradcpp/tradcpp: not found" (build/logs/
# userland-2026-11-14j.log:7715ff).  configure substitutes these into the
# Makefiles and Makefile.am forwards them through AM_MAKEFLAGS; the MAKE_FLAGS are
# the belt to that, and the CONFIGURE_ENV the braces -- configure's own default is
# $CC, i.e. the target's compiler, whenever pkgsrc does not say otherwise.
# ToolTalk's tt_type_comp compiles the types while MCDE installs: a program of
# the target.  A cross build installs the ptype files and the system compiles
# them (files/dtlogin.sh, before dtlogin starts); a native build does as upstream.
PLIST_VARS+=	tttypes ptypes xmpp whatsapp
.if ${USE_CROSS_COMPILE:U:tl} == "yes"
CONFIGURE_ARGS+=	--disable-tttypes-build
PLIST.ptypes=		yes
.else
PLIST.tttypes=		yes
.endif
.if ${USE_CROSS_COMPILE:U:tl} == "yes"
# the build tools are 1990s C, and this host's compiler defaults to C23 (see the
# Motif recipe's CFLAGS_FOR_BUILD)
# the host's flags, from tools/pkgsrc-cross.mk.conf
CFLAGS_FOR_BUILD=	${HOST_BUILD_CFLAGS}
CPPFLAGS_FOR_BUILD=	-DHAVE_FCNTL_H
# the include path of the host's X, from tools/pkgsrc-cross.mk.conf
CPPFLAGS_FOR_BUILD+=	-I${HOST_X11BASE:Q}/include
# Nothing of the target in what links a build tool: no -L/-R of the sysroot, and
# not the X_LIBS configure would add (a bare -R, which the host's gcc rejects:
# build/logs/mcde-hosttools-2026-11-14b.log, "unrecognized command-line option -R").
LDFLAGS_FOR_BUILD=	-Wl,-O1
CONFIGURE_ENV+=		LDFLAGS_FOR_BUILD=${LDFLAGS_FOR_BUILD:Q}
CONFIGURE_ENV+=		CC_FOR_BUILD=${NATIVE_CC:Q:U${CC:Q}}
CONFIGURE_ENV+=		AR_FOR_BUILD=${NATIVE_AR:Q:U${AR:Q}}
CONFIGURE_ENV+=		RANLIB_FOR_BUILD=${NATIVE_RANLIB:Q:U${RANLIB:Q}}
CONFIGURE_ENV+=		CFLAGS_FOR_BUILD=${CFLAGS_FOR_BUILD:Q}
CONFIGURE_ENV+=		CPPFLAGS_FOR_BUILD=${CPPFLAGS_FOR_BUILD:Q}
.endif
MAKE_FLAGS+=	CC_FOR_BUILD=${NATIVE_CC:Q:U${CC:Q}}
MAKE_FLAGS+=	AR_FOR_BUILD=${NATIVE_AR:Q:U${AR:Q}}
MAKE_FLAGS+=	RANLIB_FOR_BUILD=${NATIVE_RANLIB:Q:U${RANLIB:Q}}
MAKE_FLAGS+=	CPPFLAGS_FOR_BUILD=${CPPFLAGS_FOR_BUILD:Q}
MAKE_FLAGS+=	LDFLAGS_FOR_BUILD=${LDFLAGS_FOR_BUILD:Q}
MAKE_FLAGS+=	LIBS_FOR_BUILD=
MAKE_FLAGS+=	CFLAGS_FOR_BUILD=${CFLAGS_FOR_BUILD:Q}
SPECIAL_PERMS+=	${PREFIX}/dt/bin/dtterm ${SETUID_ROOT_PERMS}
SPECIAL_PERMS+=	${PREFIX}/dt/bin/dtapppgather ${SETUID_ROOT_PERMS}
SPECIAL_PERMS+=	${PREFIX}/dt/bin/dtmail ${REAL_ROOT_USER} mail 2755

RCD_SCRIPTS=	dtlogin

EGDIR=		${PREFIX}/share/examples/${PKGBASE}
.for i in .alias .dir
CONF_FILES+=	${PREFIX}/dt/share/cde/fontaliases/fonts${i} \
		${PKG_SYSCONFDIR}/dt/config/xfonts/C/fonts${i}
.endfor

BUILD_DEFS+=		VARBASE
OWN_DIRS+=		${VARBASE}/dt
.for d in dt/appconfig/appmanager dt/tmp spool/calendar
OWN_DIRS+=		${VARBASE}/${d}
OWN_DIRS_PERMS+=	${VARBASE}/${d} ${REAL_ROOT_USER} ${REAL_ROOT_GROUP} 0770
.endfor
OWN_DIRS+=		${PKG_SYSCONFDIR}/dt/config
OWN_DIRS+=		${PKG_SYSCONFDIR}/dt/config/Xsession.d
OWN_DIRS+=		${PKG_SYSCONFDIR}/dt/config/xfonts/C

MAKE_DIRS+=		share/xsessions
INSTALLATION_DIRS+=	bin share/doc/${PKGBASE}

post-extract:
	${RUN}${MKDIR} ${WRKSRC}/pkgsrc
	${RUN}${CP} ${FILESDIR}/startcde.sh ${WRKSRC}/pkgsrc/startcde
.for f in cde.desktop README.*
	${RUN}${CP} ${FILESDIR}/${f} ${WRKSRC}/pkgsrc
.endfor

# The paths of CDE become the package's.  pre-configure runs again whenever
# tools/userland.sh or tools/ext-resync.sh configure an already built tree, so
# each replacement is
#   - for text only (grep -I): sed over the objects of a built tree garbled 35
#     .o, .a and .so files ("file not recognized", build/logs/
#     userland-2026-11-17g.log:33-51 and h.log:148);
#   - idempotent: ${PKG_SYSCONFDIR}/dt is /usr/pkg/etc/dt and holds /etc/dt
#     again, so a second pass wrote /usr/pkg/usr/pkg/etc/dt into 276 files.
#     What is already the package's path is set aside first;
#   - silent for a file it does not change: no new time stamp, nothing
#     recompiled for it.
MCDE_PATHFIX=	for f in `${FIND} ${WRKSRC} -type f | ${XARGS} ${GREP} -lI "$$from"`; do \
		${SED} -e "s:$$to:@MCDE_PATHFIX@:g" -e "s:$$from:$$to:g" \
		    -e "s:@MCDE_PATHFIX@:$$to:g" $$f > $$f.pdone; \
		if cmp -s $$f $$f.pdone; then ${RM} -f $$f.pdone; \
		else ${ECHO} "Replacing $$from in $$f"; ${MV} $$f.pdone $$f; fi; \
	done

pre-configure:
	${RUN}${CHMOD} u+w ${WRKDIR}/*
	${RUN}from=/usr/bin/X11 to=${X11BASE}/bin; ${MCDE_PATHFIX}
	${RUN}from=/usr/dt to=${PREFIX}/dt; ${MCDE_PATHFIX}
	${RUN}from=/etc/dt to=${PKG_SYSCONFDIR}/dt; ${MCDE_PATHFIX}
	${RUN}set -e; cd ${WRKSRC} && autoreconf -fiv

# libtool wrote '=<workdir>/.buildlink/lib/libpng16.la' into dependency_libs of
# libDtSvc.la and libcsa.la (build/obj/destdir.amd64/usr/pkg/dt/lib, measured
# 2026-11-14, tasksd t0196): a path that exists only during this build, and one
# pkgsrc's buildlink filter does not rewrite for a consumer (its patterns need
# a separator before the path, not '=').  Same reason and same fix as in
# x11/mcde-motif (ext/src/mcde/pkgsrc-motif/Makefile, post-install): the
# installed file names the target's path.  The '=${PREFIX}/dt/lib/...' entries
# libtool writes for the package's own libraries are the sysroot notation and
# stay.
post-install: pam-install
	${RUN}${FIND} ${DESTDIR}${PREFIX}/dt -type f -perm -111 -exec	\
		${SH} -c '${STRIP} --strip-unneeded {} > /dev/null 2>&1' \;
	${RUN}${FIND} ${DESTDIR}${PREFIX}/dt -name '*.la' -type f |	\
	while read f; do						\
		${SED} -i -e 's|=${WRKDIR}/.buildlink/lib/|${PREFIX}/lib/|g' $$f; \
	done
	${INSTALL_SCRIPT} ${WRKSRC}/pkgsrc/startcde ${DESTDIR}${PREFIX}/bin
	${INSTALL_DATA} ${WRKSRC}/pkgsrc/cde.desktop	\
		${DESTDIR}${PREFIX}/share/xsessions
.for f in CONTRIBUTORS COPYING HISTORY README.md
	${RUN}${MV} ${DESTDIR}${PREFIX}/dt/${f}		\
		${DESTDIR}${PREFIX}/share/doc/${PKGBASE}
.endfor
.for r in README.*
	${INSTALL_DATA} ${WRKSRC}/pkgsrc/${r}		\
		${DESTDIR}${PREFIX}/share/doc/${PKGBASE}
.endfor
	${INSTALL_DATA} ${WRKSRC}/pkgsrc/cde.desktop	\
		${DESTDIR}${PREFIX}/share/xsessions

.include "../../x11/libXScrnSaver/buildlink3.mk"
.include "../../x11/libXinerama/buildlink3.mk"
.include "../../x11/libXrandr/buildlink3.mk"
.include "../../x11/libXfixes/buildlink3.mk"
.include "../../x11/libXdmcp/buildlink3.mk"
.include "../../x11/xbitmaps/buildlink3.mk"
.include "../../x11/libXmu/buildlink3.mk"
.include "../../x11/libXau/buildlink3.mk"
.include "../../x11/libSM/buildlink3.mk"

.include "../../converters/libiconv/buildlink3.mk"
.include "../../devel/gettext-lib/buildlink3.mk"
.include "../../databases/lmdb/buildlink3.mk"
.include "../../textproc/opensp/buildlink3.mk"
.include "../../fonts/fontconfig/buildlink3.mk"
.include "../../lang/tcl/buildlink3.mk"
# dtchat (configure.ac): libopus for the calls, libretls for the IRC adapter
.include "../../audio/libopus/buildlink3.mk"
.include "../../security/libretls/buildlink3.mk"
.include "../../mk/pthread.buildlink3.mk"
# Makronix's Motif, from motif/ of the same archive (../pkgsrc-motif)
.include "../../x11/mcde-motif/buildlink3.mk"
.include "../../mk/jpeg.buildlink3.mk"
.include "../../graphics/png/buildlink3.mk"
.include "../../mk/bsd.pkg.mk"
