################################################################################
#
# rubygem.cygclass - functions for building RubyGems packages
#
# 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/rubygem.cygclass
#  SYNOPSIS
#  inherit rubygem
#  DESCRIPTION
#  RubyGems is a Ruby extension hosting service, much like CPAN is for Perl.
#  Packages are distributed in the .gem format and have a unique installation
#  scheme which allows for multiple versions of any package to be installed.
#  Since Ruby 1.9, RubyGems is fully integrated into Ruby, and has become the
#  preferred way to distribute Ruby extensions.
#
#  This cygclass provides a framework for building RubyGems packages for Cygwin.
#  INHERITS
#  ruby.cygclass
#****

inherit ruby

check_prog_req gem rubygems

#****o* rubygem.cygclass/RUBYGEM_NAME
#  DESCRIPTION
#  The original package name of the package as listed on RubyGems.  The default
#  value is NAME without the leading "ruby-", which covers most RubyGems packages.
#  ORIG_PN is also supported for backwards compatibility.
#****
: ${RUBYGEM_NAME=${ORIG_PN:-${NAME#ruby-}}}

#****o* rubygem.cygclass/CATEGORY (rubygem)
#  DEFINITION
CATEGORY="Ruby"
#****
#****o* rubygem.cygclass/SUMMARY (rubygem)
#  DEFINITION
SUMMARY="Ruby ${NAME#ruby-} module"
#****
#****o* rubygem.cygclass/HOMEPAGE (rubygem)
#  DESCRIPTION
#  Default homepage of the Ruby module on RubyGems.
#****
HOMEPAGE="http://rubygems.org/gems/${RUBYGEM_NAME}"

#****o* rubygem.cygclass/SRC_URI (rubygem)
#  DESCRIPTION
#  Default download location of the Ruby module on RubyGems.
#****
SRC_URI="http://rubygems.org/downloads/${RUBYGEM_NAME}-${VERSION}.gem"

SRC_DIR="${RUBYGEM_NAME}-${VERSION}"

#****d* rubygem.cygclass/RUBYGEM_DIR
#  DESCRIPTION
#  The RubyGems system root directory, namely /usr/share/gems.
#****
RUBYGEM_DIR=$(${RUBY} -e 'print Gem.default_dirs[:system][:gem_dir]')

#****d* rubygem.cygclass/RUBYGEM_INSTDIR
#  DESCRIPTION
#  The directory containing the original runtime content of this particular Gem.
#****
RUBYGEM_INSTDIR=${RUBYGEM_DIR}/gems/${RUBYGEM_NAME}-${VERSION}

# this file is removed, no need for it in the binary package
RUBYGEM_CACHE=${RUBYGEM_DIR}/cache/${RUBYGEM_NAME}-${VERSION}.gem

#****d* rubygem.cygclass/RUBYGEM_SPEC
#  DESCRIPTION
#  The RubyGems specification file for this particular Gem.
#****
RUBYGEM_SPEC=${RUBYGEM_DIR}/specifications/${RUBYGEM_NAME}-${VERSION}.gemspec

#****d* rubygem.cygclass/RUBYGEM_DOCDIR
#  DESCRIPTION
#  The directory containing the generated documentation of this particular Gem.
#****
RUBYGEM_DOCDIR=${RUBYGEM_DIR}/doc/${RUBYGEM_NAME}-${VERSION}

#****d* rubygem.cygclass/RUBYGEM_EXTDIR
#  DESCRIPTION
#  The directory containing any native compiled extensions of this particular Gem.
#****
RUBYGEM_EXTDIR=$(${RUBY} -e 'print Gem.default_dirs[:system][:ext_dir]')/ruby/${RUBY_VERSION%.*}.0/${RUBYGEM_NAME}-${VERSION}


#****C* rubygem.cygclass/rubygem_compile
#  SYNOPSIS
#  rubygem_compile
#  DESCRIPTION
#  Rebuilds the source gem (including any patches made to files in $S) and
#  prepares it for installation.
#****
rubygem_compile() {
	gem build ${RUBYGEM_NAME}.gemspec
}

#****I* rubygem.cygclass/rubygem_install
#  SYNOPSIS
#  rubygem_install [CONFIGURE_FLAGS]
#  DESCRIPTION
#  Installs the gem contents into $D.  Any arguments are passed to extconf.rb
#  as configure flags.
#  SEE ALSO
#  RUBYGEM_CONFIGURE_ARGS
#****
rubygem_install() {
#****v* rubygem.cygclass/RUBYGEM_CONFIGURE_ARGS
#  DESCRIPTION
#  Configure flags to be passed to extconf.rb when compiling Gems with
#  C extensions.
#****
	CONFIGURE_ARGS="--with-cflags='${CFLAGS} -fdebug-prefix-map=${D}${RUBYGEM_INSTDIR}=/usr/src/debug/${NAME}-${VERSION}-${RELEASE}' $RUBYGEM_CONFIGURE_ARGS $@" \
	gem install -V --local --ignore-dependencies --document=rdoc,ri \
		--install-dir ${D}${RUBYGEM_DIR} --bindir ${D}/usr/bin \
		${RUBYGEM_NAME}-${VERSION}.gem

	# used later to clean up RUBYGEM_INSTDIR
	gem spec ${RUBYGEM_NAME}-${VERSION}.gem files --yaml \
		| sed -e 's|^[- ]*|./|g' > ${B}/${RUBYGEM_NAME}-${VERSION}.gem.lst

	# no need for a copy of the source gem in the binary package
	rm -f ${D}${RUBYGEM_CACHE}

	pushd ${D}${RUBYGEM_INSTDIR}

	# Ruby-GNOME2 components
	if [ -f ext/${RUBYGEM_NAME}/libruby-${RUBYGEM_NAME/-/_}.a ]
	then
		dolib ext/${RUBYGEM_NAME}/libruby-${RUBYGEM_NAME/-/_}.a
	fi
	if [ -f ext/${RUBYGEM_NAME}/ruby-${RUBYGEM_NAME/_/-}.pc ]
	then
		dopkgconfig ext/${RUBYGEM_NAME}/ruby-${RUBYGEM_NAME/_/-}.pc
	fi

	# clean up built files
	find -type f | grep -Fxvf ${B}/${RUBYGEM_NAME}-${VERSION}.gem.lst | xargs rm -f
	# these are not needed once installed
	rm -fr Rakefile *.gemspec depend extconf.rb install.rb setup.rb doc/ ext/ src/ test/

	popd # ${D}${RUBYGEM_INSTDIR}

	if [ -d ${D}${RUBYGEM_EXTDIR} ]
	then
		pushd ${D}${RUBYGEM_EXTDIR}

		# more Ruby-GNOME2 components
		for h in $(find -name '*.h')
		do
			dodir ${RUBY_HEADERDIR}
			mv ${h} ${D}${RUBY_HEADERDIR}/
		done

		# these are not needed, but gem.build_complete must remain
		rm -f gem_make.out mkmf.log

		popd # ${D}${RUBYGEM_EXTDIR}
	fi
}

#****o* rubygem.cygclass/src_compile (rubygem)
#  DEFINITION
src_compile() {
	cd ${S}
	rubygem_compile
}
#****

#****o* rubygem.cygclass/src_install (rubygem)
#  DEFINITION
src_install() {
	cd ${S}
	rubygem_install
}
#****

# docs are already installed into RUBYGEM_INSTDIR
_CYGPORT_RESTRICT_postinst_doc_=1

readonly -f rubygem_compile rubygem_install
