# Template build file for 'ruby'.
_ruby_abiver=2.5.0

pkgname=ruby
version=2.5.1
revision=1
build_style=gnu-configure
configure_args="--enable-shared --disable-rpath
 DOXYGEN=/usr/bin/doxygen DOT=/usr/bin/dot PKG_CONFIG=/usr/bin/pkg-config"
make_build_args="all capi"
short_desc="Ruby programming language"
homepage="http://www.ruby-lang.org/en/"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="BSD"
distfiles="https://cache.ruby-lang.org/pub/ruby/${version%.*}/${pkgname}-${version}.tar.bz2"
checksum=0f5d20f012baca865381a055e73f22db814615fee3c68083182cb78a4b3b30cb

hostmakedepends="pkg-config bison groff"
makedepends="zlib-devel readline-devel libffi-devel libressl-devel
 gdbm-devel libyaml-devel pango-devel"

case "$XBPS_TARGET_MACHINE" in
	*-musl)	# Broken on the builders but successfully built locally
		# As a workaround disable all docs
		configure_args+=" --disable-install-doc"
		;;
	*)
		hostmakedepends+=" doxygen graphviz"
		configure_args+=" DOXYGEN=/usr/bin/doxygen DOT=/usr/bin/dot"
		;;
esac


if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" ruby"
fi

pre_build() {
	# Force getaddrinfo detection.
	sed -e 's,\(checking_for("wide getaddrinfo") {try_\)run,\1link,' -i ext/socket/extconf.rb
	# Hack for cross-builds.
	touch ext/ripper/check
	if [ "$XBPS_GCC_VERSION_MAJOR" -ge 7 ]; then
		# Set -fno-stack-protector for i686* or otherwise linking fails with
		# ...: undefined reference to '__stack_chk_fail_local'
		case "$XBPS_TARGET_MACHINE" in
			i686*) sed -i Makefile \
					-e "s;-fstack-protector-strong;;" \
					-e "s;-fstack-protector;-fno-stack-protector;"
				;;
		esac
	fi
}
post_install() {
	# Remove references to the cross compiler
	if [ "$CROSS_BUILD" ]; then
		sed -e "s,${XBPS_CROSS_TRIPLET}-,,g" -i ${DESTDIR}/usr/lib/ruby/${_ruby_abiver}/*/rbconfig.rb
	fi
	# Replace reference to the install(1) wrapper
	sed -e "s,${XBPS_WRAPPERDIR},/usr/bin,g" -i \
		${DESTDIR}/usr/lib/ruby/${_ruby_abiver}/*/rbconfig.rb
	# Remove references to hardening -specs.
	sed -e "s|\(-specs=.*hardened-ld\)||g" -e "s|\(-specs=.*hardened-cc1\)||g" -i \
		${DESTDIR}/usr/lib/ruby/${_ruby_abiver}/*/rbconfig.rb
	vlicense COPYING
}
ruby-devel-doc_package() {
	short_desc+=" - HTML C API documentation files"
	noarch=yes
	pkg_install() {
		if [ -d "${DESTDIR}/usr/share/doc" ]; then
			vmove usr/share/doc
		else
			vmkdir usr/share/doc/ruby
			echo "Currently not available." > \
				${PKGDESTDIR}/usr/share/doc/ruby/README
		fi
	}
}
ruby-devel_package() {
	depends="ruby-${version}_${revision} gmp-devel"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/ruby/${_ruby_abiver}/mkmf.rb
		vmove usr/lib/pkgconfig
	}
}
ruby-ri_package() {
	depends="ruby-${version}_${revision}"
	short_desc="Ruby Interactive reference"
	noarch=yes
	pkg_install() {
		vmove usr/bin/ri
		if [ -d "${DESTDIR}/usr/share/ri" ]; then
			vmove usr/share/ri
		fi
		vmove "usr/share/man/man1/ri*"
	}
}