##
##  perl.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         perl
Summary:      Practical Extraction and Reporting Language
URL:          http://www.perl.com/
Vendor:       The Perl Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        CORE
Group:        Perl
License:      GPL/Artistic
Version:      5.22.4
Release:      20170902

#   list of sources
Source0:      http://www.cpan.org/src/perl-%{version}.tar.gz
Patch0:       perl.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Perl ("Practical Reporting and Extraction Language") is a very
    sophisticated and flexible (but this way also complex) programming
    language. This is the original implementation of the Perl 5
    interpreter from Larry Wall and his community.

%track
    prog perl = {
        version   = %{version}
        url       = http://www.cpan.org/src/
        regex     = perl-(5\.(?:[02468]|[12][02468])\.\d+)\.tar\.gz
    }

%prep
    %setup -q
    chmod -R u+w .
    %patch -p0

%build
    #   configure the Perl package
    %{l_shtool} subst \
        -e 's;\(.*for thislib in $libswanted.*\);libswanted=`echo " $libswanted " | sed -e "s/ bind / /g" -e "s/ db / /g" -e "s/ gdbm / /g" -e "s/ iconv / /g"`\; \1;' \
        -e 's;package=perl5;package=perl;' \
        Configure
    %{l_shtool} subst \
        -e 's; */usr/local/lib;;' \
        hints/freebsd.sh hints/netbsd.sh
    optimize=""
    case "%{l_platform -t}" in
        amd64-* ) optimize="%{l_cflags}"    ;;
        *-aix*  ) optimize="%{l_cflags}"    ;;
        *-*     ) optimize="%{l_cflags -O}" ;;
    esac
    if [ ".$optimize" = . ]; then
        optimize="-Uoptimize"
    else
        optimize="-Doptimize=$optimize"
    fi
    libdirs=""
    for dir in %{l_prefix}/lib /lib64 /usr/lib64 /lib /usr/lib /usr/ccs/lib /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu; do
        if [ -d $dir ]; then
            if [ ".$libdirs" = . ]; then
                libdirs="$dir"
            else
                libdirs="$libdirs $dir"
            fi
        fi
    done
    ./Configure \
        -d -e -s \
        -Dcf_by="%{l_openpkg_release}" \
        -Dcf_email="http://www.openpkg.org/" \
        -Dprefix=%{l_prefix} \
        -Dvendorprefix=%{l_prefix} \
        -Dinstallprefix=%{l_prefix} \
        -Dinstallstyle="lib/perl5" \
        -Dman1dir=%{l_prefix}/man/man1 \
        -Dman3dir=%{l_prefix}/man/man3 \
        -Dcc="%{l_prefix}/bin/gcc" "$optimize" \
        -Dlocincpth="%{l_prefix}/include" \
        -Dloclibpth="%{l_prefix}/lib" \
        -Dldflags="%{l_ldflags}" \
        -Dlibpth="$libdirs" \
        -Dglibpth="$libdirs" \
        -Dscriptdir="%{l_prefix}/bin" \
        -Uinstallusrbinperl \
        -Ui_malloc -Ui_iconv -Ui_db \
        -Uusedevel

    #   build the Perl package
    %{l_make} %{l_mflags} -f Makefile

%install

    #   install the Perl package via standard procedure
    %{l_make} %{l_mflags} -f Makefile install DESTDIR=$RPM_BUILD_ROOT

    #   allow us to already use the temporary install perl(1)
    PERL5LIB=$RPM_BUILD_ROOT%{l_prefix}/lib/perl
    export PERL5LIB

    #   install Perl versions of system header files
    ( case "%{l_platform -t}" in
          *-darwin* ) export DYLD_LIBRARY_PATH="`pwd`" ;;
      esac
      cd /usr/include
      eval `$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installarchlib`
      echo *.h sys/*.h |\
      xargs $RPM_BUILD_ROOT%{l_prefix}/bin/perl \
          $RPM_BUILD_ROOT%{l_prefix}/bin/h2ph -h -d $RPM_BUILD_ROOT$installarchlib
    ) || exit $?

    #   post-adjustments to installation tree
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/perl%{version}
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/man/man3
    for name in \
        libnetcfg perlaix perlamiga perlapollo \
        perlbeos perlbs2000 perlcygwin perldgux perlepoc perlfreebsd perlhpux \
        perlhurd perlirix perlmachten perlmacos perlmacosx perlmint perlmpeix \
        perlnetware perlos2 perlos390 perlos400 perlplan9 perlqnx perlsolaris \
        perlvmesa perlvms perlvos perlwin32; do
        rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/$name.1
    done

    #   re-adjust configuration as mentioned in Perl's INSTALL document
    ( case "%{l_platform -t}" in
          *-darwin* ) export DYLD_LIBRARY_PATH="`pwd`" ;;
      esac
      $RPM_BUILD_ROOT%{l_prefix}/bin/perl -pi.orig \
          -e "s:$RPM_BUILD_ROOT%{l_prefix}:%{l_prefix}:g" \
          $RPM_BUILD_ROOT%{l_prefix}/lib/perl/%{version}/*/Config.pm \
          `find $RPM_BUILD_ROOT%{l_prefix}/lib/perl/%{version}/ \
                -type f -name ".packlist" -print`
      find $RPM_BUILD_ROOT%{l_prefix}/lib/perl/ \
           -name "*.orig" -print | xargs rm -f
    ) || exit $?

    #   assume ownership for various arch/site/vendor install dirs
    ( eval "`$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installarchlib`"
      eval "`$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installprivlib`"
      eval "`$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installsitearch`"
      eval "`$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installsitelib`"
      eval "`$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installvendorarch`"
      eval "`$RPM_BUILD_ROOT%{l_prefix}/bin/perl -V:installvendorlib`"
      %{l_shtool} mkdir -f -p -m 755 \
          $RPM_BUILD_ROOT$installarchlib/auto \
          $RPM_BUILD_ROOT$installprivlib/auto \
          $RPM_BUILD_ROOT$installsitearch/auto \
          $RPM_BUILD_ROOT$installsitelib/auto \
          $RPM_BUILD_ROOT$installvendorarch/auto \
          $RPM_BUILD_ROOT$installvendorlib/auto
    ) || exit $?

    #   strip binary files
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* 2>/dev/null || true

    #   resolve file conflicts (with perl-locale)
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/enc2xs \
          $RPM_BUILD_ROOT%{l_prefix}/bin/piconv \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/enc2xs.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/piconv.1

    #   resolve file conflicts (with perl-module)
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/corelist \
          $RPM_BUILD_ROOT%{l_prefix}/bin/config_data \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/config_data.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/corelist.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/cpan.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/instmodsh.1

    #   resolve file conflicts (with perl-sys)
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/ptar \
          $RPM_BUILD_ROOT%{l_prefix}/bin/ptardiff \
          $RPM_BUILD_ROOT%{l_prefix}/bin/ptargrep \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/ptar.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/ptardiff.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/ptargrep.1

    #   resolve file conflicts (with perl-crypto)
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/shasum \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/shasum.1

    #   resolve file conflicts (with perl-comp)
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/zipdetails \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/zipdetails.1

    #   determine installed files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

