# Build recipe for python3.
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=Python
version=3.9.14
short_version="${version%.*}"
release=1

# Define a category for the output of the package name
pkgcategory=python

# Define a custom package name
pkgname=python3

# The name of the tarball
tarname=${program}-${version}.tgz

# Remote source(s)
fetch=https://www.python.org/ftp/python/${version}/$tarname

description="
Multi-paradigm programming language (v3).

Python is an interpreted, interactive object-oriented programming
language suitable (amongst other uses) for distributed application
development, scripting, numeric computing and system testing.

Python is often compared to Tcl, Perl, Java, JavaScript, Visual
Basic or Scheme.
"

homepage=https://www.python.org/
license="Python Software Foundation License"

# Source documentation
docs="LICENSE README.rst"
docsdir="${docdir}/${pkgname}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Add compatible support of Python modules to work with LibreSSL

    patch -p0 < "${worktree}/patches/python/patch-Modules__hashopenssl_c"
    patch -p0 < "${worktree}/patches/python/patch-Modules__ssl_c"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Use system libraries instead of the bundle ones
    rm -rf Modules/expat Modules/zlib Modules/_ctypes/libffi*

    ./configure $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --build="$(gcc -dumpmachine)" \
     --enable-shared \
     --enable-loadable-sqlite-extensions \
     --enable-ipv6 \
     --enable-optimizations \
     --with-system-expat \
     --with-system-ffi \
     --with-ensurepip=yes \
     --with-dbmliborder=gdbm:ndbm \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    # Make default symlink to invoke it as "python"
    ( cd "${destdir}/usr/bin" && ln -sf python3 python )

    # Include the Python tools under site-packages
    TOOL_DIR=/usr/lib/python${short_version}
    (
        cd Tools || exit 2

        # Do not clobber README file in site-packages directory
        test -f README && mv -f README README.Tools

        cp -rP ./* "${destdir}${TOOL_DIR}/"
    )

    # Make some useful symlinks at usr/bin
    (
        cd "${destdir}/usr/bin" || exit 2

        ln -sf "${TOOL_DIR}/i18n/msgfmt.py" msgfmt.py
        ln -sf "${TOOL_DIR}/i18n/pygettext.py" pygettext.py
        ln -sf "${TOOL_DIR}/pynche/pynche" pynche
    )
    unset -v short_version TOOL_DIR

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

