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

pkgname=fpc
pkgver=2.4.0
pkgrel=1ab
#arch=x86_64
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.0/fpc-2.4.0.source.tar.gz"
        "http://downloads.sourceforge.net/project/freepascal/Linux/2.4.0/fpc-2.4.0.${arch/i?86/i386}-linux.tar")
sourcetemplate="http://gaia.homelinux.org/salix/packages-x86_64/$pkgname/$pkgver/"
#docs=("readme" "install" "copying" "changelog" "authors" "news" "todo")
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() {
	threadcount=$(($(sed -ne '/cpu cores/h;${g;s/[^0-9]*//p}' \
						< /proc/cpuinfo)*2))
	if [ $threadcount -lt 2 ]; then
		threadcount=2
	elif [ $threadcount -gt 8 ]; then
		threadcount=8
	fi
	threads="-j${threadcount}"
  
  case "$arch" in
    i?86)
      srcarch=i386
      shortarch=386
      ;;
    x86_64)
      srcarch="$arch"
      shortarch=x64
      ;;
  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 $threads 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 possible creates $HOME/.{fp,fpc.cfg{,.orig}} too, but no way to avoid
  bash $startdir/src/fpc-2.4.0/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/
} 
