# docker-finance | modern accounting for the power-user
#
# Copyright (C) 2025-2026 Aaron Fiore (Founder, Evergreen Crypto LLC)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

FROM archlinux:base-devel AS root-build

RUN pacman -Syu \
  git \
  --noconfirm --disable-download-timeout

RUN useradd -m -s /bin/bash -r builder
RUN gpasswd -a builder wheel
RUN sed -i 's/# %wheel ALL=(ALL:ALL) NOPASSWD: ALL/%wheel ALL=(ALL:ALL) NOPASSWD: ALL/' /etc/sudoers

USER builder
WORKDIR /home/builder
RUN git clone --depth=1 https://gitlab.archlinux.org/archlinux/packaging/packages/root -b 6.38.04-1

WORKDIR /home/builder/root

#
# CMake settings
#

# Build with C++20 support
RUN sed -i -e 's/STANDARD 17/STANDARD 20/g' -e 's/c++17/c++20/g' settings.cmake

# Disable TMVA
RUN sed -i -e 's/tmva ON/tmva OFF/' -e 's/tmva-cpu ON/tmva-cpu OFF/' -e 's/tmva-pymva ON/tmva-pymva OFF/' settings.cmake
# Disable Fortran
RUN sed -i -e 's/fortran ON/fortran OFF/' settings.cmake
# Disable GDML
RUN sed -i -e 's/gdml ON/gdml OFF/' settings.cmake
# Disable XRootD
RUN sed -i -e 's/xrootd ON/xrootd OFF/' settings.cmake
# Disable WebGUI (not HTTP server)
RUN sed -i -e 's/webgui ON/webgui OFF/' settings.cmake
# Disable Qt6-WebEngine
RUN sed -i -e 's/qt6web ON/qt6web OFF/' settings.cmake
# Disable x11
RUN sed -i -e 's/x11 ON/x11 OFF/' settings.cmake

# Update final b2sum of settings.cmake
RUN sed -i 's/6eacf425d2a75e7bcabbf9d1b3bcf4c7d06093774ef1adee9131c582caac68d433fdfafc2772032d02a996849d7b81e369bdbf1298992d0b37630108b90a193c/412eac1f470fe544e2143511aae89aa91301cb9c4fa0fa1bdee99b875d7846e2f735a3a09d21d4fd6e82ec28ca6a05980090deda94807050fc5e45a6cfcd5e9c/' PKGBUILD

#
# PKGBUILD
#

# Remove CUDA build
RUN sed -i 's/pkgname=(root root-cuda)/pkgname=(root)/' PKGBUILD
RUN sed -i -e '/build-cuda/d' -e '/package_root-cuda() {/,/}/{/.*/d}' PKGBUILD

# Remove CUDA makedep
RUN sed -i -E -e '/^  cud(a|nn)$/d' PKGBUILD
# Remove Fortran makedep
RUN sed -i -e '/^  gcc-fortran$/d' PKGBUILD
# Remove XRootD makedep
RUN sed -i -e '/^  xrootd$/d' PKGBUILD
# Remove Chromium makedep (WebGUI)
RUN sed -i -e '/^  chromium$/d' PKGBUILD
# Remove Qt6-WebEngine
RUN sed -i -e '/^  qt6-webengine$/d' PKGBUILD

# Do not build root debug package
RUN sed -i "s/options=(\!lto)$/options=('\!debug' '\!lto')/" PKGBUILD

# Execute build
RUN PACKAGER="https://gitea.evergreencrypto.co/EvergreenCrypto/docker-finance" makepkg -s --noconfirm

#
# Production stage
#

FROM archlinux:base AS root

COPY --from=root-build /home/builder/root/root-*.pkg.tar.zst /root/

RUN pacman -Syu \
  gcc \
  --noconfirm --disable-download-timeout

WORKDIR /root
RUN pacman -U root-*.pkg.tar.zst --noconfirm

# vim: sw=2 sts=2 si ai et
