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

pkgname=erlang-otp
pkgver=R13B03
pkgrel=1ab
arch=x86_64
source=('http://ftp.sunet.se/pub/lang/erlang/download/otp_src_R13B03.tar.gz'
        'http://ftp.sunet.se/pub/lang/erlang/download/otp_doc_html_R13B03.tar.gz'
        'http://ftp.sunet.se/pub/lang/erlang/download/otp_doc_man_R13B03.tar.gz')
sourcetemplate="http://gaia.homelinux.org/salix/packages-x86_64/$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
  
  failcount=0
  while true; do
    failed=""
    
      make $threads || failed="true"
      
    if [ "$failed" == "true" ]; then
      ((failcount++))  # failure, remember
    else
      break  # success, go on normally
    fi
    test "$failcount" -gt "2" && return 1 # third fail -> abort
    test "$failcount" -gt "1" && threads="" # if we twice disable paralell build
    continue # if we execute this we've failed at least once
  done
  
  # "hack" in destdir
  sed -e "s;\(prefix\|.*MAN1DIR\|.*LIBDIR\)\(.*=.*\)/usr\(.*\);\1\2$startdir/pkg/usr\3;" -i Makefile
  sed -e "s;^\(BINDIR.*=\).*;\1 $startdir/pkg/usr/bin;" -i Makefile
  sleep 5s
	make install prefix=$destdir || return 1
  
  # fix symbolic links
  for i in `find $startdir/pkg/ -type l`; do
    target="`readlink -f $i | sed -e "s;^$startdir/pkg;;"`" # strip destdir
    ln -vsf $target $i
  done
  
  # clean scripts
  for i in `grep -Ir "$startdir" $startdir/pkg 2>/dev/null | sed -e 's/\([^:]*\):.*$/\1/'`; do
    sed -e "s;$startdir/pkg;;" -i $i
  done
  
  # 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 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
} 
