# Maintainer: Christopher Price <pricechrispy at gmail dot com>
# Contributor: Siavosh Kasravi <siavosh.kasravi@gmail.com>
# Contributor: JustKidding <jk@vin.ovh>
# Contributor: James P. Harvey <jamespharvey20 at gmail dot com>
# Contributor: Christoph Bayer <chrbayer@criby.de>
# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: Mathias Stearn <mathias@10gen.com>
# Contributor: Alec Thomas
# Contributor: Fredy García <frealgagu at gmail dot com>

pkgname=mongodb
_pkgname=mongodb
# #.<odd number>.# releases are unstable development/testing
pkgver=8.3.7
pkgrel=1
pkgdesc="A high-performance, open source, schema-free document-oriented database"
arch=("x86_64")
url="https://www.mongodb.com/"
license=("SSPL-1.0")
depends=('libstemmer' 'snappy' 'yaml-cpp' 'libunwind' 'curl' 'zstd' 'zlib'
  'libsasl' 'openssl' 'glibc' 'gcc15-libs')
makedepends=('python' 'python-psutil' 'python-setuptools' 'python-regex' 'python-cheetah3'
  'python-yaml' 'python-requests' 'python-pymongo' 'python-gitpython'
  'python-distro' 'python-retry' 'python-timeout-decorator' 'gcc15' 'lld' 'onetbb'
  'bazelisk' 'bc' 'libxml2-legacy' 'python-boto3' 'python-yaml' 'python-pymongo')
optdepends=('mongodb-tools: mongoimport, mongodump, mongotop, etc'
  'mongosh: interactive shell to connect with MongoDB')
backup=("etc/mongodb.conf")
provides=(mongodb="$pkgver")
options=(!debug)
source=("https://github.com/mongodb/mongo/archive/refs/tags/r$pkgver.tar.gz"
  mongodb.sysusers
  mongodb.tmpfiles
  bazel-python-path.patch
  bazel-python-modules.patch
  bazel-distro.patch
  bazel-version.patch
  bazelisk-url.patch
)
sha256sums=('d0179a61d3e126a6fce328db38e5f7568f24dfb1a33748d1b994b7fe641c7d88'
  '3757d548cfb0e697f59b9104f39a344bb3d15f802608085f838cb2495c065795'
  'b7d18726225cd447e353007f896ff7e4cbedb2f641077bce70ab9d292e8f8d39'
  'f1b6bebc6223be86eeb8dc35814f6f2ce15a1748af036dbd777ab0e9e15af02e'
  '96c6558d9f867b4beec7bfcedc27509877649753c09346fec648b8123a38787f'
  '1f68140783cbbb1693866e68fb0fc6eb711021972095ef7188f8f8c9a2978df7'
  '966553db16cae3b5eabb4c2d9793b2276022e6e8b27646e421903a086c166859'
  'f5a202137719fd6422b2f91e38084f145b4870241b3a539dcfd7d3cf5bfc3086'
)

prepare() {
  cd "${srcdir}/mongo-r${pkgver}"

  # Keep historical Arch dbPath
  sed -i 's|dbPath: /var/lib/mongo|dbPath: /var/lib/mongodb|' rpm/mongod.conf

  # Keep historical Arch conf file name
  sed -i 's|-f /etc/mongod.conf|-f /etc/mongodb.conf|' rpm/mongod.service

  # Keep historical Arch user name (no need for separate daemon group name)
  sed -i 's/User=mongod/User=mongodb/' rpm/mongod.service
  sed -i 's/Group=mongod/Group=mongodb/' rpm/mongod.service
  sed -i 's/chown mongod:mongod/chown mongodb:mongodb/' rpm/mongod.service

  # Remove sysconfig file, used by upstream's init.d script not used on Arch
  sed -i '/EnvironmentFile=-\/etc\/sysconfig\/mongod/d' rpm/mongod.service

  # Make systemd wait as long as it takes for MongoDB to start
  # If MongoDB needs a long time to start, prevent systemd from restarting it every 90 seconds
  # See: https://jira.mongodb.org/browse/SERVER-38086
  sed -i 's/\[Service]/[Service]\nTimeoutStartSec=infinity/' rpm/mongod.service

  # Enable Glibc's RSEQ, which as a side effect disables TCMalloc's RSEQ usage, required for kernels 6.19+
  # See: https://jira.mongodb.org/browse/SERVER-121912
  sed -i 's/Environment="GLIBC_TUNABLES=glibc.pthread.rseq=0"/Environment="GLIBC_TUNABLES=glibc.pthread.rseq=1"/' rpm/mongod.service

  for file in "$srcdir"/*.patch; do
    echo "Applying patch $file..."
    patch -Np1 -i "$file"
  done
}

build() {
  cd "${srcdir}/mongo-r${pkgver}"

  # Set gcc15 toolchain for Bazel to resolve C++ toolchain properly
  export CC="${CC:-gcc-15}"
  export CXX="${CXX:-g++-15}"
  export AR="${AR:-gcc-ar-15}"

  # export BAZELISK_SKIP_WRAPPER=1
  # export MONGO_BAZEL_WRAPPER_DEBUG=1
  export USE_NATIVE_TOOLCHAIN=1

  # core does not build the mongo shell (jstestshell)
  # bazelisk build install-core

  # Build using the install-devcore target (includes mongod, mongos, and jstestshell/mongo)
  # Force Bazel to use system toolchain instead of MongoDB's custom toolchain
  # Override MongoDB's .bazelrc that disables native toolchain detection
  # Using some options from --config=opt
  bazelisk build install-devcore \
    --verbose_failures \
    --define=MONGO_VERSION=${pkgver} \
    --define=MONGO_DISTMOD=arch \
    --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type' \
    --action_env=CC="${CC}" \
    --action_env=CXX="${CXX}" \
    --action_env=AR="${AR}" \
    --repo_env=CC="${CC}" \
    --repo_env=CXX="${CXX}" \
    --repo_env=AR="${AR}" \
    --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0 \
    --features=external_include_paths \
    --host_features=external_include_paths \
    --cxxopt='-std=c++20' \
    --host_cxxopt='-std=c++20' \
    --cxxopt='-march=x86-64-v3' \
    --host_cxxopt='-march=x86-64-v3' \
    --cxxopt='-w' \
    --host_cxxopt='-w' \
    --cxxopt='-Wno-error' \
    --host_cxxopt='-Wno-error' \
    --cxxopt='-I/usr/include' \
    --host_cxxopt='-I/usr/include' \
    --copt='-D_GNU_SOURCE' \
    --host_copt='-D_GNU_SOURCE' \
    --copt='-march=x86-64-v3' \
    --host_copt='-march=x86-64-v3' \
    --copt='-w' \
    --host_copt='-w' \
    --copt='-Wno-error' \
    --host_copt='-Wno-error' \
    --copt='-I/usr/include' \
    --host_copt='-I/usr/include' \
    --linkopt='-lresolv' \
    --linkopt='-ltbb' \
    --linkopt='-Wl,-w' \
    --//bazel/config:linker=lld \
    --compilation_mode=opt \
    --fission=yes \
    --//bazel/config:dwarf_version=5 \
    --//bazel/config:compress_debug_compile=True \
    --//bazel/config:opt=on \
    --//bazel/config:dbg=False \
    --//bazel/config:dbg_level=2 \
    --//bazel/config:debug_symbols=True \
    --//bazel/config:separate_debug=False
  # --//bazel/config:thin_lto=True
}

package() {
  cd "${srcdir}/mongo-r${pkgver}"

  # Install binaries from Bazel install-core output
  # Built binaries are in bazel-bin/install-devcore/bin/ (symlinks to real outputs)
  install -D "bazel-bin/install-devcore/bin/mongod" "$pkgdir/usr/bin/mongod"
  install -D "bazel-bin/install-devcore/bin/mongos" "$pkgdir/usr/bin/mongos"

  # install-devcore includes the mongo shell (jstestshell)
  if [[ -f "bazel-bin/install-devcore/bin/mongo" ]]; then
    install -D "bazel-bin/install-devcore/bin/mongo" "$pkgdir/usr/bin/mongo"
  fi

  # Keep historical Arch conf file name
  install -Dm644 "rpm/mongod.conf" "${pkgdir}/etc/${_pkgname}.conf"

  # Keep historical Arch service name
  install -Dm644 "rpm/mongod.service" "${pkgdir}/usr/lib/systemd/system/${_pkgname}.service"

  # Install manpages
  #install -Dm644 "debian/mongo.1" "${pkgdir}/usr/share/man/man1/mongo.1"
  install -Dm644 "debian/mongod.1" "${pkgdir}/usr/share/man/man1/mongod.1"
  install -Dm644 "debian/mongos.1" "${pkgdir}/usr/share/man/man1/mongos.1"

  # Install systemd files
  install -Dm644 "${srcdir}/${_pkgname}.sysusers" "${pkgdir}/usr/lib/sysusers.d/${_pkgname}.conf"
  install -Dm644 "${srcdir}/${_pkgname}.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/${_pkgname}.conf"

  # Install license
  install -D LICENSE-Community.txt "$pkgdir/usr/share/licenses/mongodb/LICENSE"
}
