# Compiling Time: 5.03 SBU
# Maintainer: Miklos Vajna <vmiklos@frugalware.org>

USE_JAVA=${USE_JAVA:-"y"}
USE_ODBC=${USE_ODBC:-"y"}

pkgname=erlang
pkgver=17.3
pkgrel=4
pkgdesc="A programming language designed at the Ericsson Computer Science Laboratory."
url="http://www.erlang.org/"
depends=('openssl>=1.0.0' 'ncurses>=6.0-3')
makedepends=('fop')
Fuse $USE_JAVA && makedepends=($makedependes 'openjdk')
Fuse $USE_ODBC && makedepends=($makedependes 'unixodbc')
groups=('devel-extra')
archs=('i686' 'x86_64')
_F_archive_name="otp_src"
Fpkgversep="_"
_F_archive_prefix="R"
up2date="Flasttar http://www.erlang.org/download.html"
source=(http://www.erlang.org/download/otp_src_${pkgver/_/-}.tar.gz \
	http://www.erlang.org/download/otp_doc_html_${pkgver/_/-}.tar.gz \
	http://www.erlang.org/download/otp_doc_man_${pkgver/_/-}.tar.gz)
sha1sums=('64d6e29fff076543d29948b5590b77409d53a7cf' \
          'fee5762225ef990e8c07aa4baa563a57208b0198' \
          '3f7717186f572bb6431e1a1e5bc6c0f5ffd53171')

subpkgs=('wxerlang')
subdescs=('Erlang bindings for wxWidgets API.')
subdepends=('wxwidgets>=3.1.0')
subrodepends=("$pkgname=$pkgver")
subgroups=('devel-extra')
subarchs=('i686 x86_64')

if Fuse $USE_ODBC; then
	subpkgs=("${subpkgs[@]}" 'erlang-odbc')
	subdescs=("${subdescs[@]}" 'The Erlang ODBC application.')
	subdepends=("${subdepends[@]}" '')
	subrodepends=("${subrodepends[@]}" "$pkgname=$pkgver")
	subgroups=("${subgroups[@]}" 'devel-extra')
	subarchs=("${subarchs[@]}" "i686 x86_64")
fi

build()
{
	unset MAKEFLAGS
	Fcd otp_src_${pkgver/_/-}
	Fbuild
	ertsver=`sed -n '/^VSN/s/.*= //p' erts/vsn.mk`
	interfacever=`sed -n '/^EI_VSN/s/.*= //p' lib/erl_interface/vsn.mk`
	# remove references to Fdestdir
	Fsed "$Fdestdir" "" $Fdestdir/usr/lib/erlang/bin/erl
	Fsed "$Fdestdir" "" $Fdestdir/usr/lib/erlang/bin/start
	grep -rle "$Fdestdir" $Fdestdir//usr/lib/erlang/erts-$ertsver | xargs sed -i -e "s|$Fdestdir||g"
	# create symlinks to /usr
	Fmkdir /usr/bin
	Fln /usr/lib/erlang/bin/erl /usr/bin/erl
	Fln /usr/lib/erlang/bin/erlc /usr/bin/erlc
	Fln /usr/lib/erlang/bin/escript /usr/bin/escript
	Fln /usr/lib/erlang/lib/erl_interface-$interfacever/bin/erl_call /usr/bin/erl_call
	Fln /usr/lib/erlang/erts-$ertsver/bin/beam /usr/bin/beam
	# man pages
	cd $Fsrcdir
	for i in man/man* ; do
		Fmkdir /usr/share/${i}erl
	done
	for i in man/man*/*.[1-9]; do
		# avoid namespace collisions
		mv $i ${i}erl
		Ffilerel ${i}erl /usr/share/man/man${i##*.}erl/
	done
	# html doc
	mkdir html
	cd html
	Fextract $Fsrcdir/otp_doc_html_${pkgver/_/-}.tar.gz
	cd ..
	Fmkdir /usr/share/doc/$pkgname-$pkgver
	Fcp html /usr/share/doc/$pkgname-$pkgver/html
	Fsplit wxerlang usr/lib/erlang/lib/wx-*
	if Fuse $USE_ODBC; then
		Fsplit erlang-odbc usr/lib/erlang/lib/odbc-*
	fi
}

# optimization OK
