# SPDX-FileCopyrightText: 2021-22 Samuel Tyler <samuel@samuelt.me>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# XXX: If you change the version of this, you must update the corresponding
# tarball in Python 3.11.

src_prepare() {
    default

    # Remove a bunch of pregenerated files
    rm apps/*.{pem,crt}
    find demos -name "*.pem" -delete
    rm crypto/asn1/charmap.h \
        crypto/bn/bn_prime.h \
        crypto/conf/conf_def.h \
        crypto/ec/ecp_sm2p256_table.c \
        crypto/ec/ecp_nistz256_table.c \
        crypto/objects/obj_dat.h \
        crypto/objects/obj_xref.h
    # This file, while supposedly being generated by util/mkerr.pl, is just
    # a stub and not regenerated by util/mkerr.pl
    mv crypto/kdf/kdf_err{,.bak}.c
    rm crypto/*/?*err.c \
        crypto/*err.c \
        crypto/sslerr.h \
        engines/*_err.{c,h} \
        include/{openssl,internal,crypto}/?*err.h \
        providers/common/include/prov/proverr.h \
        providers/common/provider_err.c
    mv crypto/kdf/kdf_err{.bak,}.c
    rm external/perl/Text-Template-1.56/Makefile.PL \
        external/perl/Text-Template-1.56/MANIFEST \
        external/perl/Text-Template-1.56/t/author-signature.t \
        external/perl/Text-Template-1.56/t/author-pod-syntax.t
    rm fuzz/oids.txt

    # Test pregenerated files
    # This may be more than necessary, but it is easy to delete tests
    find test \( -name "*.pem" \
        -o -name "*.tlssct" \
        -o -name "*.csr" \
        -o -name "*.key" \
        -o -name "*.p12" \
        -o -name "*.der" \
        -o -name "*.dat" \
        -o -name "*.crt" \
        -o -name "*.cms" \) -delete
    rm test/recipes/25-test_pkcs7_data/grfc.out \
        test/recipes/30-test_evp_data/evpmac_siphash.txt \
        test/recipes/80-test_cmp_http_data/test_connection.csv \
        test/recipes/80-test_tsa_data/all-zero.tsq \
        test/recipes/80-test_tsa_data/sectigo-all-zero.tsr \
        test/recipes/90-test_store_data/testrsa.msb \
        test/recipes/90-test_threads_data/store/8489a545.0 \
        test/recipes/90-test_store.t \
        test/shibboleth.pfx
    rm test/bn_rand_range.h \
        test/*.bin \
        test/evp_extra_test2.c \
        test/evp_pkey_provided_test.c

    # And some unverifiable files
    rm doc/designs/quic-design/images/quic-overview.odg \
        doc/life-cycles/lifecycles.ods
}

src_configure() {
    ./Configure --prefix="${PREFIX}" \
        --libdir="${LIBDIR}" \
        no-shared no-tests no-docs linux-generic32
}

src_compile() {
    declare -x SOURCE_DATE_EPOCH=1759320660
    declare -x OSSL_COPYRIGHT_YEAR=2025

    make generate
    perl util/mkerr.pl -internal -rebuild
    for conf in engines/*.ec; do
        local c_file
        c_file="$(basename "$conf" .ec).c"
        perl util/mkerr.pl -conf "$conf" -rebuild "engines/$c_file"
    done
    mv ./*_err.{c,h} engines/

    default
}

src_install() {
    default

    rm -r "${DESTDIR}${PREFIX}/ssl/misc"
}
