# Template file for 'openssl'
pkgname=openssl
version=3.5.8
revision=4
short_desc="Cryptography and TLS library and tools"
maintainer="finwo <finwo@pm.me>"
license="Apache-2.0"
homepage="https://www.openssl.org"
distfiles="https://www.openssl.org/source/openssl-3.5.8.tar.gz"
checksum=a8f84a39918ec6415ce765d9b429d313ba97b8143169c172e734b9514464f5b2
# glibc first: the post-install runs /sbin/ldconfig, which needs libc
# present. busybox first: control scripts run under /bin/sh, which must
# exist before the script executes (ordering, not just runtime truth -
# apk runs each package's scripts right after unpacking it).
depends="glibc busybox"
hostmakedepends="perl"

# Notes:
# - 3.5 is the LTS line (supported to 2030); the 3.6 feature line is
#   deliberately not taken. Pin cross-checked against the published .sha256.
# - perl is build-only (same category as python3 for glibc), never shipped.
# - no-docs skips the multi-thousand-page man build; ship code only.
# - Shared-only per packages/README.md: the .a files (tens of MB) are
#   removed after install.
# - install_sw (software without docs) into DESTDIR and SYSROOT; the
#   sysroot .pc copies get the prefix rewrite so apk-tools' muon/meson
#   configure links under the sysroot.

do_configure() {
  cd "${WRKSRC}"
  case "${ARCH:-x86_64}" in
    x86_64) _ossl_target=linux-x86_64 ;;
    aarch64|arm64) _ossl_target=linux-aarch64 ;;
    *) die "openssl: unsupported ARCH ${ARCH}" ;;
  esac
  perl ./Configure --prefix=/usr --libdir=lib no-docs "${_ossl_target}"
}

do_build() {
  cd "${WRKSRC}"
  make -j"$(nproc)"
}

do_install() {
  cd "${WRKSRC}"
  make install_sw DESTDIR="${DESTDIR}"
  make install_sw DESTDIR="${SYSROOT}"
  rm -f "${DESTDIR}/usr/lib/libcrypto.a" "${DESTDIR}/usr/lib/libssl.a"
  rm -f "${SYSROOT}/usr/lib/libcrypto.a" "${SYSROOT}/usr/lib/libssl.a"
  for pc in libcrypto.pc libssl.pc openssl.pc; do
    [ -f "${SYSROOT}/usr/lib/pkgconfig/${pc}" ] || die "openssl: no sysroot ${pc}"
  done
}
