################################################################################
#
# gnome2.cygclass - functions for building GNOME 2.x packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/gnome2.cygclass
#  SYNOPSIS
#  inherit gnome2
#  DESCRIPTION
#  The GNOME project provides a full-featured X desktop with a wide variety
#  of applications, built on an object-oriented library stack based on GLib
#  and the GTK+ toolkit.  Most packages are written in C and are built with
#  an autotools-based system with some additional tools.
#
#  This cygclass manages the building of most GNOME-based packages.
#  INHERITS
#  autotools.cygclass, gnome.org.cygclass
#****

inherit gnome.org

#****C* gnome2.cygclass/gnome2_autoreconf
#  SYNOPSIS
#  gnome2_autoreconf
#  DESCRIPTION
#  Updates the build system with gnome-autogen.sh.  gnome2_autoreconf must be
#  run in the directory containing the top-level configure.ac or configure.in
#  (usually $S).
#  WARNING
#  Skipping this step is strongly discouraged.  Patches to configure.ac, aclocal
#  macros, or Makefile.am files will not take effect until their tools have been
#  regenerated.  Also, Cygwin's libtool package often includes patches which
#  are not yet available in the latest upstream release, so libraries may not
#  be built optimally, or even correctly, without this step having been run.
#  The same applies for some of the GNOME build tools as well.
#  REQUIRES
#  autoconf, automake, gettext-devel, gnome-common, gnome-doc-utils, gtk-doc,
#  intltool, libglib2.0-devel, libtool, pkg-config.
#  SEE ALSO
#  cygautoreconf
#****
gnome2_autoreconf() {
	local auxdir
	local configure_ac
	local skip_d

	if [ -e configure.ac ]
	then
		configure_ac="configure.ac"
	elif [ -e configure.in ]
	then
		configure_ac="configure.in"
	else
		error "gnome2: configure.ac or configure.in not found"
	fi

	check_prog_req gnome-autogen.sh gnome-common

	if [ -f omf-install/Makefile.am -o -f omf.make -o -f xmldocs.make ]
	then
		USE_COMMON_DOC_BUILD=yes
	fi

	if defined WANT_AUTOMAKE
	then
		export ACLOCAL=aclocal-${WANT_AUTOMAKE}
		export AUTOMAKE=automake-${WANT_AUTOMAKE}
	else
		export ACLOCAL=aclocal-$(automake --version | head -n 1 | sed 's/^.*[  ]\([0-9]*\.[0-9]*\).*$/\1/')
		export AUTOMAKE=${ACLOCAL/aclocal/automake}
	fi

	export ACLOCAL_FLAGS
	export NOCONFIGURE=1
	export PKG_NAME=${ORIG_PN:-${PN}}
	export REQUIRED_LIBTOOL_VERSION=2.2.2
	export USE_COMMON_DOC_BUILD

	if cross_compiling
	then
		local ccsysroot=$(${CC} -print-sysroot)
		local sysrootltize=${ccsysroot}$(__target_prefix)/bin/libtoolize
		local sysrootaclocaldir=${ccsysroot}$(__target_prefix)/share/aclocal
		if [ -x ${sysrootltize} ]
		then
			export LIBTOOLIZE=${sysrootltize}
		fi
		if [ -d ${sysrootaclocaldir} ]
		then
			ACLOCAL_FLAGS+=" -I ${sysrootaclocaldir}"
		fi
	fi

#****v* gnome2.cygclass/GNOME2_NO_AUTOGEN
#  DESCRIPTION
#  By default, gnome-autogen.sh will retool the working directory's and any
#  subdirectories containing a configure.ac/configure.in which may be configured
#  separately with an AC_CONFIG_SUBDIRS.  If a subdirectory need not be
#  retooled (e.g. it will not built for whatever reason), or it does not use
#  autotools (e.g. the libdb included in evolution), define this variable to
#  the subdirectories to skip, relative to the directory where gnome2_autoreconf
#  will be run (usually $S).
#****
	for skip_d in ${GNOME2_NO_AUTOGEN//,/ }
	do
		if [ ! -d ${skip_d} ]
		then
			warning "GNOME2_NO_AUTOGEN: directory not found: ${skip_d}"
			continue
		fi

		touch ${skip_d}/NO-AUTO-GEN
	done

	# sometimes ACLOCAL_AMFLAGS is defined but the given directory
	# exists only in VCS (or is created by an autogen.sh) and is
	# not shipped in the tarball
	if [ -f Makefile.am ]
	then
		__parse_aclocal_amflags $(grep '^ACLOCAL_AMFLAGS' Makefile.am | sed 's/=/ = /') | xargs -r mkdir -p
	fi

	__config_set with_autoconf 1

	if [ ! -f aclocal.m4 ] || $(head -n 1 aclocal.m4 | grep -q 'generated.* by aclocal')
	then
		__config_set with_aclocal 1
	fi

	if $(grep -q A[CM]_CONFIG_HEADER ${configure_ac})
	then
		__config_set with_autoheader 1
	fi

	if $(grep -q AM_INIT_AUTOMAKE ${configure_ac})
	then
		__config_set with_automake 1
	fi

	if $(grep -Eq '(A[CM]_PROG_LIBTOOL|LT_INIT)' ${configure_ac})
	then
		__config_set with_libtool 1
	fi

	# FIXME: does not detect sublevel configures
	if $(grep -Eq 'AM_(GNU_GETTEXT|ICONV)' ${configure_ac})
	then
		__config_set with_gnu_gettext 1
		cp -f /usr/share/gettext/config.rpath .
	elif $(grep -Eq 'AM_GLIB_GNU_GETTEXT' ${configure_ac})
	then
		__config_set with_glib_gettext 1
	fi

	if $(grep -Eq '(AC|IT)_PROG_INTLTOOL' ${configure_ac})
	then
		__config_set with_intltool 1
	fi

	if $(grep -q '\./libtool -' ${configure_ac}) && ! $(grep -q 'LT_OUTPUT' ${configure_ac})
	then
		warning "${configure_ac} may require LT_OUTPUT macro"
	fi

	# aclocal requires that its directories exist
	sed -n -e 's|^AC_CONFIG_MACRO_DIRS*(\[*\([^]]*\)\]*).*|\1|p' ${configure_ac} | xargs -r mkdir -p

	# Automake will exit if these are not present
	# dodoc won't install these, however, if they are empty
	touch AUTHORS COPYING ChangeLog INSTALL NEWS README

	gnome-autogen.sh || error "gnome2: autoreconf failed"

	auxdir="$(grep '^[^\#]*AC_CONFIG_AUX_DIR' ${configure_ac} | sed -e 's!.*AC_CONFIG_AUX_DIR(\[*\(.*\))!\1!g' -e 's!\]*!!g')"
	if [ -f ${auxdir:-.}/config.guess ]
	then
		gnuconfigize ${auxdir:-.}
	fi
}

#****C* gnome2.cygclass/gnome2_configure
#  SYNOPSIS
#  cd $B
#  gnome2_configure [CONFIGURE_FLAGS]
#  DESCRIPTION
#  Calls cygconf with the following options:
#  * --enable-gtk-doc: create and install API docs (native builds only)
#  * --disable-schemas-install: GConf schemas are still installed, but are
#    registered with postinstall and preremove scripts
#  * --disable-scrollkeeper: Scrollkeeper is obsolete
#  All arguments to gnome2_configure are passed to cygconf as configure flags.
#****
gnome2_configure() {
	local gtk_doc

	# packages that use intltool w/o glib-gettext get this wrong
	export DATADIRNAME="share"

	if cross_compiling
	then
		gtk_doc="--disable-gtk-doc"
	else
		gtk_doc="--enable-gtk-doc"
	fi

	cygconf \
		${gtk_doc} \
		--disable-schemas-install \
		--disable-scrollkeeper \
		"${@}"
}

#****C* gnome2.cygclass/gnome2_compile
#  SYNOPSIS
#  cd $B
#  gnome2_compile [CONFIGURE_FLAGS]
#  DESCRIPTION
#  Calls gnome2_configure followed by cygmake. All arguments to gnome2_compile
#  are passed as configure flags.
#****
gnome2_compile() {
	gnome2_configure "${@}"
	cygmake
}

#****o* gnome2.cygclass/src_compile (gnome2)
#  DEFINITION
src_compile() {
	cd ${S}
	gnome2_autoreconf
	cd ${B}
	gnome2_configure
	cygmake
}
#****

readonly -f gnome2_autoreconf gnome2_configure gnome2_compile
