SUMMARY="C-style arbitrary precision calculator"
DESCRIPTION="Calc is an interactive calculator which provides for easy large \
numeric calculations, but which also can be easily programmed for difficult or \
long calculations. It can accept a command line argument, in which case it \
executes that single command and exits. Otherwise, it enters interactive mode. \
In this mode, it accepts commands one at a time, processes them, and displays \
the answers. In the simplest case, commands are simply expressions which are \
evaluated."
HOMEPAGE="http://www.isthe.com/chongo/tech/comp/calc/"
COPYRIGHT="1999-2017 Landon Curt Noll"
LICENSE="GNU LGPL v2.1"
REVISION="1"
SOURCE_URI="http://www.isthe.com/chongo/src/calc/calc-$portVersion.tar.bz2"
CHECKSUM_SHA256="57af44181ca3af7348c82ee628cfd221677a09fef11a29d2e5667726d5aafc90"
PATCHES="calc-$portVersion.patchset"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

libVersion=$portVersion

PROVIDES="
	calc$secondaryArchSuffix = $portVersion
	cmd:calc$secondaryArchSuffix = $portVersion
	lib:libcalc$secondaryArchSuffix = $libVersion
	lib:libcustcalc$secondaryArchSuffix = $libVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libhistory$secondaryArchSuffix
	lib:libncurses$secondaryArchSuffix
	lib:libreadline$secondaryArchSuffix
	calc_data == $portVersion
	"

if [ -z "$secondaryArchSuffix" ]; then
	SUMMARY_data="Standard resource files for calc"
	ARCHITECTURES_data="any"
	PROVIDES_data="
		calc_data = $portVersion
		"
	REQUIRES_data=""
fi

PROVIDES_devel="
	calc${secondaryArchSuffix}_devel = $portVersion
	devel:libcalc$secondaryArchSuffix = $libVersion
	devel:libcustcalc$secondaryArchSuffix = $libVersion
	"
REQUIRES_devel="
	calc$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libhistory$secondaryArchSuffix >= 8
	devel:libncurses$secondaryArchSuffix
	devel:libreadline$secondaryArchSuffix >= 8
	"
if [ -n "$secondaryArchSuffix" ]; then
BUILD_REQUIRES+="
	calc_data == $portVersion
	"
fi
BUILD_PREREQUIRES="
	cmd:awk
	cmd:cmp
	cmd:ctags
	cmd:diff
	cmd:egrep
	cmd:less
	cmd:make
	cmd:gcc$secondaryArchSuffix
	cmd:sed
	cmd:strip$secondaryArchSuffix
	cmd:touch
	cmd:xargs
	"

defineDebugInfoPackage calc$secondaryArchSuffix \
	"$binDir"/calc \
	"$libDir"/libcalc.so.$libVersion \
	"$libDir"/libcustcalc.so.$libVersion

BUILD()
{
	make target=Haiku \
		INCDIR=$includeDir \
		LIBDIR=$libDir \
		BINDIR=$binDir \
		SCRIPTDIR=$libDir/calc/cscript \
		CALC_SHAREDIR=$dataDir/calc \
		MANDIR=$manDir/man1 \
		BLD_TYPE=calc-dynamic-only \
		HAVE_UNISTD_H=YES \
		HAVE_FPOS_POS=1 \
		HAVE_UID_T=1 \
		HAVE_USTAT=1 \
		EXTRA_CFLAGS="-DUSE_TERMIOS" \
	 	USE_READLINE=-DUSE_READLINE \
		READLINE_LIB=-lreadline \
		READLINE_EXTRAS="-lhistory -lncurses" \
		LN="ln -s" \
		LDCONFIG= \
		WNO_ERROR_LONG_LONG=
}

INSTALL()
{
	make install \
		target=Haiku \
		INCDIR=$includeDir \
		LIBDIR=$libDir \
		BINDIR=$binDir \
		SCRIPTDIR=$libDir/calc/cscript \
		CALC_SHAREDIR=$dataDir/calc \
		MANDIR=$manDir/man1 \
		BLD_TYPE=calc-dynamic-only \
		HAVE_UNISTD_H=YES \
		HAVE_FPOS_POS=1 \
		HAVE_UID_T=1 \
		HAVE_USTAT=1 \
		EXTRA_CFLAGS="-DUSE_TERMIOS" \
	 	USE_READLINE=-DUSE_READLINE \
		READLINE_LIB=-lreadline \
		READLINE_EXTRAS="-lhistory -lncurses" \
		LN="ln -s" \
		LDCONFIG= \
		WNO_ERROR_LONG_LONG=

	prepareInstalledDevelLibs libcalc libcustcalc
	fixPkgconfig

	if [ -z "$secondaryArchSuffix" ]; then
		# Shorten some paths in the man page.
		sed -i \
			-e "s|#!$binDir/calc\>|#!/$relativeBinDir/calc|" \
			-e "s|$prefix/|/system/|" \
			$manDir/man1/calc.1

		# Some files in $dataDir/calc/help/ have code snippets with
		# long shebangs that we can shorten. Currently, these files are
		# cscript, full, libcalc and usage.
		sed -i \
			-e "s|$binDir/calc\>|/$relativeBinDir/calc|" \
			-e "s|$prefix/|/system/|g" \
			$dataDir/calc/help/*

		packageEntries data \
			$dataDir/calc
	else
		rm -rf $dataDir/calc $manDir
	fi

	# Shorten the paths to bin/calc in several shebangs.
	sed -i -e "1 s|$binDir/calc\>|/$relativeBinDir/calc|" \
		$libDir/calc/cscript/*

	packageEntries devel \
		$developDir
}

TEST()
{
	make check
}
