# Copyright 2007-08 Peter A. Gustafson petegus at umich.edu
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils

MY_P=ccx_${PV}

DESCRIPTION="A Free Software Three-Dimensional Structural Finite Element Program"
HOMEPAGE="http://www.calculix.de/"
SRC_URI="http://www.dhondt.de/${MY_P}.src.tar.bz2"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="spooles arpack lapack threads"

DEPEND="spooles? (>=sci-libs/spooles-2.2)
	arpack? (>=sci-libs/arpack-96)
	lapack? (virtual/lapack)
	virtual/blas"

## Notes
## spooles: provides an improved solver over the built-in solver
## arpack: provides the ability to do *buckle and *frequency calculations
## 	   via eigenvalue/eigenvector calculations
## lapack: link to gentoo supplied lapack instead of included lapack

src_compile () {
	cd ${WORKDIR}

	epatch "${FILESDIR}"/01_${MY_P}_Makefile.patch

	if use lapack; then
	## The version of lapack shipped with ccx enters an infinite
	## loop when doing *freqency or *buckle calculations.
	## Use local lapack instead.
		export LAPACK=`pkg-config --libs lapack`
		epatch "${FILESDIR}"/01_${MY_P}_lapack.patch
	fi

	export BLAS=`pkg-config --libs blas`

	if use spooles; then
	       export SPOOLESINC="-I /usr/lib/spooles/2.2/ -DSPOOLES"
	       export SPOOLESLIB=/usr/lib/spooles/2.2/spooles.a
	       if use threads; then
	       	  export USE_MT=-DUSE_MT
	       	  export SPOOLESLIB="/usr/lib/spooles/2.2/spoolesMT.a /usr/lib/spooles/2.2/spooles.a"
	       fi	  
        else
	       if use threads; then
	       	       die ERROR: the threads USE flag requires spooles
	       fi
        fi     

	if use arpack; then
	       export ARPACK=-DARPACK
	       export ARPACKLIB=/usr/lib/libarpack.a
	fi

	cd ${WORKDIR}/CalculiX/${MY_P}/src/
	emake || die ERROR on compile
}

src_install () {
	cd ${WORKDIR}/CalculiX/${MY_P}/src/
	dobin ${MY_P}
	dosym ${MY_P} /usr/bin/ccx
}