# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=elasticsearch
pkgver=6.1.0
pkgrel=0
pkgdesc="Open Source, Distributed, RESTful Search Engine"
url="https://www.elastic.co/products/elasticsearch"
arch="x86 x86_64"
license="Apache-2.0"
depends="java-jna-native>=4.1 openjdk8-jre bash"
makedepends=""
install="$pkgname.pre-install"
subpackages="$pkgname-doc"
source="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	README.alpine
	"
builddir="$srcdir/$pkgname-$pkgver"

default_module=transport-netty4
_modules="
	aggs-matrix-stats
	analysis-common
	ingest-common
	lang-expression
	lang-mustache
	lang-painless
	mapper-extras
	parent-join
	percolator
	reindex
	repository-url
	tribe
	"
for _mod in $_modules; do
	subpackages="$subpackages $pkgname-$_mod:_${_mod//-/_}"
	eval "_${_mod//-/_}() { _builtin_module $_mod; }"
done

_basedir="/usr/share/java/$pkgname"

build() {
	return 0
}

package() {
	local destdir="$pkgdir/$_basedir"
	local confdir="$pkgdir/etc/$pkgname"
	local docdir="$pkgdir/usr/share/doc/elasticsearch"
	local heapsize="256m"

	cd "$builddir"

	install -dm755 "$destdir"/lib "$destdir"/modules "$destdir"/bin
	install -m644 -t "$destdir"/lib lib/* || return 1

	install -dm755 "$docdir"
	install -dm755 "$confdir"
	install -m644 -t "$confdir" config/* || return 1

	# remove windows files
	find bin -type f -name *.bat -exec rm -f {} \;
	find bin -type f -name *.exe -exec rm -f {} \;

	# ES bin script parses the new jvm.options file
	install -m755 -t "$destdir"/bin bin/* || return 1

	# ES does not run without a transport module
	install -dm755 "$destdir/modules/$default_module"
	install -m644 -t "$destdir/modules/$default_module" modules/"$default_module"/* || return 1

	# reduce heap sizes
	sed -i \
	  -e "s|^-Xms.*|-Xms$heapsize|" \
	  -e "s|^-Xmx.*|-Xmx$heapsize|" \
	$confdir/jvm.options

	install -m755 -D "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname || return 1

	install -m644 -D "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/$pkgname || return 1

	install -m644 -D "$srcdir"/README.alpine \
		"$docdir" || return 1
}

_builtin_module() {
	local name="$1"
	local destdir="$subpkgdir/$_basedir/modules/$name"

	install -dm755 "$destdir"
	install -m644 -t "$destdir" "$builddir"/modules/$name/*
}

sha512sums="ba4829c7bb422b42bdc480797521399d9b39c6fd9bb2597585f8fdb0a261e8e11bd8e98b3df562b7320b2775643be9918b043d818caa79b0a6403c9c0657774e  elasticsearch-6.1.0.tar.gz
dc916861497d4a589d6b3ab70d90ff9970e4d57003ed82d08c5f90dec337f995dcabf2556b7a27ade926e846ad435392f7845acfe9280dce17b4c172d85328ae  elasticsearch.initd
2ab1baf1b5c8782f3f371ba221aadd3e841abc62175f0b1ddcfc68d711e2370465124e076f8cc2e549c25a1da9db8c90172b2f410bd6bbe4153f0e831620b6ba  elasticsearch.confd
6de81485cdc059afef58382862e4155482463fde0b604aaa8dbe904c778b841467c4a383a5e54acd09e3436f1fb7be9923e001fb77bd3d7894e113a5e0f4036b  README.alpine"
