# Template file for 'fpc'
pkgname=fpc
version=3.0.2
revision=1
create_wrksrc=yes
build_wrksrc="${pkgname}build-${version}"
conf_files="/etc/fpc.cfg /etc/fppkg.cfg"
hostmakedepends="rpmextract"
makedepends="ncurses-devel zlib-devel expat-devel"
short_desc="Free Pascal Compiler"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="GPL-2, LGPL-2.1, FPC-FPR"
homepage="http://freepascal.org/"
distfiles="http://gd.tuwien.ac.at/languages/pascal/${pkgname}/dist/${version}/source/${pkgname}build-${version}.tar.gz"
checksum=f261ed0a361f190169fff428d65b707df1507df317fc0ed101b0e63596541b75
case "$XBPS_TARGET_MACHINE" in
*-musl*)
	broken="https://build.voidlinux.eu/builders/x86_64-musl_builder/builds/1375/steps/shell_3/logs/stdio"
	;;
x86_64*)
	distfiles+=" http://gd.tuwien.ac.at/languages/pascal/${pkgname}/dist/${version}/x86_64-linux/${pkgname}-${version}-1.x86_64.rpm"
	checksum+=" 85ad192927b2129022c27afbd6a6fc657a569ccfb43120e4868fea7812c10c52"
	;;
i686)
	distfiles+=" http://gd.tuwien.ac.at/languages/pascal/${pkgname}/dist/${version}/i386-linux/${pkgname}-${version}-1.i686.rpm"
	checksum+=" 0f049ff901305c315f90ec4c80fd3b054b97b1d8fca1552bcb036299aa64338f"
	;;
esac
# TODO: figure out cross-build and how to unwrap the ARM .tar.
nocross=yes
nopie=yes
only_for_archs="x86_64 x86_64-musl i686 i686-musl"

post_extract() {
	# relative links needed
	ln -sf ../lib64/fpc/${version}/ppcx64 usr/bin
	ln -sf ../lib/fpc/${version}/ppc386 usr/bin

	# tweak PT_INTERP for musl targets
	case "$XBPS_TARGET_MACHINE" in
	x86_64-musl)
		sed -i s,/lib64/ld-linux-x86-64.so.2,/lib/ld-musl-x86_64.so.1, $build_wrksrc/fpcsrc/compiler/systems/t_linux.pas
		;;
	i686-musl)
		sed -i s,/lib/ld-linux.so.2,/lib/ld-musl-i386.so.1, $build_wrksrc/fpcsrc/compiler/systems/t_linux.pas
		;;
	esac
}

do_build() {
	FPC=$wrksrc/usr/bin/fpc make ${makejobs} all NOGDB=1
}

do_install() {
	FPC=$wrksrc/usr/bin/fpc make install NOGDB=1 \
		PREFIX=$DESTDIR/usr INSTALL_MANDIR=$DESTDIR/usr/share/man
	ln -srf $DESTDIR/usr/lib/fpc/${version}/ppc* $DESTDIR/usr/bin
	vmkdir etc
	PATH=$DESTDIR/usr/bin:$PATH \
		$DESTDIR/usr/lib/fpc/${version}/samplecfg \
			$DESTDIR/usr/lib/fpc/${version} $DESTDIR/etc
	sed -i "s,${DESTDIR},,g" $DESTDIR/etc/*.cfg $DESTDIR/etc/fppkg/*
	vlicense fpcsrc/rtl/COPYING.FPC
}

