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

pkgbase=perl
pkgname=('perl' 'perl-doc' 'perl-devel')
pkgver=5.40.3
pkgrel=1
pkgdesc="A highly capable, feature-rich programming language"
arch=(i686 x86_64)
license=('GPL')
url="https://www.perl.org/"
msys2_references=(
  "cpe: cpe:/a:larry_wall:perl"
  "cpe: cpe:/a:perl:perl"
)
makedepends=('libdb-devel' 'libgdbm-devel' 'libxcrypt-devel' 'gcc' 'make' 'groff')
source=(https://www.cpan.org/src/5.0/perl-${pkgver}.tar.xz
        perlbin.sh
        perlbin.csh
        perlbin.fish
        perl-binary-module-dependency-1.template
        0002-perl.cygwin-hints.patch
        0003-perl.cygwin-Configure-libsearch.patch
        0004-perl.cygwin-Configure-libpth.patch
        0005-perl.cygwin-Win32.patch
        0006-perl-5.36.0-msys2.patch
        0007-Skip-a-regeneration-check-in-unrelated-git-repositor.patch)
options=('makeflags' '!purge' 'emptydirs')
sha256sums=('65f63b4763ab6cb9bb3d5731dd10369e1705be3c59be9847d453eb60b349ab43'
            '91bf8a45a3f04f51fe0eca146a71e278d41dbde19ba60dabc1e41c4639477c07'
            '478a52dc440d5ca324f4c95238b39cc8c90d5d7d8d5601fa085ffef79fea04f4'
            '11ce66bcf4803b0bf27179a32ce53f41bcdfa967c4c0d54ac103dbc4951d18a3'
            'fe91f1de38af525cb82503feef553b71299c52ea52c01b1959143f48f4051ffa'
            'e45df328a7128604a1a7b901e44da67f7ebb34162ca23c980b4e36553c0e7449'
            'a074ce00dabd7876293cf0fa8c8be26029992f51873e33cd2213b6acf60cafbb'
            '1bf02b601f670cc1c8f65e63ec7a10578f92c8ecd8a5752fe331479bd6115311'
            'e05e20b29c950555edfa246d2b9177188a4b265d1887c262f5b272f382696474'
            '9c661e438c935a047770e8f91c810a2f0e074e2f72bf89da27e1b6b979b247a4'
            '4c44801139321e77a3ade921ce93b114100346ad5b17d03a67b2a92b85ee8a27')

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

  patch -p1 -i ${srcdir}/0002-perl.cygwin-hints.patch
  patch -p1 -i ${srcdir}/0003-perl.cygwin-Configure-libsearch.patch
  patch -p1 -i ${srcdir}/0004-perl.cygwin-Configure-libpth.patch
  patch -p1 -i ${srcdir}/0005-perl.cygwin-Win32.patch

  patch -p1 -i ${srcdir}/0006-perl-5.36.0-msys2.patch

  #debian
  patch -p1 -i ${srcdir}/0007-Skip-a-regeneration-check-in-unrelated-git-repositor.patch

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

  # 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}

  CFLAGS+=" -Wno-int-conversion"

  export MSYSTEM=CYGWIN
  ./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}-cygwin-threads \
    -Dmyarchname=${ARCH}-cygwin \
    -Dlibperl=msys-perl"$(echo "${pkgver%.[0-9]*}" | tr . _)".dll \
    -Dcc=gcc -Dld=g++ \
    -Accflags="$CFLAGS -fwrapv" \
    -Ud_setproctitle

  LC_ALL=C make

  _package "${srcdir}/_install"
}

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

_package() {
  local pkgdir="${1}"

  make DESTDIR="${pkgdir}" 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
}

_find_doc_files() {
  pushd "${1}" > /dev/null
    echo usr/bin/core_perl/perldoc
    echo usr/share/man/man1/perldoc.1perl
    echo usr/share/perl5/core_perl/pods/

    # everything else than section 1 goes as-is
    find usr/share/man/ -maxdepth 1 -mindepth 1 -not -name man1

    # section 1 manual pages for installed programs go in perl not perl-doc
    find usr/share/man/man1 -type f -printf "%f\n" | \
    while read man; do
      prog=$(echo $man | sed 's/\.[13]perl$//')
      if [ ! -f usr/bin/core_perl/$prog ] && [ ! -f usr/bin/$prog ]; then
          echo usr/share/man/man1/$man
      fi
    done
  popd > /dev/null
}

package_perl() {
  depends=('db' 'gdbm' 'libxcrypt' '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=3.02_001'
          'perl-Attribute-Handlers=1.03'
          'perl-AutoLoader=5.74'
          'perl-CPAN-Meta-Requirements=2.143'
          'perl-CPAN-Meta-YAML=0.018'
          'perl-CPAN-Meta=2.150010'
          'perl-CPAN=2.36'
          'perl-Carp=1.54'
          'perl-Compress-Raw-Bzip2=2.212'
          'perl-Compress-Raw-Zlib=2.212'
          'perl-Config-Perl-V=0.36'
          'perl-DB_File=1.859'
          'perl-Data-Dumper=2.189'
          'perl-Devel-PPPort=3.72'
          'perl-Devel-SelfStubber=1.06'
          'perl-Digest-MD5=2.58_01'
          'perl-Digest-SHA=6.04'
          'perl-Digest=1.20'
          'perl-Dumpvalue=1.21'
          'perl-Encode=3.21'
          'perl-Env=1.06'
          'perl-Exporter=5.78'
          'perl-ExtUtils-CBuilder=0.280240'
          'perl-ExtUtils-Constant=0.25'
          'perl-ExtUtils-Install=2.22'
          'perl-ExtUtils-MakeMaker=7.70'
          'perl-ExtUtils-Manifest=1.75'
          'perl-ExtUtils-PL2Bat=0.005'
          'perl-ExtUtils-ParseXS=3.51'
          'perl-File-Fetch=1.04'
          'perl-File-Path=2.18'
          'perl-File-Temp=0.2311'
          'perl-Filter-Simple=0.96'
          'perl-Filter-Util-Call=1.64'
          'perl-FindBin=1.54'
          'perl-Getopt-Long=2.57'
          'perl-HTTP-Tiny=0.088'
          'perl-I18N-Collate=1.02'
          'perl-I18N-LangTags=0.45'
          'perl-IO-Compress=2.212'
          'perl-IO-Socket-IP=0.42'
          'perl-IO-Zlib=1.15'
          'perl-IO=1.55'
          'perl-IPC-Cmd=1.04'
          'perl-IPC-SysV=2.09'
          'perl-JSON-PP=4.16'
          'perl-Locale-Maketext-Simple=0.21_01'
          'perl-Locale-Maketext=1.33'
          'perl-MIME-Base64=3.16_01'
          'perl-Math-BigInt-FastCalc=0.5018'
          'perl-Math-BigInt=2.003002'
          'perl-Math-Complex=1.62'
          'perl-Memoize=1.16'
          'perl-Module-CoreList=5.20250803_40'
          'perl-Module-Load-Conditional=0.74'
          'perl-Module-Load=0.36'
          'perl-Module-Loaded=0.08'
          'perl-Module-Metadata=1.000038'
          'perl-NEXT=0.69'
          'perl-Net-Ping=2.76'
          'perl-Params-Check=0.38'
          'perl-PathTools=3.91'
          'perl-Perl-OSType=1.010'
          'perl-PerlIO-via-QuotedPrint=0.10'
          'perl-Pod-Checker=1.77'
          'perl-Pod-Escapes=1.07'
          'perl-Pod-Perldoc=3.2801'
          'perl-Pod-Simple=3.45'
          'perl-Pod-Usage=2.03'
          'perl-Safe=2.46'
          'perl-Scalar-List-Utils=1.63'
          'perl-Search-Dict=1.07'
          'perl-SelfLoader=1.27'
          'perl-Socket=2.038'
          'perl-Storable=3.32'
          'perl-Sys-Syslog=0.36'
          'perl-Term-ANSIColor=5.01'
          'perl-Term-Cap=1.18'
          'perl-Term-Complete=1.403'
          'perl-Term-ReadLine=1.17'
          'perl-Term-Table=0.018'
          'perl-Test-Harness=3.48'
          'perl-Test-Simple=1.302199'
          'perl-Test2-Suite=0.000162'
          'perl-Test=1.31'
          'perl-Text-Abbrev=1.02'
          'perl-Text-Balanced=2.06'
          'perl-Text-ParseWords=3.31'
          'perl-Text-Tabs=2024.001'
          'perl-Thread-Queue=3.14'
          'perl-Thread-Semaphore=2.13'
          'perl-Tie-File=1.09'
          'perl-Tie-RefHash=1.40'
          'perl-Time-HiRes=1.9777'
          'perl-Time-Local=1.35'
          'perl-Time-Piece=1.3401_01'
          'perl-Unicode-Collate=1.31'
          'perl-Unicode-Normalize=1.32'
          'perl-Win32=0.59'
          'perl-Win32API-File=0.1203_01'
          'perl-XSLoader=0.32'
          'perl-autodie=2.37'
          'perl-autouse=1.11'
          'perl-base=2.27'
          'perl-bignum=0.67'
          'perl-constant=1.33'
          'perl-encoding-warnings=0.14'
          'perl-experimental=0.032'
          'perl-if=0.0610'
          'perl-lib=0.65'
          'perl-libnet=3.15'
          'perl-parent=0.241'
          'perl-perlfaq=5.20240218'
          'perl-podlators=5.010'
          'perl-threads-shared=1.69'
          'perl-threads=2.40'
          'perl-version=0.9930')
  # Add your own provides here
  provides=(${provides[@]})
  replaces=('perl-Scalar-List-Utils')

  cp -r "${srcdir}/_install/"* "${pkgdir}"

  for file in $(_find_doc_files ${srcdir}/_install); do
    rm -Rf "${pkgdir}/${file}"
  done
}

package_perl-doc() {
  pkgdesc="Perl documentation"
  depends=("perl=${pkgver}")

  for file in $(_find_doc_files ${srcdir}/_install); do
    mkdir -p "${pkgdir}/$(dirname "${file}")"
    cp -r "${srcdir}/_install/${file}" "${pkgdir}/${file}"
  done
}

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