+dnl Copyright © 2012 Nick Bowler
+dnl
+dnl Base definitions for dxcommon.
+dnl
+dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+dnl This is free software: you are free to do what the fuck you want to.
+dnl There is NO WARRANTY, to the extent permitted by law.
+
+m4_pattern_forbid([^_?DX_])
+
+dnl DX_INIT([directory])
+dnl
+dnl Sets the directory of the dxcommon checkout, which is used by other macros
+dnl to find any source files that they may need. If this file was included
+dnl from its original location using m4_include, which normally happens when
+dnl using aclocal, then the directory will be determined automatically (and
+dnl this macro does not need to be called in configure.ac at all). Otherwise,
+dnl this should be called prior to any other dxcommon macro.
+
+m4_define([_DX_BASE_FILE], __file__)
+AC_DEFUN_ONCE([DX_INIT], [dnl
+
+dnl Care must be taken to avoid spurious expansions of things that look like
+dnl macros in the filename; this is made especially difficult since
+dnl m4_bpatsubst does not expand to a quoted string.
+m4_define([_DX_BASEDIR], m4_if([$1], [],
+ [m4_bpatsubst(m4_dquote(m4_defn([_DX_BASE_FILE])),
+ [/m4/[^/]*\.m4\(.\)$], [\1])],
+ [[$1]]))
+
+dnl Include the stamp file, which will cause a failure at autoconf time
+dnl if it does not exist in the checkout. We circumvent m4_include to
+dnl avoid warnings about multiple inclusions.
+m4_builtin([include], m4_defn([_DX_BASEDIR])[/m4/dx-stamp.m4])
+_DX_STAMP_DUMMY
+
+dnl Autoconf scripts should use the DX_BASEDIR m4 macro instead of the
+dnl AC_SUBSTed shell variable, but we use the same name for the macro since
+dnl it means the same thing as the substituted variable.
+dnl
+dnl Autoconf versions <= 2.69 have a bug which causes the first argument of
+dnl AC_SUBST to be expanded as a macro when using the two-argument form. So
+dnl until we require a newer version of autoconf, we must use the one-argument
+dnl form of AC_SUBST.
+
+[DX_BASEDIR]="AS_ESCAPE(m4_dquote(m4_defn([_DX_BASEDIR])))"
+m4_pushdef([m4_pattern_allow])
+AC_SUBST([DX_BASEDIR])
+m4_popdef([m4_pattern_allow])
+])
+
+AC_DEFUN([DX_BASEDIR], [dnl
+AC_REQUIRE([DX_INIT])dnl
+m4_defn([_DX_BASEDIR])dnl
+])