################################################################################
#
# ruby-gnome2.cygclass - functions for building Ruby GNOME 2.x bindings
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# 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 <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/ruby-gnome2.cygclass
#  SYNOPSIS
#  [RUBY_GNOME2_MODULES="..."]
#  inherit ruby-gnome2
#  DESCRIPTION
#  The Ruby-GNOME project provides Ruby bindings for the GLib, GTK+, and GNOME
#  libraries, allowing full-fledged GNOME programs to be written in the Ruby
#  programming language.  Source packages are based on the Ruby extconf.rb
#  system with some additions to handle the unique requirements of inter-module
#  dependencies and pkg-config usage.  A few third-party modules are also
#  available which use the same system to bind libraries not yet covered by
#  the Ruby-GNOME project.
#
#  This cygclass builds both official and third-party Ruby-GNOME2 binding packages.
#  NOTE
#  Ruby-GNOME packages may now be built from gems with rubygem.cygclass.
#  Therefore, this cygclass is deprecated and may be removed in the future.
#  INHERITS
#  ruby.cygclass
#****

inherit ruby

__ruby_gnome2_get_module_dir() {
	local ret

	if (( $# > 1 ))
	then
		error "$FUNCNAME: too many arguments"
	fi

	case ${@} in
		atk|pango|gnomevfs|gtkglext|gtkhtml2|gtkmozembed|gtksourceview|\
		gtksourceview2|poppler|vte|libgda|goocanvas)
			ret=${@} ;;
		glib2|gtk2|libglade2|bonobo2|bonoboui2|libart2|gnomecanvas2|gconf2|\
		gnome2|gnomeprint2|gnomeprintui2|rsvg2)
			ret=${@%2} ;;
		gdk_pixbuf2)
			ret=gdkpixbuf ;;
		gst)
			ret=gstreamer ;;
		panelapplet2)
			ret=panel-applet ;;
		*)
			# external packages
			ret=. ;;
	esac

	echo -n ${ret}
}


rbg2_pn=${PN#ruby-}

#****v* ruby-gnome2.cygclass/RUBY_GNOME2_MODULES
#  DESCRIPTION
#  When building the official ruby-gnome2-all source package, RUBY_GNOME2_MODULES
#  must be set to a whitespace-separated list of binding modules to be built.
#  Third-party packages are assumed to contain a single binding to be built.
#****

case ${rbg2_pn} in
	gnome2-all)
		if ! defined RUBY_GNOME2_MODULES
		then
			error "ruby-gnome2: RUBY_GNOME2_MODULES undefined"
		fi

		for _mod in ${RUBY_GNOME2_MODULES}
		do
			rbg2_dir+="$(__ruby_gnome2_get_module_dir ${_mod}) "
		done
		unset _mod
		ORIG_PN="ruby-gnome2-all"
		;;
	*)
		# external package, be sure to override SRC_URI
		rbg2_dir=.
		ORIG_PN=${ORIG_PN:-${PN}}
		RUBY_GNOME2_MODULES=${rbg2_pn}
		;;
esac

#****o* ruby-gnome2.cygclass/CATEGORY (ruby-gnome2)
#  DEFINITION
CATEGORY="Ruby"
#****
#****o* ruby-gnome2.cygclass/SUMMARY (ruby-gnome2)
#  DEFINITION
SUMMARY="Ruby ${rbg2_pn} bindings"
#****
#****o* ruby-gnome2.cygclass/HOMEPAGE (ruby-gnome2)
#  DEFINITION
HOMEPAGE="http://ruby-gnome2.sourceforge.jp/"
#****

unset rbg2_pn

#****o* ruby-gnome2.cygclass/SRC_URI (ruby-gnome2)
#  DESCRIPTION
#  Download location of the Ruby-GNOME2 source package.
#****
#****o* ruby-gnome2.cygclass/SVN_URI (ruby-gnome2)
#  DESCRIPTION
#  Location of the Ruby-GNOME2 Subversion repository.
#****
#****o* ruby-gnome2.cygclass/SVN_REV (ruby-gnome2)
#  DESCRIPTION
#  If PV is of the form major.minor.micro.rev, a Subversion snapshot will
#  be assumed, svn.cygclass will be inherit()ed, and the fourth (rev) component
#  of PV defined as SVN_REV.
#****

case ${PVP[3]} in
[1-9]*)
	SVN_URI="http://ruby-gnome2.svn.sourceforge.net/svnroot/ruby-gnome2/ruby-gnome2/trunk/${rbg2_dir}"
	SVN_REV=${PVP[3]}
	inherit svn
	SRC_URI+="
		http://ruby-gnome2.svn.sourceforge.net/viewvc/*checkout*/ruby-gnome2/ruby-gnome2/trunk/extconf.rb
		http://ruby-gnome2.svn.sourceforge.net/viewvc/*checkout*/ruby-gnome2/ruby-gnome2/trunk/exec_make.rb
		"
	SRC_DIR=.
	;;
rc*|'')
	SRC_URI="mirror://sourceforge/ruby-gnome2/${ORIG_PN}-${PV/_/-}.tar.gz"
	SRC_DIR="${ORIG_PN}-${PV/_/-}"
	;;
*)	error "Unknown package version: ${PV}" ;;
esac

# WARNING: CAIRO_PATH is supposed to be the ruby-cairo builddir, not
# the installed RUBY_VENDORARCH, so this will create a bogus -L flag no
# matter what.  We compensate by correctly defining LOCAL_LIBS below.
#
# However, be sure that all extconf.rb have_func() calls come BEFORE any
# add_depend_package() calls, otherwise the tests will give false negatives
# due to linker errors (e.g. unable to find -lruby-cairo).
export CAIRO_PATH="${RUBY_VENDORARCH}"

#****o* ruby-gnome2.cygclass/MAKEOPTS (ruby-gnome2)
#  DESCRIPTION
#  The Ruby-GNOME2 source packages do not support parallel make.
#****
MAKEOPTS+=" -j1"

#****o* ruby-gnome2.cygclass/DEPS_PATH (ruby-gnome2)
#  DESCRIPTION
#  Ruby-GNOME2 modules are unique that they are not only linked against the C
#  libraries which they bind, but also expose an API to bindings higher up in
#  the stack which link against them as well.  Therefore, DEPS_PATH is set to
#  catch these inter-module link dependencies.
#****
DEPS_PATH="${RUBY_VENDORARCH}"

#****C* ruby-gnome2.cygclass/ruby_gnome2_compile
#  SYNOPSIS
#  cd $B
#  ruby_gnome2_compile
#  DESCRIPTION
#  Configures and compiles the Ruby-GNOME2 bindings specified in
#  RUBY_GNOME2_MODULES, then generates RI documentation for those bindings.
#****
ruby_gnome2_compile() {
	local d

	${RUBY} extconf.rb --vendor ${rbg2_dir} || error "extconf.rb failed"

	cygmake cflags="${CFLAGS} ${CPPFLAGS}" LOCAL_LIBS="-L${RUBY_VENDORARCH}"

	for d in ${rbg2_dir}
	do
		if [ -d ${d}/src ]
		then
			rdoc --force-update --ri --op ${d}/rdoc ${d}/src
		else
			rdoc --force-update --ri --op ${d}/rdoc ${d}
		fi
	done
}

#****I* ruby-gnome2.cygclass/ruby_gnome2_install
#  SYNOPSIS
#  cd $B
#  ruby_gnome2_install
#  DESCRIPTION
#  Installs the Ruby-GNOME2 bindings specified in RUBY_GNOME2_MODULES, as
#  well as import libraries for those bindings whose API is used by other
#  bindings.  RI documentation is then installed per the definition of
#  RDOC_MODULE.
#****
ruby_gnome2_install() {
	local d mod

	ruby_install

	for mod in ${RUBY_GNOME2_MODULES}
	do
		d=$(__ruby_gnome2_get_module_dir ${mod})

		case ${mod} in
			cairo|glib2|atk|pango|gtk2|libart2)
				insinto ${RUBY_VENDORARCH}
				doins $(find ${d}/ -name '*.so' | sed -e 's|\(.*\)/\(.*\)\.so|\1/libruby-\2.a|')
				;;
		esac

		docinto /ruby-${mod}
		dodoc ${S}/${d}/{COPYING.LIB,ChangeLog,README}

		if [ -d ${d}/rdoc ]
		then
			dodir ${RI_SITEDIR}

			pushd ${d}/rdoc

			for docmod in ${RDOC_MODULE}
			do
				if [ -d ${docmod} ]
				then
					dodir ${RI_SITEDIR}/${docmod}
					cp -r ${docmod}/* ${D}${RI_SITEDIR}/${docmod}/
				fi
			done

			popd
		fi
	done
}

#****o* ruby-gnome2.cygclass/src_compile (ruby-gnome2)
#  DEFINITION
src_compile() {
	cd ${B}
	lndirs
	ruby_gnome2_compile
}
#****

#****o* ruby-gnome2.cygclass/src_install (ruby-gnome2)
#  DEFINITION
src_install() {
	cd ${B}
	ruby_gnome2_install
}
#****

readonly -f __ruby_gnome2_get_module_dir ruby_gnome2_compile ruby_gnome2_install
