# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
# TODO: Add server components.
pkgname=opennebula
pkgver=6.10.2
pkgrel=0
pkgdesc="OpenNebula (currenty empty, install opennebula-tools instead)"
url="https://opennebula.io"
arch="noarch"
license="Apache-2.0"
_depends_ruby="
	ruby
	ruby-csv
	ruby-ipaddress
	ruby-nokogiri
	ruby-parse-cron
	ruby-rbvmomi2
	ruby-rexml
	ruby-treetop
	ruby-xmlrpc
	"
_depends_tools="
	ruby
	ruby-activesupport
	ruby-opennebula=$pkgver-r$pkgrel
	"
makedepends="
	$_depends_ruby
	$_depends_tools
	bash
	"
# The subpackages reflect names of the official Debian/Ubuntu packages.
subpackages="
	ruby-$pkgname:rubylibs:noarch
	$pkgname-tools::noarch
	"
source="https://github.com/OpenNebula/one/archive/release-$pkgver/opennebula-$pkgver.tar.gz
	fix-missing-active_support.patch
	nokogiri-version.patch
	"
builddir="$srcdir/one-release-$pkgver"

build() {
	./share/rubygems/generate
}

check() {
	# Tests are not provided, so at least try to run one of the commands to
	# ensure that there's no problem with ruby dependencies.

	_gem_install opennebula dist
	_gem_install opennebula-cli dist

	cd dist
	GEM_PATH="$(realpath usr/lib/ruby/gems/[0-9]*)" ./usr/bin/onevm --version
}

package() {
	mkdir -p "$pkgdir"
}

rubylibs() {
	pkgdesc="Ruby libraries needed to talk to OpenNebula"
	depends="$_depends_ruby"

	cd "$builddir"
	_gem_install opennebula "$subpkgdir"
}

tools() {
	pkgdesc="Command Line interface for OpenNebula"
	depends="$_depends_tools"

	cd "$builddir"
	_gem_install opennebula-cli "$subpkgdir"

	install -D -m644 src/cli/etc/*.yaml -t "$subpkgdir"/etc/one/cli/
}

_gem_install() {
	local gemname="$1"
	local destdir="$2"
	local gemdir="$destdir/$(ruby -e 'puts Gem.default_dir')"
	local geminstdir="$gemdir/gems/$gemname-$pkgver"

	mkdir -p "$destdir"/usr/bin
	gem install \
		--local \
		--install-dir "$gemdir" \
		--bindir "$destdir/usr/bin" \
		--ignore-dependencies \
		--no-document \
		--verbose \
		./$gemname-$pkgver.gem

	# Remove unnecessary files and rubbish...
	(cd "$gemdir" && rm -r cache build_info doc extensions plugins)
	(cd "$geminstdir" && rm -f LICENSE NOTICE)
}

sha512sums="
963311a5929007dc961783f31a4e6ec914fb6135fba5bd0d63c622677ec7b4a9f296cc2eba6ce6fe1a9cd720a21ced44f87aecabc3f9687d2e4f3a93e09bb00a  opennebula-6.10.2.tar.gz
88b696ddcacbeffe9edc359f8c334b638e1fd0b0e74089548e162fb995cc04e8b0500a88e7bf07882ec620ba59f53fa09378f227920c1f09cd742d1bcd5eda34  fix-missing-active_support.patch
c27900bfe57140d2dc6df717c605d266f1f7a7b1aca2c42a378acf02afdc5f48997f55a4c2221e1fa22d7460bdbd1655a38a032a6255ed636e75c1e8dd4dbf77  nokogiri-version.patch
"
