# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Dan McGee <dan@archlinux.org>

pkgbase=postgresql
pkgname=(
  postgresql
  postgresql-libs
  postgresql-docs
)
pkgver=18.1
pkgrel=1
pkgdesc="Sophisticated object-relational DBMS"
url="https://www.postgresql.org/"
arch=(x86_64 aarch64)
license=(PostgreSQL)
depends=(
  bash
  gcc-libs
  glibc
  icu
  krb5
  libldap
  liburing
  libxml2
  libxslt
  llvm-libs
  lz4
  numactl
  openssl
  pam
  systemd-libs
  util-linux-libs
  zlib
  zstd
)
makedepends=(
  clang
  curl
  docbook-xml
  docbook-xsl
  llvm
  perl
  perl-ipc-run
  python
  readline
  systemd
  tcl
  util-linux
)
source=(
  https://ftp.postgresql.org/pub/source/v${pkgver}/postgresql-${pkgver}.tar.bz2
  0001-Set-DEFAULT_PGSOCKET_DIR-to-run-postgresql.patch
  0002-Force-RPATH-to-be-used-for-the-PL-Perl-plugin.patch
  postgresql-check-db-dir.in
  postgresql.logrotate
  postgresql.pam
  postgresql.service
  postgresql.sysusers
  postgresql.tmpfiles
  llvm-21-aarch64.patch::https://www.postgresql.org/message-id/attachment/181518/llvm-21-aarch64.patch
)
b2sums=('5e3cc11ab2940f2c1f971b3ac4c6c4850cfb446da4322ca389e1fcdb4d5f9369c5618ca8d57687fea15512d004cf596fd701811f3ad807742a66ded8264d41c8'
        '057e65b1e5a1fd82c7fa9da1c4e2fef39321ee59f74f54d9864e8ab4d7db4862c8fb6353f6006e1a9e1f5132543cf0c12b388963487a09b1465447b3ff6d7321'
        'd98280e004adcb1ad85053d9bd202a2b4798df6e7a72b21cb263da6b282f42b5463ce77dcc4f0b1b39d3f55303d0f6869b23f2338ffcd98720b0550204adf85a'
        '682bfab1189221d82f6cce9bcb8c40c6d37ecacbcaf0568bbaa2706503b1f102e5476d0d110dcc6240ab62c5e7c2c20a5d9af8edd4038d212327f8866b1cf15a'
        '2209b7550acad7955102ec6922754b4046b2a2ad2a7e1cfb2cc4053c0705abac7aa7d7968eab617f50894797d06345f51c9a669926bd2a77dcf688206a2027e0'
        '3eab84d332d96678fe6e435ee243c8f1a82b838f601d61d3604d11e918aed7a62202edca5e476c4b9031ed284570e6fcd6c659cfdbd9624aa0019d3233755f81'
        '4543982e70337d01f308bd1a7178b9f60fae5edfb50f14e410e4164ac9f4c11625b6a93bbb66adf808e3c445690db0e77a9b9c7e561d76e4550234ee4e2b6ea0'
        'e025e78797879323bb78752dff8ea4eae984015240d273de8e83e1b7f7f7a8b9a549c33df6b72c40721f05a3df149fe065f9bb8c3fdf4f3b9f759963bb3da288'
        'd66fcad8c00ed29cb50cf6721c89023787b42973c3bf50535c4f822a8870883557396b3ec5cc79465e75d0786246a523db69368b2a7feff40c3430c736999cca'
        'd730f79f14ec6c0e4b3b8cbe7b885643de4830c6ce19c20d5e349e4009cf81b017168324595a4d72841f07da2fc8bbbcd2c3d310163f8e76358203d90f34ce99')

# PostgreSQL releases are unsigned and only provide these sums
md5sums=('32f047dc587bbc90a4f68ea7b903485d'
         '344d51b4e3ddf4cb4c2e143650b0476b'
         'b74d848f5527740dce663ae72f39c5ae'
         '10123caec61006cbe316de8ab7a497bc'
         'd28e443f9f65a5712c52018b84e27137'
         '96f82c38f3f540b53f3e5144900acf17'
         'fe9d4dea2d878a7f0b555b0dae333793'
         'da6a0dda84638d0c3463abf4bc01e621'
         '9138244e0e6b98acc770931a4b150b86'
         '58fdb5a17290bde6f9d45c908ea00b38')
sha256sums=('ff86675c336c46e98ac991ebb306d1b67621ece1d06787beaade312c2c915d54'
            '064b7e266094e07feae29735efab3246e2b7a3364d6948d2f98d338e827d6a1a'
            '0fb4915c06b9767933b27adc329e7319485e043fb9f17b1697b969779a00cf14'
            '94af93b53bf7772e6664c239523ef952ffc905a0de3c2c4b2dfc2fe8f3a2efed'
            '6abb842764bbed74ea4a269d24f1e73d1c0b1d8ecd6e2e6fb5fb10590298605e'
            '57dfd072fd7ef0018c6b0a798367aac1abb5979060ff3f9df22d1048bb71c0d5'
            '3a0bbf9fd61259c4898465c55a8b7cd26202885132ff2b5c685dfe0e0ddd4968'
            '3cfe36dd202af56b3ef8e6d6a746b24e6f46f0d9e0d3fa125dbfb5e598170afb'
            'ea771830c15b24c8725ded92e6a9ba9848b13f722357c5f5857dfeb21985d54c'
            'b19e69d83736b3e0144d422df59f34505ce60ebb0e6bc429985e7fb11a40d67c')

prepare() {
  cd postgresql-${pkgver}
  patch -Np1 < ../0001-Set-DEFAULT_PGSOCKET_DIR-to-run-postgresql.patch
  patch -Np1 < ../0002-Force-RPATH-to-be-used-for-the-PL-Perl-plugin.patch
  patch -Np1 < ../llvm-21-aarch64.patch

  # Disabling test with io_method=io_uring failing in CI
  sed -i src/test/modules/test_aio/t/001_aio.pl \
    -e 's/if (have_io_uring())/if (0 \&\& have_io_uring())/'
}

build() {
  local configure_options=(
    --prefix=/usr
    --sysconfdir=/etc
    --mandir=/usr/share/man
    --datadir=/usr/share/postgresql
    --disable-rpath
    --enable-nls
    --enable-tap-tests
    --with-gssapi
    --with-icu
    --with-ldap
    --with-libcurl
    --with-libnuma
    --with-liburing
    --with-libxml
    --with-libxslt
    --with-llvm
    --with-lz4
    --with-openssl
    --with-pam
    --with-perl
    --with-python
    --with-readline
    --with-system-tzdata=/usr/share/zoneinfo
    --with-systemd
    --with-tcl
    --with-uuid=e2fs
    --with-zstd
  )

  # use fat LTO objects for static libraries
  CFLAGS+=" -ffat-lto-objects"
  CXXFLAGS+=" -ffat-lto-objects"

  mkdir -p build; cd build
  ../postgresql-${pkgver}/configure "${configure_options[@]}"
  make world
}

check() (
  export LANG=C LC_ALL=C
  if ! make -C build check-world; then
    find . -name regression.diffs | while read -r line; do
      echo "make check-world failure: ${line}"
      cat -- "${line}" || :
    done
    exit 1
  fi
)

_pick() {
  local p="$1" f d; shift
  for f; do
    d="${srcdir}/${p}/${f#${pkgdir}/}"
    mkdir -p "$(dirname "${d}")"
    mv "${f}" "${d}"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "${f}")"
  done
}

package_postgresql() {
  depends+=("postgresql-libs>=${pkgver}")
  optdepends=(
    'logrotate: rotates system logs automatically'
    'perl: for PL/Perl support'
    'postgresql-old-upgrade: upgrade from previous major version using pg_upgrade'
    'python: for PL/Python 3 support'
    'tcl: for PL/Tcl support'
  )
  options+=(staticlibs)
  install=postgresql.install
  backup=(
    etc/logrotate.d/postgresql
    etc/pam.d/postgresql
  )

  make -C build DESTDIR="${pkgdir}" install-world

  (
    cd "${pkgdir}"

    _pick libs usr/bin/pg_{config,dump,dumpall,isready,restore}
    _pick libs usr/bin/{cluster,reindex,vacuum}db
    _pick libs usr/bin/{create,drop}{db,user}
    _pick libs usr/bin/{ecpg,psql,vacuumlo}

    _pick libs usr/include/{ecpg,libpq,pg,postgres_,sql}*
    _pick libs usr/include/postgresql/internal

    _pick libs usr/lib/lib*
    _pick libs usr/lib/pkgconfig

    _pick libs usr/share/locale/*/LC_MESSAGES/{ecpg,libpq,pgscripts,psql}*
    _pick libs usr/share/locale/*/LC_MESSAGES/pg_{config,dump}-*

    _pick libs usr/share/man/man1/pg_{config,dump,dumpall,isready,restore}.1
    _pick libs usr/share/man/man1/{cluster,reindex,vacuum}db.1
    _pick libs usr/share/man/man1/{create,drop}{db,user}.1
    _pick libs usr/share/man/man1/{ecpg,psql,vacuumlo}.1

    _pick libs usr/share/postgresql/{pg_service.conf,psqlrc}.sample

    _pick docs usr/share/doc
  )

  local majorver=${pkgver%%.*}
  sed -e "s/%PGMAJORVERSION%/${majorver}/g" \
      -e "s/%PREVMAJORVERSION%/$((majorver - 1))/g" \
      postgresql-check-db-dir.in |
    install -Dm755 /dev/stdin "${pkgdir}/usr/bin/postgresql-check-db-dir"

  install -Dm644 postgresql.pam "${pkgdir}/etc/pam.d/postgresql"
  install -Dm644 postgresql.logrotate "${pkgdir}/etc/logrotate.d/postgresql"
  install -Dm644 postgresql.service -t "${pkgdir}/usr/lib/systemd/system"
  install -Dm644 postgresql.sysusers "${pkgdir}/usr/lib/sysusers.d/postgresql.conf"
  install -Dm644 postgresql.tmpfiles "${pkgdir}/usr/lib/tmpfiles.d/postgresql.conf"

  install -Dm644 postgresql-${pkgver}/COPYRIGHT -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_postgresql-libs() {
  pkgdesc+=" - Client binaries and libraries"
  depends=(
    curl libcurl.so
    glibc
    krb5 libgssapi_krb5.so
    libldap
    lz4 liblz4.so
    openssl libcrypto.so libssl.so
    readline libreadline.so
    zlib libz.so
    zstd libzstd.so
  )
  provides=(
    libecpg.so
    libecpg_compat.so
    libpgtypes.so
    libpq.so
    postgresql-client
  )
  conflicts=(postgresql-client)

  mv libs/* "${pkgdir}"

  install -Dm644 postgresql-${pkgver}/COPYRIGHT -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_postgresql-docs() {
  pkgdesc+=" - HTML documentation"
  depends=()

  mv docs/* "${pkgdir}"

  install -Dm644 postgresql-${pkgver}/COPYRIGHT -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

# vim:set sw=2 sts=-1 et:
