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

pkgname=fpc
pkgver=2.4.4
pkgrel=1ab
case "$( uname -m )" in
	i?86) export arch=i486 ;;
	arm*) export arch=arm ;;
	*) export arch=$( uname -m ) ;;
esac
echo "Automatically setting arch to $arch"
source=("http://downloads.sourceforge.net/project/freepascal/Source/2.4.4/fpc-2.4.4.source.tar.gz"
        "http://downloads.sourceforge.net/project/freepascal/Linux/2.4.4/fpc-2.4.4.${arch/i?86/i386}-linux.tar")
sourcetemplate="http://gaia.homelinux.org/salix/slkbuilds/$pkgname/"
url='http://www.freepascal.org/'

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"$pkgname - Free Pascal compiler"
"Free Pascal (aka FPK Pascal) is a 32 and 64 bit professional Pascal"
"compiler. It is available for different processors: Intel x86,"
"Amd64/x86_64, PowerPC, PowerPC64, Sparc, ARM.. The discontinued 1.0"
"version also supports the Motorola 680x0. The following operating"
"systems are supported: Linux, FreeBSD, Haiku, Mac OS X/Darwin, DOS,"
"Win32, Win64, WinCE, OS/2, Netware (libc and classic) and MorphOS."
)


build() {
	set -e

	case "$arch" in
	i?86)
		srcarch=i386
		shortarch=386
		;;
	x86_64)
		srcarch="$arch"
		shortarch=x64
		;;
	default)
		echo "ERROR: Unsupported arch."
		return 1
		;;
	esac


	# bootstrap compiler
	cd $startdir/src
	tar -xf binary.$srcarch-linux.tar base.$srcarch-linux.tar.gz
	tar -xf base.$srcarch-linux.tar.gz lib/fpc/$pkgver/ppc$shortarch -O >ppc$shortarch
	chmod +x ppc$shortarch

	cd $startdir/src/fpc-$pkgver
	INSTALLOPTS="INSTALL_PREFIX=$startdir/pkg/usr \
		INSTALL_LIBDIR=$startdir/pkg/usr/lib${LIBDIRSUFFIX} \
		INSTALL_BASEDIR=$startdir/pkg/usr/lib${LIBDIRSUFFIX}/fpc/${pkgver}"
	make all -j $numjobs PP=$startdir/src/ppc$shortarch ${INSTALLOPTS} || return 1
	make install PP=compiler/ppc$shortarch ${INSTALLOPTS} || return 1

	# post fixes
	install -vd $startdir/pkg/etc
	# this possibly creates $HOME/.{fp,fpc.cfg{,.orig}} too, but no way to avoid
	bash $startdir/src/fpc-${pkgver}/compiler/utils/samplecfg \
		/usr/lib${LIBDIRSUFFIX}/fpc/$pkgver $startdir/pkg/etc \
		2>&1	| tee $startdir/src/cfglog
	grep -q "$HOME/.fp" $startdir/src/cfglog && rm -vr "$HOME/.fp"
	#install -vD -m644 $startdir/src/fpc.cfg $startdir/pkg/etc/fpc.cfg
	install -vd $startdir/pkg/usr/doc
	mv $startdir/pkg/usr/share/doc/fpc-$pkgver/ $startdir/pkg/usr/doc/fpc-$pkgver/
	rmdir $startdir/pkg/usr/share/doc
	ln -s ../lib${LIBDIRSUFFIX}/fpc/$pkgver/ppc$shortarch $startdir/pkg/usr/bin/

	set +e
}