#!/bin/bash

set -e

# Shallow clone of FPC
git clone --depth=1 https://gitlab.com/freepascal.org/FPC/source.git fpcsource
# Build FPC and install to seperate folder
make -C fpcsource all -j$(numcpus)
make -C fpcsource install INSTALL_PREFIX=$(realpath ./fpc)
# Cleanup to save storage space
git -C fpcsource clean -xdff
# Setup config
FPC_VER=$(ls fpc/lib/fpc | grep '[0-9]')
fpcmkcfg -d basepath=$(realpath fpc/lib/fpc/$FPC_VER/) -p -o fpc/lib/fpc/etc/fpc.cfg
ln -s $(realpath fpc/lib/fpc/$FPC_VER/ppcx64) fpc/bin/ppcx64
# Shallow clone of Lazarus
git clone --depth=1 https://gitlab.com/freepascal.org/lazarus/lazarus.git
# Build Lazarus
make -C lazarus all
# Setup config for standalone installation
echo "--pcp=$(realpath ./lazarus/config)" > lazarus/lazarus.cfg
