# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# secfixes:
#   2.2.8-r0:
#     - CVE-2017-0898
#     - CVE-2017-10784
#     - CVE-2017-14033
#     - CVE-2017-14064
#     - CVE-2017-0899
#     - CVE-2017-0900
#     - CVE-2017-0901
#     - CVE-2017-0902
#
setvar pkgname = 'ruby2.2'
setvar _pkgname = 'ruby'
setvar _majorver = ${pkgname#ruby}
setvar pkgver = ""$_majorver.8""
setvar _pkgver = ${pkgver/_/-}
setvar _abiver = ""$_majorver.0""
setvar pkgrel = '0'
setvar pkgdesc = ""An object-oriented language for quick and easy programming""
setvar url = ""http://www.ruby-lang.org/en/""
setvar arch = ""all""
setvar license = ""Ruby""
setvar depends = """"
setvar depends_dev = ""gmp-dev""
setvar makedepends = ""$depends_dev zlib-dev libressl-dev gdbm-dev db-dev readline-dev
	libffi-dev coreutils yaml-dev autoconf""
setvar subpackages = ""$pkgname-doc $pkgname-dev $pkgname-irb::noarch
	$pkgname-rdoc::noarch $pkgname-rake::noarch $pkgname-bigdecimal::noarch
	$pkgname-io-console:io_console $pkgname-minitest::noarch
	$pkgname-json $pkgname-libs
	""
setvar source = ""ftp://ftp.ruby-lang.org/pub/ruby/${pkgver%.*}/$_pkgname-$_pkgver.tar.bz2""
setvar options = ""!fhs""
setvar builddir = ""$srcdir/$_pkgname-$_pkgver""

setvar _gemdir = ""/usr/lib/ruby/gems/$_abiver""
setvar _rubydir = ""/usr/lib/ruby/$_abiver""
setvar _chost = "${CHOST/-alpine-/-}"

case (CARCH) {
	x86 { setvar _arch = ""i386""}
	* { setvar _arch = "$CARCH"}
}

proc prepare {
	default_prepare || return 1

	cd $builddir
	autoconf
}

proc build {
	cd $builddir

	# -fomit-frame-pointer makes ruby segfault, see gentoo bug #150413
	# In many places aliasing rules are broken; play it safe
	# as it's risky with newer compilers to leave it as it is.
	export CFLAGS="$CFLAGS -fno-omit-frame-pointer -fno-strict-aliasing"
	export CPPFLAGS="$CPPFLAGS -fno-omit-frame-pointer -fno-strict-aliasing"

	# ruby saves path to install. we want use $PATH
	export INSTALL=install

	# the configure script does not detect isnan/isinf as macros
	export ac_cv_func_isnan=yes
	export ac_cv_func_isinf=yes

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-sitedir='/usr/local/lib/site_ruby' \
		--with-search-path="/usr/lib/site_ruby/\$(ruby_ver)/$_arch-linux" \
		--enable-pthread \
		--disable-rpath \
		--enable-shared \
		--with-mantype=man \
		--program-suffix=$_majorver \
		--with-soname=$pkgname \
		|| return 1
	make || return 1
	make test
}

proc package {
	cd $builddir

	make DESTDIR="$pkgdir" install || return 1

	if test -d "$pkgdir"/usr/local {
		local f=$(cd "$pkgdir" ; find usr/local -type f)
		if test -n $f {
			error "Found files in /usr/local:"
			echo $f
			return 1
		}
	}
	install -Dm644 COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING
}

proc rake {
	setvar pkgdesc = ""Ruby based make-like utility""
	setvar depends = "$pkgname"

	_mvgem rake || return 1
	_mv usr/bin/rake$_majorver
}

proc irb {
	setvar pkgdesc = ""The Interactive Ruby""
	setvar replaces = "$pkgname"
	setvar depends = "$pkgname"

	cd $pkgdir
	_mv usr/bin/irb$_majorver \
		usr/lib/ruby/$_abiver/irb*
}

proc rdoc {
	setvar pkgdesc = ""Ruby documentation tool""
	setvar depends = ""$pkgname $pkgname-json $pkgname-io-console""

	_mvgem rdoc || return 1
	_mv usr/bin/ri$_majorver \
		usr/bin/rdoc$_majorver
}

proc bigdecimal {
	setvar pkgdesc = ""Ruby arbitrary-precision floating point decimal arithmetic""
	setvar depends = "$pkgname"

	_mvgem bigdecimal
}

proc io_console {
	setvar pkgdesc = ""Ruby simple console IO library""
	setvar depends = "$pkgname"
	_mvgem io-console

	_mv $_rubydir/io \
		$_rubydir/$_chost/io/console.so
}

proc minitest {
	setvar pkgdesc = ""Ruby suite of testing facilities""
	setvar depends = "$pkgname"

	_mvgem minitest
}

proc json {
	setvar pkgdesc = ""JSON implementation as a Ruby extension in C""
	setvar depends = "$pkgname"

	_mvgem json
}

proc libs {
	setvar pkgdesc = ""Libraries necessary to run Ruby""
	setvar depends = """"

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

proc _mv {
	local i; for i in "$@" {
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/${i} "$subpkgdir"/${i%/*}/ || return 1
	}
}

proc _mvgem {
	cd $pkgdir
	local path; for path in ./$_rubydir/${1}.rb \
			./$_rubydir/${1} \
			./$_rubydir/$_chost/${1} \
			./$_gemdir/gems/${1}-* \
			./$_gemdir/specifications/${1}-* \
			./$_gemdir/specifications/default/${1}-*  {
		test ! -e "$pkgdir/$path" || _mv $path
	}
}

setvar sha512sums = ""aa1c65f76a51a57d9059a38a13a823112b53850a9e7d6f72c3f3e38d381412014521049f7065c1b00877501b3b554235135d0f308045c2a9da133c766f5b9e46  ruby-2.2.8.tar.bz2""