# Contributor: Michael Mason <ms13sp@gmail.com>
# Contributor: Gabriele Santomaggio <g.santomaggio@gmail.com>
# Contributor: Marlus Saraiva <marlus.saraiva@gmail.com>
# Maintainer: William Pitcock <nenolod@dereferenced.org>
pkgname=erlang
pkgver=20.3.1
_srcver=$pkgver
pkgrel=0
pkgdesc="General-purpose programming language and runtime environment"
url="http://www.erlang.org/"
license="Apache-2.0"
arch="all"
depends="$pkgname-kernel $pkgname-stdlib $pkgname-compiler"
makedepends="perl-dev zlib-dev ncurses-dev libressl-dev openjdk8 unixodbc-dev
	autoconf wxgtk-dev glu-dev"
subpackages="$pkgname-dev
             $pkgname-asn1:asn
	     $pkgname-common-test:common_test
	     $pkgname-compiler:compiler
	     $pkgname-cosevent:cos_event
	     $pkgname-coseventdomain:cos_event_domain
	     $pkgname-cosfiletransfer:cos_file_transfer
	     $pkgname-cosnotification:cos_notification
	     $pkgname-cosproperty:cos_property
	     $pkgname-costime:cos_time
	     $pkgname-costransaction:cos_transactions
	     $pkgname-crypto:crypto
	     $pkgname-debugger:debugger
	     $pkgname-dialyzer:dialyzer
	     $pkgname-diameter:diameter
	     $pkgname-edoc:edoc
	     $pkgname-eldap:eldap
	     $pkgname-erl-docgen:erl_docgen
	     $pkgname-erl-interface:erl_interface
	     $pkgname-erts:erts
	     $pkgname-et:et
	     $pkgname-eunit:eunit
	     $pkgname-hipe:hipe
	     $pkgname-ic:ic
	     $pkgname-inets:inets
	     $pkgname-jinterface:jinterface
	     $pkgname-kernel:kernel
	     $pkgname-megaco:megaco
	     $pkgname-mnesia:mnesia
	     $pkgname-observer:observer
	     $pkgname-odbc:odbc
	     $pkgname-orber:orber
	     $pkgname-os-mon:os_mon
	     $pkgname-otp-mibs:otp_mibs
	     $pkgname-parsetools:parsetools
	     $pkgname-public-key:public_key
	     $pkgname-reltool:reltool
	     $pkgname-runtime-tools:runtime_tools
	     $pkgname-sasl:sasl
	     $pkgname-snmp:snmp
	     $pkgname-ssh:ssh
	     $pkgname-ssl:ssl
	     $pkgname-stdlib:stdlib
	     $pkgname-syntax-tools:syntax_tools
	     $pkgname-tools:tools
	     $pkgname-xmerl:xmerl
	     $pkgname-wx:wx"
options="!check"
source="https://github.com/erlang/otp/archive/OTP-$_srcver.tar.gz
        0005-Do-not-install-nteventlog-and-related-doc-files-on-n.patch
        0010-fix-nteventlog-remove.patch"

builddir="$srcdir/otp-OTP-$_srcver"

prepare() {
	default_prepare || return 1

	cd "$builddir"
	#rm lib/os_mon/ebin/*
}

build() {
	cd "$builddir"
	export CPPFLAGS="-D_BSD_SOURCE $CPPFLAGS"
	export PATH="/usr/lib/jvm/java-1.8-openjdk/bin:$PATH"
	./otp_build autoconf
	./configure --prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--host="$CHOST" \
		--build="$CBUILD" \
		--enable-threads \
		--enable-shared-zlib \
		--enable-ssl=dynamic-ssl-lib \
		|| return 1
	make -j1 || return 1
}

package() {
	cd "$builddir"
	make -j1 DESTDIR="$pkgdir" install || return 1
}

_mv_erlang_lib() {
	local lib=$1
	case "$depends" in
	*$subpkgname*)	depends="";;
	*)		depends="$pkgname=$pkgver-r$pkgrel";;
	esac

	mkdir -p "$subpkgdir"/usr/lib/erlang/lib
        rm -f "$pkgdir"/usr/lib/erlang/lib/$lib-*/src/*.erl
	mv "$pkgdir"/usr/lib/erlang/lib/$lib-* "$subpkgdir"/usr/lib/erlang/lib/
}

asn() { _mv_erlang_lib asn1; depends="$depends erlang-crypto"; }
common_test() { _mv_erlang_lib common_test; }
compiler() { _mv_erlang_lib compiler; depends="$depends erlang-syntax-tools erlang-parsetools erlang-erl-interface"; }
cos_event() { _mv_erlang_lib cosEvent; }
cos_event_domain() { _mv_erlang_lib cosEventDomain; }
cos_file_transfer() { _mv_erlang_lib cosFileTransfer; }
cos_notification() { _mv_erlang_lib cosNotification; }
cos_property() { _mv_erlang_lib cosProperty; }
cos_time() { _mv_erlang_lib cosTime; }
cos_transactions() { _mv_erlang_lib cosTransactions; }
crypto() { _mv_erlang_lib crypto; }
debugger() { _mv_erlang_lib debugger; }
dialyzer() { _mv_erlang_lib dialyzer; }
diameter() { _mv_erlang_lib diameter; }
edoc() { _mv_erlang_lib edoc; }
eldap() { _mv_erlang_lib eldap; }
erl_docgen() { _mv_erlang_lib erl_docgen; }
erl_interface() { _mv_erlang_lib erl_interface; }
erts() { _mv_erlang_lib erts; }
et() { _mv_erlang_lib et; }
eunit() { _mv_erlang_lib eunit; depends="$depends erlang-common-test"; }
hipe() { _mv_erlang_lib hipe; }
ic() { _mv_erlang_lib ic; }
inets() { _mv_erlang_lib inets; }
jinterface() { _mv_erlang_lib jinterface; }
kernel() { _mv_erlang_lib kernel; }
megaco() { _mv_erlang_lib megaco; }
mnesia() { _mv_erlang_lib mnesia; }
observer() { _mv_erlang_lib observer; }
odbc() { _mv_erlang_lib odbc; }
orber() { _mv_erlang_lib orber; }
os_mon() { _mv_erlang_lib os_mon; }
otp_mibs() { _mv_erlang_lib otp_mibs; }
parsetools() { _mv_erlang_lib parsetools; }
public_key() { _mv_erlang_lib public_key; depends="$depends erlang-asn1"; }
reltool() { _mv_erlang_lib reltool; }
runtime_tools() { _mv_erlang_lib runtime_tools; }
sasl() { _mv_erlang_lib sasl; depends="$depends erlang-inets"; }
snmp() { _mv_erlang_lib snmp; depends="$depends erlang-inets"; }
ssh() { _mv_erlang_lib ssh; depends="$depends erlang-public-key erlang-inets"; }
ssl() { _mv_erlang_lib ssl; depends="$depends erlang-public-key erlang-inets"; }
stdlib() { _mv_erlang_lib stdlib; }
syntax_tools() { _mv_erlang_lib syntax_tools; }
tools() { _mv_erlang_lib tools; }
xmerl() { _mv_erlang_lib xmerl; }
wx() { _mv_erlang_lib wx; }

dev() {
	set -x
	local i= j=
	depends="$pkgname=$pkgver-r$pkgrel $depends_dev"
	pkgdesc="$pkgdesc (development files)"

	cd "$pkgdir" || return 0
	local libdirs=usr/
	[ -d lib/ ] && libdirs="lib/ $libdirs"
	for i in usr/include usr/lib/pkgconfig usr/share/aclocal\
			usr/share/gettext usr/bin/*-config	\
			usr/share/vala/vapi usr/share/gir-[0-9]*\
			usr/share/qt*/mkspecs			\
			usr/lib/qt*/mkspecs			\
			usr/lib/cmake				\
			$(find . -name include -type d) 	\
			$(find $libdirs -name '*.[acho]' \
				-o -name '*.prl' 2>/dev/null); do
		if [ -e "$pkgdir/$i" ] || [ -L "$pkgdir/$i" ]; then
			d="$subpkgdir/${i%/*}"	# dirname $i
			mkdir -p "$d"
			mv "$pkgdir/$i" "$d"
			rmdir "$pkgdir/${i%/*}" 2>/dev/null || true
		fi
	done
	# move *.so links needed when linking the apps to -dev packages
	for i in lib/*.so usr/lib/*.so; do
		if [ -L "$i" ]; then
			mkdir -p "$subpkgdir"/"${i%/*}"
			mv "$i" "$subpkgdir/$i" || return 1
		fi
	done
	return 0
}


sha512sums="fc63df0d7e057d37930c82f5b995b4c500c0cee64bb2f91837409b143a192c9157ae8c159d25d2848cc74fe63019db7a5b1b39d41b0fb2bda6a7f8bd41d229c5  OTP-20.3.1.tar.gz
5d377faccd73382bc86c5aa3182767bc5d1639220c78c2f624135f597f3c823a6871ff13f6f8a109baa8a9ae5d215233b40193e5cfe07af275aa53f327e956de  0005-Do-not-install-nteventlog-and-related-doc-files-on-n.patch
bb4346dabe17115bc310837c5f0aeb367a745d8ba2159495084e599d0419fc57648d144c811306914ac48d0e087d6150a356f38640ba070642b4578acc5fe573  0010-fix-nteventlog-remove.patch"
