# Template file for 'ruby'
_ruby_abiver=2.7.0

pkgname=ruby
version=2.7.2
# Liberation: Adds pure Ruby json version
_jsonver=2.3.0
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"
hostmakedepends="pkg-config bison groff"
makedepends="zlib-devel readline-devel libffi-devel libressl-devel
 gdbm-devel libyaml-devel pango-devel"
checkdepends="tzdata"
short_desc="Ruby programming language"
homepage="http://www.ruby-lang.org/en/"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="Ruby, BSD-2-Clause"
# Liberation: Adds pure Ruby json distfile
distfiles="https://cache.ruby-lang.org/pub/ruby/${version%.*}/${pkgname}-${version}.tar.bz2
 https://github.com/flori/json/archive/v${_jsonver}.tar.gz"
checksum="65a590313d244d48dc2ef9a9ad015dd8bc6faf821621bbb269aa7462829c75ed
 b8a7576b9b6dfb997d9b3911e13b2e5b1bde50ff1f5aea46b116c560649ec1a4"

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

case "$XBPS_TARGET_MACHINE" in
	arm*-musl) # Default is ucontext
		configure_args+=" --with-coroutine=arm32"
		;;
	ppc64*) # Default is ucontext on BE, but ppc64le really just means ELFv2
		configure_args+=" --with-coroutine=ppc64le"
		;;
	mips*-musl|ppc-musl)
		_need_libucontext=yes
		makedepends+=" libucontext-devel"
		configure_args+=" LIBS=-lucontext"
		;;
esac

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

post_patch() {
	[ "$XBPS_TARGET_LIBC" = "glibc" ] && return 0

	echo "Patching out using binary gems for non-glibc..."
	patch -sNp1 -i ${FILESDIR}/rubygems-avoid-platform-specific-gems.patch

	# Liberation: Implement pure Ruby json
	# Some codes are taken from:
	# https://git.parabola.nu/abslibre.git/tree/libre/ruby/PKGBUILD
	cd "${XBPS_BUILDDIR}/json-${_jsonver}"

	# Modify 'json' so that it doesn't try to load (non-free!) 'json/ext'
	echo "applying json-libre.patch to json"
	rm -rf -- json.gemspec ext lib/json/ext*
	patch -p1 -i "${FILESDIR}/json-libre.patch"

	# Now remove the json ext from the ruby sources
	cd "${XBPS_BUILDDIR}/${wrksrc}"
	rm -r ext/json test/json

	# and insert the json_pure lib instead
	cp -r  ../json-${_jsonver}/lib/*             -t lib/
	cp     ../json-${_jsonver}/json_pure.gemspec -t lib/json/
	cp -rT ../json-${_jsonver}/tests                test/json

	# Clean up the original json sources
	rm -rf "{XBPS_BUILDDIR}/json-${_jsonver}"
}

pre_configure() {
	# this allows the test suite to pass without crashing
	export CFLAGS+=" -fno-omit-frame-pointer -fno-strict-aliasing"
	export CPPFLAGS+=" -fno-omit-frame-pointer -fno-strict-aliasing"

	case "$XBPS_TARGET_MACHINE" in
		ppc64le*) ;;
		ppc*)
			# Random segfaults otherwise
			export CFLAGS+=" -Os"
			export CPPFLAGS+=" -Os"
			;;
	esac

	# force this to pass on systems with these as macros (musl)
	export ac_cv_func_isnan=yes
	export ac_cv_func_isinf=yes
}

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
	# 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
}

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"
	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"
	if [ "$_need_libucontext" ]; then
		depends+=" libucontext-devel"
	fi
	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"
	pkg_install() {
		vmove usr/bin/ri
		if [ -d "${DESTDIR}/usr/share/ri" ]; then
			vmove usr/share/ri
		fi
		vmove "usr/share/man/man1/ri*"
	}
}
