# Maintainer: Florian Pritz <bluewind@xinu.at>
# Contributor: Angel Velasquez <angvp@archlinux.org>
# Contributor: kevin <kevin.archlinux.org>
# Contributor: judd <jvinet.zeroflux.org>
# Contributor: francois <francois.archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - configure ARMv5 with -Dd_u32align
#    https://rt.perl.org/Public/Bug/Display.html?id=133803

pkgname=perl
pkgver=5.30.1
_baseversion="${pkgver%.*}"
pkgrel=1
pkgdesc="A highly capable, feature-rich programming language"
arch=(x86_64)
license=('GPL' 'PerlArtistic')
url="https://www.perl.org"
depends=('gdbm>=1.17' 'db' 'glibc')
# NOTE: This array is automatically generated by `./patchprov`.
#       If you want to add entries, do so in the next array.
provides=('perl-archive-tar=2.32'
          '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.084'
          'perl-compress-raw-zlib=2.084'
          'perl-config-perl-v=0.32'
          'perl-constant=1.33'
          'perl-cpan-meta-requirements=2.140'
          'perl-cpan-meta-yaml=0.018'
          'perl-cpan-meta=2.150010'
          'perl-cpan=2.22'
          'perl-data-dumper=2.174'
          'perl-db_file=1.843'
          'perl-devel-ppport=3.52'
          'perl-devel-selfstubber=1.06'
          'perl-digest-md5=2.55'
          'perl-digest-sha=6.02'
          'perl-digest=1.17_01'
          'perl-dumpvalue=1.18'
          'perl-encode=3.01'
          'perl-encoding-warnings=0.13'
          'perl-env=1.04'
          'perl-experimental=0.020'
          'perl-exporter=5.73'
          'perl-extutils-cbuilder=0.280231'
          'perl-extutils-constant=0.25'
          'perl-extutils-install=2.14'
          'perl-extutils-makemaker=7.34'
          '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.95'
          'perl-filter-util-call=1.59'
          'perl-getopt-long=2.5'
          'perl-http-tiny=0.076'
          'perl-i18n-collate=1.02'
          'perl-i18n-langtags=0.43'
          'perl-if=0.0608'
          'perl-io-compress=2.084'
          'perl-io-socket-ip=0.39'
          'perl-io-zlib=1.10'
          'perl-io=1.40'
          'perl-ipc-cmd=1.02'
          '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-fastcalc=0.5008'
          'perl-math-bigint=1.999816'
          'perl-math-bigrat=0.2614'
          'perl-math-complex=1.5901'
          'perl-memoize=1.03_01'
          'perl-mime-base64=3.15'
          'perl-module-corelist=5.20191110'
          'perl-module-load-conditional=0.68'
          'perl-module-load=0.34'
          'perl-module-loaded=0.08'
          'perl-module-metadata=1.000036'
          'perl-net-ping=2.71'
          'perl-params-check=0.38'
          'perl-parent=0.237'
          'perl-pathtools=3.78'
          'perl-perl-ostype=1.010'
          'perl-perlfaq=5.20190126'
          'perl-perlio-via-quotedprint=0.08'
          'perl-pod-checker=1.73'
          'perl-pod-escapes=1.07'
          'perl-pod-parser=1.63'
          'perl-pod-perldoc=3.2801'
          'perl-pod-simple=3.35'
          'perl-pod-usage=1.69'
          'perl-podlators=5.006'
          'perl-safe=2.40'
          'perl-scalar-list-utils=1.50'
          'perl-search-dict=1.07'
          'perl-selfloader=1.25'
          'perl-socket=2.027'
          'perl-storable=3.15'
          'perl-sys-syslog=0.35'
          'perl-term-ansicolor=4.06'
          'perl-term-cap=1.17'
          'perl-term-complete=1.403'
          'perl-term-readline=1.17'
          'perl-test-harness=3.42'
          'perl-test-simple=1.302162'
          '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.13'
          'perl-thread-semaphore=2.13'
          'perl-threads-shared=1.60'
          'perl-threads=2.22'
          'perl-tie-file=1.02'
          'perl-tie-refhash=1.39'
          'perl-time-hires=1.9760'
          'perl-time-local=1.28'
          'perl-time-piece=1.33'
          'perl-unicode-collate=1.27'
          'perl-unicode-normalize=1.26'
          'perl-version=0.9924'
          'perl-xsloader=0.30')
# Add your own provides here
provides=(${provides[@]})
source=(https://www.cpan.org/src/5.0/perl-${pkgver}.tar.xz
        perlbin.sh
        perlbin.csh
        perlbin.fish
        detect-old-perl-modules.sh
        detect-old-perl-modules.hook)
options=('makeflags' '!purge' 'emptydirs')
sha512sums=('8f3339efdcd1bb58fa58a90042181bef86bb09e4598c737e446ed43b56d2ab23d67eced5e36fb08fc61e076acfdb572a12e46a1277f5299a3f412054df0b88bf'
            '46724344828e7f86e016f9c8d588bf52b2e764e65e0acc1a38899a530c99bc6e4fd8b46fa0d4bbd685aa2074dd5bcbf9029ac3bb3f2d0ee9adfc4f6c0745f373'
            'fc1344a02c741d61af6f6b5967f29cc6f43c2059761522b150261924dd7e1989da5254c03ffa0627accd9af01bc152edd24e84a6b59579acb9ee1900b6ce9383'
            '881e2efe05ba818cd7300f126800b56bb0685cb5c9c5fb7e67ef6aaf5abd17d2391a979d5d16d109c5111f4b35504ba83d19b0e6eda4431e8421fcbea19d2f1a'
            'bd48af7a6209f2ad51aa1747a7238ecb11607a53f61460d873202bf14b55c3b7dd6f66f4a9f2cac8a24240313789a9a44dbc81b73587de46a6b1866bdfca5e26'
            '6b5b2ba606d443da22c6c1a754829abd36f3fdfef1089bcf06c8f9db0217f2c2f02ebc14958ffa7afe618c9a80bd1025e76704f67466c0c3db7d40ef2c0e56b3')
# https://www.cpan.org/src/5.0/perl-$pkgver.tar.xz.sha256.txt

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

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

  if [ "${CARCH}" = "x86_64" ]; then
    # for x86_64
    arch_opts="-Dcccdlflags='-fPIC'"
  else
    # for i686
    arch_opts=""
  fi

  [[ $CARCH == "arm" ]] && CONFIG="-Dd_u32align"
  ./Configure -des -Dusethreads -Duseshrplib -Doptimize="${CFLAGS}" \
    -Dprefix=/usr -Dvendorprefix=/usr \
    -Dprivlib=/usr/share/perl5/core_perl \
    -Darchlib=/usr/lib/perl5/$_baseversion/core_perl \
    -Dsitelib=/usr/share/perl5/site_perl \
    -Dsitearch=/usr/lib/perl5/$_baseversion/site_perl \
    -Dvendorlib=/usr/share/perl5/vendor_perl \
    -Dvendorarch=/usr/lib/perl5/$_baseversion/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 ${arch_opts} \
    -Dlddlflags="-shared ${LDFLAGS}" -Dldflags="${LDFLAGS}" $CONFIG
  make
}

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

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR="$pkgdir" install

  ### 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/$_baseversion/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 -m644 ${srcdir}/perlbin.sh \
                   ${pkgdir}/etc/profile.d/perlbin.sh
  # Profile script to set paths to perl scripts on csh. (FS#22441)
  install -D -m644 ${srcdir}/perlbin.csh \
                  ${pkgdir}/etc/profile.d/perlbin.csh
  # Profile script to set paths to perl scripts on fish. (FS#51191)
  install -D -m 755 "$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)
  rm "$pkgdir/usr/bin/perl$pkgver"

  install -D -m755 -t "$pkgdir/usr/share/libalpm/scripts" "$srcdir/detect-old-perl-modules.sh"
  install -D -m644 -t "$pkgdir/usr/share/libalpm/hooks" "$srcdir/detect-old-perl-modules.hook"

  find $pkgdir -name perllocal.pod -delete
  find $pkgdir -name .packlist -delete
}