SUMMARY="Software for graph, mesh and hypergraph partitioning"
DESCRIPTION="SCOTCH is a software package and libraries for graph, mesh and \
hypergraph partitioning, static mapping, and sparse matrix block ordering.
Its purpose is to apply graph theory, with a divide and \
conquer approach, to scientific computing problems such as graph and \
mesh partitioning, static mapping, and sparse matrix ordering, in \
application domains ranging from structural mechanics to operating \
systems or bio-chemistry.
The SCOTCH distribution is a set of programs and libraries which \
implement the static mapping and sparse matrix reordering algorithms \
developed within the SCOTCH project."
HOMEPAGE="http://www.labri.u-bordeaux.fr/perso/pelegrin/scotch/"
COPYRIGHT="2002-2020 Francois Pellegrini, LABRI"
LICENSE="CeCILL-C"
REVISION="1"
SOURCE_URI="https://gforge.inria.fr/frs/download.php/latestfile/298/scotch_$portVersion.tar.gz"
CHECKSUM_SHA256="622b4143cf01c480bb15708b3651b29c25e4aeb00c8c6447ff196aca2eca5c93"
PATCHES="scotch-$portVersion.patchset"
SOURCE_DIR="scotch_$portVersion"

ADDITIONAL_FILES="
	Makefile.inc.x86_64_pc_haiku.shlib
	Makefile.inc.x86_pc_haiku.shlib
	"

ARCHITECTURES="all !x86_gcc2 ?arm ?ppc ?sparc"
SECONDARY_ARCHITECTURES="x86"

commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

portVersionCompat="$portVersion compat >= ${portVersion%%.*}"
libVersion=$portVersion
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"

PROVIDES="
	scotch$secondaryArchSuffix = $portVersionCompat
	lib:libesmumps$secondaryArchSuffix = $libVersionCompat
	lib:libscotch$secondaryArchSuffix = $libVersionCompat
	lib:libscotcherr$secondaryArchSuffix = $libVersionCompat
	lib:libscotcherrexit$secondaryArchSuffix = $libVersionCompat
	lib:libscotchmetis$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:liblzma$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	scotch${secondaryArchSuffix}_devel = $portVersionCompat
	devel:libesmumps$secondaryArchSuffix = $libVersionCompat
	devel:libscotch$secondaryArchSuffix = $libVersionCompat
	devel:libscotcherr$secondaryArchSuffix = $libVersionCompat
	devel:libscotcherrexit$secondaryArchSuffix = $libVersionCompat
	devel:libscotchmetis$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	scotch$secondaryArchSuffix == $portVersion base
	"

PROVIDES_tools="
	scotch${secondaryArchSuffix}_tools = $portVersionCompat
	cmd:acpl$commandSuffix = $portVersionCompat
	cmd:amk_ccc$commandSuffix = $portVersionCompat
	cmd:amk_fft2$commandSuffix = $portVersionCompat
	cmd:amk_grf$commandSuffix = $portVersionCompat
	cmd:amk_hy$commandSuffix = $portVersionCompat
	cmd:amk_m2$commandSuffix = $portVersionCompat
	cmd:amk_p2$commandSuffix = $portVersionCompat
	cmd:atst$commandSuffix = $portVersionCompat
	cmd:gbase$commandSuffix = $portVersionCompat
	cmd:gcv$commandSuffix = $portVersionCompat
	cmd:gmap$commandSuffix = $portVersionCompat
	cmd:gmk_hy$commandSuffix = $portVersionCompat
	cmd:gmk_m2$commandSuffix = $portVersionCompat
	cmd:gmk_m3$commandSuffix = $portVersionCompat
	cmd:gmk_msh$commandSuffix = $portVersionCompat
	cmd:gmk_ub2$commandSuffix = $portVersionCompat
	cmd:gmtst$commandSuffix = $portVersionCompat
	cmd:gord$commandSuffix = $portVersionCompat
	cmd:gotst$commandSuffix = $portVersionCompat
	cmd:gout$commandSuffix = $portVersionCompat
	cmd:gpart$commandSuffix = $portVersionCompat
	cmd:gscat$commandSuffix = $portVersionCompat
	cmd:gtst$commandSuffix = $portVersionCompat
	cmd:mcv$commandSuffix = $portVersionCompat
	cmd:mmk_m2$commandSuffix = $portVersionCompat
	cmd:mmk_m3$commandSuffix = $portVersionCompat
	cmd:mord$commandSuffix = $portVersionCompat
	cmd:mtst$commandSuffix = $portVersionCompat
	"
REQUIRES_tools="
	scotch$secondaryArchSuffix == $portVersion base
	haiku$secondaryArchSuffix
	lib:liblzma$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:liblzma$secondaryArchSuffix
	devel:libbz2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:bison
	cmd:gcc$secondaryArchSuffix
	cmd:flex
	cmd:make
	"

TEST_REQUIRES="
	cmd:diff
	"

defineDebugInfoPackage scotch$secondaryArchSuffix \
	"$libDir"/libesmumps.so \
	"$libDir"/libscotch.so \
	"$libDir"/libscotcherr.so \
	"$libDir"/libscotcherrexit.so\
	"$libDir"/libscotchmetis.so

PATCH()
{
	# make install will fail because these binaries are missing
	# This could be caused by the ptscotch target which has been disabled on this release
	# Thus, this is not going to be a permanent patch, hence it is not
	# in the main patch set
	sed -i 's|-\$(CP) -f \.\./bin/d\[agm\]\*\$(EXE) \$(bindir)||g' src/Makefile
}

BUILD()
{
	cp "$portDir"/additional-files/Makefile.inc."${effectiveTargetArchitecture}"_pc_haiku.shlib src/Makefile.inc

	make -C src $jobArgs scotch esmumps
}


INSTALL()
{
	declare -a destdirs=("$commandBinDir" "$includeDir" "$includeDir" "$libDir" "$libDir"/pkgconfig "$dataDir" "$manDir")
	for i in "${destdirs[@]}"
	do
		mkdir -p ${i}
	done

	make -C src $jobArgs bindir=$commandBinDir includedir=$includeDir libdir=$libDir datarootdir=$dataDir mandir=$manDir install

	# create symlinks
	declare -a libraries=("libesmumps" "libscotch" "libscotcherr" "libscotcherrexit" "libscotchmetis")
	for soname in "${libraries[@]}"
	do
		ln -s ${libDir}/${soname}.so ${libDir}/${soname}.so.$libVersion
		ln -s ${libDir}/${soname}.so ${libDir}/${soname}.so.${libVersion%%.*}
	done

	cat <<-EOF > $libDir/pkgconfig/scotch.pc
		prefix=${prefix}
		exec_prefix=${prefix}
		libdir=\${prefix}/develop/lib
		includedir=\${prefix}/develop/headers
		Name: scotch
		Description: ${SUMMARY}
		Version: ${portVersion}
		URL: ${HOMEPAGE}
		Libs: -L\${libdir} -lscotcherr -lscotch
		Private: -lz -llzma -lbz2 -lm
		Cflags: -I\${includedir}
	EOF

	cat <<-EOF > $libDir/pkgconfig/scotchmetis.pc
		prefix=${prefix}
		libdir=\${prefix}/develop/lib
		includedir=\${prefix}/develop/headers
		Name: scotchmetis
		Description: ${SUMMARY}
		Version: ${portVersion}
		URL: ${HOMEPAGE}
		Libs: -L\${libdir} -lscotchmetis -lscotcherr -lscotch
		Private: -lz -llzma -lbz2 -lm
		Cflags: -I\${includedir}
	EOF

	prepareInstalledDevelLibs `printf '%s\n' "$(IFS=' '; printf '%s' "${libraries[*]}")"`
	fixPkgconfig

	# copy pdf documentation
	mkdir -p $developDocDir
	cp -rd doc/*.pdf $developDocDir

	# devel package
	packageEntries devel $developDir

	# tools package
	packageEntries tools $commandBinDir $manDir
}

TEST()
{
	export LIBRARY_PATH=$LIBRARY_PATH:$sourceDir/lib

	# The (non-standard) Makefile used to perform the tests has some relative paths in it which
	# won't work from HP (although they will from bare Terminal).
	# Makefile is patched just to have these paths overridable by command line
	sed -i 's|= \.\.\/\.\.\/|?= \.\.\/\.\.\/|g' src/check/Makefile

	# NOTE:
	# Tests are commented out because for two reasons:
	#	1. They don't work from HP (Makefile issue?)
	#	2. Some of them will hang (bug in source code and/or incompatibilities with Haiku?)

	SCOTCHBINDIR="$sourceDir/bin" \
		SCOTCHLIBDIR="$sourceDir/lib" \
		SCOTCHINCDIR="$sourceDir/include" \
		SCOTCHTGTDIR="$sourceDir/tgt" \
		make -C src check $jobArgs
}
