abinfo "Building Free Pascal ..."
(
    cd "$SRCDIR"/fpcsrc/compiler
    fpcmake -Tall
)
make build NOGDB=1

abinfo "Installing Free Pascal ..."
make -j1 install \
    PREFIX="$PKGDIR"/usr

abinfo "Installing compiler symlinks to /usr/bin ..."
if ab_match_arch amd64; then
    ln -sv ../lib/fpc/${PKGVER}/ppcx64 \
        "$PKGDIR"/usr/bin/
elif ab_match_arch arm64; then
    ln -sv ../lib/fpc/${PKGVER}/ppca64 \
        "$PKGDIR"/usr/bin/
elif ab_match_arch ppc64el; then
    ln -sv ../lib/fpc/${PKGVER}/ppcppc64 \
        "$PKGDIR"/usr/bin/
fi

abinfo "Creating sample configuration files ..."
mkdir -pv "$PKGDIR"/etc
"$PKGDIR"/usr/lib/fpc/${PKGVER}/samplecfg \
    "$PKGDIR"/usr/lib/fpc/${PKGVER} \
    "$PKGDIR"/etc

abinfo "Enabling -fPIC by default for amd64 ..."
echo -e \
    "#ifdef cpux86_64\n# for x86_64 use -fPIC by default\n-Cg\n#endif" \
    >> "$PKGDIR"/etc/fpc.cfg

abinfo "Moving man pages to correct location ..."
mv -v "$PKGDIR"/usr/man \
    "$PKGDIR"/usr/share/

abinfo 'Removing references to $PKGDIR in /etc ...'
find "$PKGDIR"/etc/ -type f \
    -exec sed -i "s|$PKGDIR||g" {} \;

abinfo "Installing Free Pascal sources ..."
make distclean
mkdir -pv "$PKGDIR"/usr/lib/fpc/src
cp -Rv "$SRCDIR"/fpcsrc/* \
    "$PKGDIR"/usr/lib/fpc/src
mkdir -pv "$PKGDIR"/usr/src
ln -sv /usr/lib/fpc/src \
    "$PKGDIR"/usr/src/fpc-$PKGVER
