abinfo "Configuring Perl ..."
"$SRCDIR"/Configure \
    -des \
    -Dusethreads \
    -Duseshrplib \
    -Dprefix=/usr \
    -Dvendorprefix=/usr \
    -Dvendorprefix=/usr \
    -Dprivlib=/usr/share/perl5/core_perl \
    -Darchlib=/usr/lib/perl5/core_perl \
    -Dsitelib=/usr/share/perl5/site_perl \
    -Dsitearch=/usr/lib/perl5/site_perl \
    -Dvendorlib=/usr/share/perl5/vendor_perl \
    -Dvendorarch=/usr/lib/perl5/vendor_perl \
    -Dscriptdir=/usr/bin/core_perl \
    -Dsitescript=/usr/bin/site_perl \
    -Dvendorscript=/usr/bin/vendor_perl \
    -Dman1dir=/usr/share/man/man1 \
    -Dman3dir=/usr/share/man/man3 \
    -Dinc_version_list=none \
    -Dcccdlflags="${CFLAGS}" \
    -Dlddlflags="-shared ${LDFLAGS}" \
    -Dldflags="${LDFLAGS}" \
    -Doptimize="${CFLAGS}"

# FIXME: This issue occurs consistently when building with Qemu.
abinfo "HACK: Runnning generation scripts manually to ensure Makefile is generated ..."
bash "$SRCDIR"/makedepend.SH
bash "$SRCDIR"/makedepend_file.SH
bash "$SRCDIR"/Makefile.SH

abinfo "Building Perl ..."
make

abinfo "Installing Perl ..."
make install \
    DESTDIR="$PKGDIR"

# From Arch Linux:

### CPAN Settings ###
# Set CPAN default config to use the site directories.
abinfo "Setting default CPAN directories ..."
sed -e '/(makepl_arg =>/   s/""/"INSTALLDIRS=site"/' \
    -e '/(mbuildpl_arg =>/ s/""/"installdirs=site"/' \
    -i "$PKGDIR"/usr/share/perl5/core_perl/CPAN/FirstTime.pm

# Add the dirs so new installs will already have them in PATH once they
# install their first perl programm
abinfo "Creating directories of vendor and site executables ..."
install -dvm755 "$PKGDIR"/usr/bin/{vendor,site}_perl

abinfo "Creating symlinks for core executables ..."
cd "$PKGDIR"/usr/bin/
ln -sv core_perl/* \
    "$PKGDIR"/usr/bin/

abinfo "Purging metadata files ..."
find "$PKGDIR" -name perllocal.pod -delete -print
find "$PKGDIR" -name .packlist -delete -print

abinfo "Converting C header files to Perl header files for mmdebstrap ..."
# FIXME: some dependent header files are missing, ignore the warnings for now
# e.g. Can't open gnu/stubs-x32.h: No such file or directory
# This does not affect the dependent program from running normally.
"$PKGDIR"/usr/bin/core_perl/h2ph \
    -a -d "$PKGDIR"/usr/lib/perl5/core_perl/ syscall.h || true
"$PKGDIR"/usr/bin/core_perl/h2ph \
    -a -d "$PKGDIR"/usr/lib/perl5/core_perl/ sys/ioctl.h || true
