#Packager: Shador <futur~DOT~andy~AT~googlemail.com>

pkgname=erlang-otp
pkgver=R13B04
pkgrel=1ab
#arch=i486
source=('http://ftp.sunet.se/pub/lang/erlang/download/otp_src_R13B04.tar.gz'
        'http://ftp.sunet.se/pub/lang/erlang/download/otp_doc_html_R13B04.tar.gz'
        'http://ftp.sunet.se/pub/lang/erlang/download/otp_doc_man_R13B04.tar.gz')
sourcetemplate="http://gaia.homelinux.org/salix/packages/$pkgname/$pkgver/"
#docs=("readme" "authors" "eplicence")
options=("noextract")
url='http://ftp.sunet.se/pub/lang/erlang/'

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"$pkgname (a programming language and runtime environment)"
"Erlang is a general-purpose programming language and runtime"
"environment. Erlang has built-in support for concurrency, distribution"
"and fault tolerance. Erlang is used in several large telecommunication"
"systems from Ericsson. Erlang is available as open source from"
"http://www.erlang.org."
)


build() {
  set -e
	threadcount=$(($(sed -ne '/cpu cores/h;${g;s/[^0-9]*//p}' \
						< /proc/cpuinfo)*2))
	if [ $threadcount -lt 2 ]; then
		threadcount=2
	elif [ $threadcount -gt 8 ]; then
		threadcount=8
	fi
	threads="-j${threadcount}"
  
  cd $startdir/src
  for archive in *.tar.gz; do
    dir=`basename $archive .tar.gz`
    install -vd $dir
    cd $dir
      tar -xf ../$archive
    cd -
  done
  
	cd $startdir/src/otp_src_$pkgver/otp_src_$pkgver
	
	./configure \
		--prefix=/usr \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--localstatedir=/usr/var \
		--sysconfdir=/usr/etc \
		--mandir=/usr/man \
    --enable-threads \
    --enable-smp-support \
    --enable-kernel-poll \
    --enable-hipe \
    --enable-dynamic-ssl-lib \
    --enable-shared-zlib || return 1
  
  make $threads || make $threads || make || return 1
  make install DESTDIR=$startdir/pkg || return 1
  
  # install documentation
  install -vd $startdir/pkg/usr/lib${LIBDIRSUFFIX}/erlang/
  cp -vr $startdir/src/otp_doc_html_$pkgver/* $startdir/pkg/usr/lib${LIBDIRSUFFIX}/erlang/
  # install manpages
  cp -vr $startdir/src/otp_doc_man_$pkgver/* $startdir/pkg/usr/lib${LIBDIRSUFFIX}/erlang/
  
  # install license and stuff
  install -vd $startdir/pkg/usr/doc/$pkgname-$pkgver/
  install -vm644 README.md AUTHORS EPLICENCE \
      $startdir/pkg/usr/doc/$pkgname-$pkgver/
  
  # compress man pages
  for man in `find $startdir/pkg/usr/lib${LIBDIRSUFFIX}/erlang/man -type f`; do
    gzip -9 $man
  done
  
  # fix non-root owned files
  chown -vR root: $startdir/pkg/*
  set +e
} 
