wget -c http://downloads.sourceforge.net/project/freepascal/Source/3.0.2/fpc-3.0.2.source.tar.gz
tar -xzf fpc-3.0.2.source.tar.gz
cd fpc-3.0.2

# FPC depends on FPC, so to make this work we need to download a binary FPC. As a safety measure we can create a chroot to limit the blob's reach
wget -c "http://downloads.sourceforge.net/project/freepascal/Linux/3.0.0/fpc-3.0.0.`uname -m | sed -e 's/^i[2-9]86/i386/'`-linux.tar"
tar -xf fpc-3.0.0.*-linux.tar
cd fpc-3.0.0.*-linux
tar -xf binary.*-linux.tar "base.`uname -m | sed -e 's/^i[2-9]86/i386/'`-linux.tar.gz"
# Create a minimal environment based on our own system
mkdir -p chroot/bin
mkdir -p chroot/lib
cp -rl /lib/*.so* chroot/lib
cp -rl /lib/*.a chroot/lib
cp -Ll /bin/sh /bin/su /usr/bin/make /bin/pwd /bin/date /bin/echo /bin/rm /bin/mkdir /bin/chown /usr/bin/ld /bin/mv /bin/cp /usr/bin/diff chroot/bin
cp -Ll /usr/lib/libncurses*.so* chroot/lib
mkdir -p chroot/usr/src/fpc
# Install the binary FPC into chroot
(cd chroot/usr; tar -xzf ../../base.*-linux.tar.gz)
cp -L chroot/usr/lib/fpc/3.0.0/ppc* chroot/bin
mkdir -p chroot/etc
# Set up a non-root user
echo 'root::0:0:root:/root:/bin/sh' > chroot/etc/passwd
echo 'chroot::1:1:chroot:/:/bin/sh' >> chroot/etc/passwd
touch chroot/etc/login.defs
mkdir -p chroot/dev
mount /dev chroot/dev -o bind # as root
mount .. chroot/usr/src/fpc -o bind # as root
chroot chroot chown -R chroot /usr # as root
# Here is where we actually compile fpc (in a path within the chroot, bound to our real FPC source directory)
chroot chroot /bin/su chroot -c 'cd /usr/src/fpc; make all' # as root
umount chroot/dev # as root
umount chroot/usr/src/fpc # as root
cd ..

make install FPC=compiler/utils/fpc FPC_COMPILERINFO="3.0.2 `uname -m | sed -e 's/^i[2-9]86/i386/'` `uname -m | sed -e 's/^i[2-9]86/i386/'` linux linux" INSTALL_PREFIX="/usr"
install -m 755 compiler/ppc386 /usr/bin
find /usr/lib/fpc/*/units/*-linux -type d | sed -e 's/^/ -Fu/' > /etc/fpc.cfg # as root