# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

pkgbase=perl
pkgname=('perl' 'perl-devel')
pkgver=5.32.1
pkgrel=1
pkgdesc="A highly capable, feature-rich programming language"
arch=(i686 x86_64)
license=('GPL')
url="https://www.perl.org/"
makedepends=('libdb-devel' 'libgdbm-devel' 'libcrypt-devel')
source=(https://www.cpan.org/src/5.0/perl-${pkgver}.tar.xz
        perlbin.sh
        perlbin.csh
        perlbin.fish
        perl-binary-module-dependency-1.template
        perl.cygwin-auto-image-base.patch
        perl.cygwin-hints.patch
        perl.cygwin-Configure-libpth.patch
        perl.cygwin-Configure-libsearch.patch
        perl.cygwin-Win32.patch
        perl-5.32.0-msys2.patch
        perl-cygwin-dont-add-fortify-source.patch
        regen-skip.diff)
options=('makeflags' '!purge' 'emptydirs')
sha256sums=('57cc47c735c8300a8ce2fa0643507b44c4ae59012bfdad0121313db639e02309'
            '91bf8a45a3f04f51fe0eca146a71e278d41dbde19ba60dabc1e41c4639477c07'
            '478a52dc440d5ca324f4c95238b39cc8c90d5d7d8d5601fa085ffef79fea04f4'
            '11ce66bcf4803b0bf27179a32ce53f41bcdfa967c4c0d54ac103dbc4951d18a3'
            'fe91f1de38af525cb82503feef553b71299c52ea52c01b1959143f48f4051ffa'
            '9745883a27edc99ac0a90cbbf930c2a532085719dc1eaf801b2dda29f7d475e9'
            'ecc088b7c69676179bf149e53d558e49645e03aff33f041ce35ec353865bedcc'
            '2520356a2ffca851207a38adeb8fd0af4d87bcaa20c6ebf5185295ba588fac58'
            '48083033bce0d57d3b41d6febe3db824d00751b6f86f65380abc773837c23ede'
            '2715c8dd69d6e72d04a269844ccab0a4cae77263448b2af9e2e3289929a0982f'
            'c07a4bd0d01d5eb63cda8fca0333a58fac2a230438b0ac4ba41f53861fd5d998'
            '0140bdd899b424732e3cd7c18ae1b7639535a3b28fb13589c5cf3c78eac988ef'
            'f2f28c33743f767e98a5c6b2666a838ed12590d913b02b2baf3258d6c175dc06')

prepare() {
  chmod -R 0666 ${srcdir}/${pkgname}-${pkgver}
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p2 -i ${srcdir}/perl.cygwin-auto-image-base.patch
  patch -p1 -i ${srcdir}/perl.cygwin-hints.patch
  patch -p2 -i ${srcdir}/perl.cygwin-Configure-libsearch.patch
  patch -p2 -i ${srcdir}/perl.cygwin-Configure-libpth.patch
  patch -p1 -i ${srcdir}/perl.cygwin-Win32.patch

  patch -p1 -i ${srcdir}/perl-5.32.0-msys2.patch
  patch -p1 -i ${srcdir}/perl-cygwin-dont-add-fortify-source.patch

  #debian
  patch -p1 -i ${srcdir}/regen-skip.diff

  cat > Policy.sh <<_EOF
#!/bin/sh
d_readlink='undef'
_EOF

sed -ri '/\s+$/s///' $(find . -type f | grep -vE ".aux$|.ico$|.tgz$|.tbz$|.tar$|.utf$|.enc$|.patch$|.pub$|.xml$|.t$|laozi38p.pod$|opnames.h$|perlguts.pod$|trailing-space$")

  # test broken with gdbm 1.15. See: https://rt.perl.org/Public/Bug/Display.html?id=133295
  sed -i 's|BEGIN {|BEGIN { plan(skip_all => "fatal test unsupported with gdbm 1.15");|' ext/GDBM_File/t/fatal.t

}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  ./Configure -des -Dusethreads \
    -Doptimize="${CFLAGS}" \
    -Dprefix=/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 \
    -Dinc_version_list=none \
    -Dman1ext=1perl -Dman3ext=3perl \
    -Darchname=${ARCH}-msys-threads \
    -Dmyarchname=${ARCH}-msys \
    -Dlibperl=msys-perl5_32.dll \
    -Dcc=gcc -Dld=g++ \
    -Accflags="$CFLAGS -fwrapv"

  LC_ALL=C make
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  TEST_JOBS=$(echo $MAKEFLAGS | sed 's/.*-j\([0-9][0-9]*\).*/\1/') make test_harness || true
  #make test
}

package_perl() {
  groups=('base-devel')
  depends=('db' 'gdbm' 'libcrypt' 'coreutils' 'sh')
# NOTE: This array is automatically generated by `./patchprov`.
#       If you want to add entries, do so in the next array.
# NOTE2: './patchprov' expects this provides= to be hard left
provides=('perl-Archive-Tar=2.36'
          'perl-Attribute-Handlers=1.01'
          'perl-autodie=2.29'
          'perl-AutoLoader=5.74'
          'perl-autouse=1.11'
          'perl-base=2.27'
          'perl-bignum=0.51'
          'perl-Carp=1.50'
          'perl-Compress-Raw-Bzip2=2.093'
          'perl-Compress-Raw-Zlib=2.093'
          'perl-Config-Perl-V=0.32'
          'perl-constant=1.33'
          'perl-CPAN=2.27'
          'perl-CPAN-Meta=2.150010'
          'perl-CPAN-Meta-Requirements=2.140'
          'perl-CPAN-Meta-YAML=0.018'
          'perl-Data-Dumper=2.174'
          'perl-DB_File=1.853'
          'perl-Devel-PPPort=3.57'
          'perl-Devel-SelfStubber=1.06'
          'perl-Digest=1.17_01'
          'perl-Digest-MD5=2.55_01'
          'perl-Digest-SHA=6.02'
          'perl-Dumpvalue=1.21'
          'perl-Encode=3.06'
          'perl-encoding-warnings=0.13'
          'perl-Env=1.04'
          'perl-experimental=0.020'
          'perl-Exporter=5.74'
          'perl-ExtUtils-CBuilder=0.280234'
          'perl-ExtUtils-Constant=0.25'
          'perl-ExtUtils-Install=2.14'
          'perl-ExtUtils-MakeMaker=7.44'
          'perl-ExtUtils-Manifest=1.72'
          'perl-ExtUtils-ParseXS=3.40'
          'perl-File-Fetch=0.56'
          'perl-File-Path=2.16'
          'perl-File-Temp=0.2309'
          'perl-Filter-Simple=0.96'
          'perl-Filter-Util-Call=1.59'
          'perl-Findbin=1.51'
          'perl-Getopt-Long=2.51'
          'perl-HTTP-Tiny=0.076'
          'perl-I18N-Collate=1.02'
          'perl-I18N-LangTags=0.44'
          'perl-if=0.0608'
          'perl-IO-Compress=2.093'
          'perl-IO-Socket-IP=0.39'
          'perl-IO-Zlib=1.10'
          'perl-IO=1.43'
          'perl-IPC-Cmd=1.04'
          'perl-IPC-SysV=2.07'
          'perl-JSON-PP=4.02'
          'perl-lib=0.65'
          'perl-libnet=3.11'
          'perl-Locale-Maketext-Simple=0.21_01'
          'perl-Locale-Maketext=1.29'
          'perl-Math-BigInt=1.999818'
          'perl-Math-BigInt-FastCalc=0.5009'
          'perl-Math-BigRat=0.2614'
          'perl-Math-Complex=1.59_01'
          'perl-Memoize=1.03_01'
          'perl-MIME-Base64=3.15'
          'perl-Module-CoreList=5.20200620'
          'perl-Module-Load=0.34'
          'perl-Module-Load-Conditional=0.70'
          'perl-Module-Loaded=0.08'
          'perl-Module-Metadata=1.000037'
          'perl-NEXT=0.67_01'
          'perl-Net-Ping=2.72'
          'perl-Params-Check=0.38'
          'perl-parent=0.238'
          'perl-Parse-CPAN-Meta=2.150010'
          'perl-PathTools=3.78'
          'perl-Perl-OSType=1.010'
          'perl-perlfaq=5.20200523'
          'perl-PerlIO-via-QuotedPrint=0.08'
          'perl-Pod-Checker=1.73'
          'perl-Pod-Escapes=1.07'
          'perl-Pod-Perldoc=3.2801'
          'perl-Pod-Simple=3.40'
          'perl-Pod-Usage=1.69'
          'perl-podlators=5.008'
          'perl-Safe=2.41'
          'perl-Scalar-List-Utils=1.55'
          'perl-Search-Dict=1.07'
          'perl-SelfLoader=1.26'
          'perl-Socket=2.029'
          'perl-Storable=3.21'
          'perl-Sys-Syslog=0.36'
          'perl-Term-ANSIColor=5.01'
          'perl-Term-Cap=1.17'
          'perl-Term-Complete=1.403'
          'perl-Term-ReadLine=1.17'
          'perl-Test-Harness=3.42'
          'perl-Test-Simple=1.302175'
          'perl-Test=1.31'
          'perl-Text-Abbrev=1.02'
          'perl-Text-Balanced=2.03'
          'perl-Text-ParseWords=3.30'
          'perl-Text-Tabs=2013.0523'
          'perl-Thread-Queue=3.14'
          'perl-Thread-Semaphore=2.13'
          'perl-threads-shared=1.61'
          'perl-threads=2.25'
          'perl-Tie-File=1.06'
          'perl-Tie-RefHash=1.39'
          'perl-Time-HiRes=1.9764'
          'perl-Time-Local=1.28'
          'perl-Time-Piece=1.3401'
          'perl-Unicode-Collate=1.27'
          'perl-Unicode-Normalize=1.27'
          'perl-version=0.9924'
          'perl-XSLoader=0.30')
  # Add your own provides here
  provides=(${provides[@]})
  replaces=('perl-Scalar-List-Utils')

  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR="${pkgdir}" V=1 install

  for template in "${srcdir}/"*.template; do
    install -Dm644 "${template}" "${pkgdir}/usr/share/makepkg-template/${template##*/}"
  done
  ln -s perl-binary-module-dependency-1.template "${pkgdir}/usr/share/makepkg-template/"perl-binary-module-dependency.template

  ### Perl Settings ###
  # Change man page extensions for site and vendor module builds.
  # Set no mail address since bug reports should go to the bug tracker
  # and not someone's email.
  sed -e '/^man1ext=/ s/1perl/1p/' -e '/^man3ext=/ s/3perl/3pm/' \
      -e "/^cf_email=/ s/'.*'/''/" \
      -e "/^perladmin=/ s/'.*'/''/" \
      -i ${pkgdir}/usr/lib/perl5/core_perl/Config_heavy.pl

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

  # Profile script to set paths to perl scripts.
  install -D -m755 ${srcdir}/perlbin.sh \
                   ${pkgdir}/etc/profile.d/perlbin.sh
  # Profile script to set paths to perl scripts on csh. (FS#22441)
  install -D -m755 ${srcdir}/perlbin.csh \
                  ${pkgdir}/etc/profile.d/perlbin.csh
  # Profile script to set paths to perl scripts on fish. (FS#51191)
  install -D -m755 ${srcdir}/perlbin.fish \
                  ${pkgdir}/usr/share/fish/vendor_conf.d/perlbin.fish

  # Add the dirs so new installs will already have them in PATH once they
  # install their first perl programm
  install -d -m755 "${pkgdir}/usr/bin/vendor_perl"
  install -d -m755 "${pkgdir}/usr/bin/site_perl"

  #(cd ${pkgdir}/usr/bin; mv perl${pkgver} perl)

  # Remove all pod files *except* those under /usr/lib/perl5/core_perl/pods/
  # (FS#16488)
  rm -f ${pkgdir}/usr/share/perl5/core_perl/*.pod
  for d in ${pkgdir}/usr/share/perl5/core_perl/*; do
    if [ -d ${d} -a $(basename ${d}) != "pods" ]; then
      find ${d} -name *.pod -delete
    fi
  done
  find ${pkgdir}/usr/lib -name *.pod -delete
  find ${pkgdir} -name .packlist -delete

  find ${pkgdir}/usr/lib -type f -exec chmod 0644 {} \;
  find ${pkgdir}/usr/share/perl5 -type f -exec chmod 0644 {} \;
  # prevent xsinit-generated code from referencing boot_Win32CORE
  sed -i -e "s/^\(static_ext\)=.*/\1=' '/" \
    ${pkgdir}/usr/lib/perl5/core_perl/Config_heavy.pl

  install -D -m755 ${srcdir}/${pkgname}-${pkgver}/lib/auto/XS/APItest/APItest.dll \
                  ${pkgdir}/usr/lib/perl5/core_perl/auto/XS/APItest/APItest.dll
  install -D -m755 ${srcdir}/${pkgname}-${pkgver}/lib/auto/XS/Typemap/Typemap.dll \
                  ${pkgdir}/usr/lib/perl5/core_perl/auto/XS/Typemap/Typemap.dll
  install -D -m755 ${srcdir}/${pkgname}-${pkgver}/lib/auto/Win32CORE/Win32CORE.a \
                  ${pkgdir}/usr/lib/perl5/core_perl/auto/Win32CORE/Win32CORE.a

}

package_perl-devel() {
  pkgdesc="Perl headers and dev dependencies"
  depends=("perl=${pkgver}" 'libcrypt-devel')
}
