%define tag_ver %(echo %{version} | awk -F. '{print $1"_"$2}')

Name:           pgadmin4
Version:        6.4
Release:        3%{?commit:.git%shortcommit}%{?dist}
Summary:        Administration tool for PostgreSQL
ExclusiveArch: %{qt5_qtwebengine_arches}

# PostgreSQL ist the main license, rest the bundled JS code (see %%{name}-%%{version}-vendor-licenses.txt)
License:        PostgreSQL and MIT and ISC and BSD and ASL 2.0 and CC-BY and CC0 and WTFPL and zlib and GPLv2 and GPLv3+ and Python
URL:            https://www.pgadmin.org/
Source0:        https://github.com/postgres/pgadmin4/archive/REL-%{tag_ver}/%{name}-%{version}.tar.gz

# ./prepare_vendor.sh
Source1:        %{name}-%{version}-vendor.tar.xz
Source2:        %{name}-%{version}-vendor-licenses.txt

Source3:        pgadmin4.cpp
Source4:        org.postgresql.pgadmin4.metainfo.xml

# Config file fixes
Patch0:         pgadmin4.patch
# Patch requirements for Fedora compat
Patch1:         pgadmin4_requirements.patch
# Don't error out on sphinx warnings
Patch2:         pgadmin4_sphinx_werror.patch
# Don't minimize code with webpack terser plugin (only applied to 32bit arches to avoid JS heap memory exhaustion)
Patch100:       pgadmin4_noterser.patch

# For node dependencies
BuildRequires:  autoconf
BuildRequires:  automake
BuildRequires:  libpng-devel
BuildRequires:  libtool
BuildRequires:  yasm

BuildRequires:  desktop-file-utils
BuildRequires:  libappstream-glib
BuildRequires:  gcc-c++
BuildRequires:  python3-devel
BuildRequires:  python3-sphinx
BuildRequires:  qt5-qtbase-devel
BuildRequires:  qt5-qtwebengine-devel
BuildRequires:  yarnpkg

# cd pgadmin4-<ver>
# patch -p1 < pgadmin4_requirements.patch
# python3 /usr/lib/rpm/redhat/pyproject_buildrequires.py -N requirements.txt 2>/dev/null | awk '{print "Requires: "$0}'
Requires: (python3dist(flask) >= 2 with python3dist(flask) < 3)
Requires: (python3dist(flask-gravatar) >= 0 with python3dist(flask-gravatar) < 1)
Requires: (python3dist(flask-login) >= 0 with python3dist(flask-login) < 1)
Requires: (python3dist(flask-mail) >= 0 with python3dist(flask-mail) < 1)
Requires: (python3dist(flask-migrate) >= 3 with python3dist(flask-migrate) < 4)
Requires: (python3dist(flask-sqlalchemy) >= 2.5 with python3dist(flask-sqlalchemy) < 2.6)
Requires: (python3dist(flask-wtf) >= 1 with python3dist(flask-wtf) < 2)
Requires: (python3dist(flask-compress) >= 1 with python3dist(flask-compress) < 2)
Requires: (python3dist(flask-paranoid) >= 0 with python3dist(flask-paranoid) < 1)
Requires: (python3dist(flask-babel) >= 2 with python3dist(flask-babel) < 3)
Requires: (python3dist(flask-security-too) >= 4 with python3dist(flask-security-too) < 5)
Requires: python3dist(flask-socketio) >= 5.0.1
Requires: (python3dist(wtforms) >= 3 with python3dist(wtforms) < 4)
Requires: (python3dist(passlib) >= 1 with python3dist(passlib) < 2)
Requires: (python3dist(pytz) >= 2021 with python3dist(pytz) < 2022)
Requires: (python3dist(simplejson) >= 3 with python3dist(simplejson) < 4)
Requires: (python3dist(six) >= 1 with python3dist(six) < 2)
Requires: (python3dist(sqlparse) >= 0 with python3dist(sqlparse) < 1)
Requires: (python3dist(psutil) >= 5 with python3dist(psutil) < 6)
Requires: (python3dist(psycopg2) >= 2.9 with python3dist(psycopg2) < 2.10)
Requires: (python3dist(python-dateutil) >= 2 with python3dist(python-dateutil) < 3)
Requires: (python3dist(sqlalchemy) >= 1.4 with python3dist(sqlalchemy) < 1.5)
Requires: (python3dist(bcrypt) >= 3 with python3dist(bcrypt) < 4)
Requires: python3dist(cryptography) >= 3
Requires: (python3dist(sshtunnel) >= 0 with python3dist(sshtunnel) < 1)
Requires: (python3dist(ldap3) >= 2 with python3dist(ldap3) < 3)
Requires: python3dist(gssapi) >= 1.6
Requires: python3dist(eventlet) = 0.33
Requires: (python3dist(httpagentparser) >= 1.9 with python3dist(httpagentparser) < 1.10)
Requires: python3dist(user-agents) = 2.2
Requires: (python3dist(authlib) >= 0.15 with python3dist(authlib) < 0.16)
Requires: python3dist(requests) >= 2.25
Requires: (python3dist(pyotp) >= 2 with python3dist(pyotp) < 3)
Requires: (python3dist(qrcode) >= 7 with python3dist(qrcode) < 8)
Requires: python3dist(pillow) >= 8.3




%description
pgAdmin is the most popular and feature rich Open Source administration and development
platform for PostgreSQL, the most advanced Open Source database in the world.


%define lang_subpkg() \
%package langpack-%{1}\
Summary:       %{2} language data for %{name}\
BuildArch:     noarch\
Requires:      %{name} = %{version}-%{release}\
Supplements:   (%{name} = %{version}-%{release} and langpacks-%{1})\
\
%description langpack-%{1}\
%{2} language data for %{name}.\
\
%files langpack-%{1}\
%{_prefix}/lib/%{name}/pgadmin/translations/%{1}/

%lang_subpkg cs Czech
%lang_subpkg de German
%lang_subpkg es Spanish
%lang_subpkg fr French
%lang_subpkg it Italian
%lang_subpkg ja Japanese
%lang_subpkg ko Korean
%lang_subpkg pl Polish
%lang_subpkg ru Russian
%lang_subpkg zh Chinese


%generate_buildrequires
%pyproject_buildrequires -N requirements.txt


%prep
%setup -q -n %{name}-REL-%{tag_ver} -a1
%autopatch -p1 -M99
%ifarch i686 armv7hl
%patch100 -p1
%endif

sed -i 's|Exec=.*|Exec=%{_bindir}/%{name}|' pkg/linux/%{name}.desktop
cp -a %{SOURCE2} .


%build
(
cd web

%ifarch aarch64
# Fix undefined reference to `png_init_filter_functions_neon'
CPPFLAGS="$CPPFLAGS -DPNG_ARM_NEON_OPT=0" \
%endif
YARN_CACHE_FOLDER="$PWD/../.package-cache" yarn install --offline
# Workaround deprecated md4 used in webpack
find node_modules/webpack/lib/ -type f -exec sed -i 's|md4|sha256|g' {} \;
yarn run bundle
rm -rf node_modules
)
g++ -o %{name} %{SOURCE3} %{optflags} $(pkg-config --cflags --libs Qt5Core Qt5Widgets Qt5Network Qt5WebEngineWidgets)
make docs


%install
install -Dpm 0755 %{name} %{buildroot}%{_bindir}/%{name}
mkdir -p %{buildroot}%{_prefix}/lib/
cp -a web %{buildroot}%{_prefix}/lib/%{name}
for size in 16 32 48 64 128; do
    install -Dpm 0644 pkg/linux/%{name}-${size}x${size}.png %{buildroot}%{_datadir}/icons/hicolor/${size}x${size}/apps/%{name}.png
done
install -Dpm 0644 pkg/linux/%{name}.desktop %{buildroot}%{_datadir}/applications/%{name}.desktop
install -Dpm 0644 %{SOURCE4} %{buildroot}%{_metainfodir}/org.postgresql.pgadmin4.metainfo.xml


%check
appstream-util validate-relax --nonet %{buildroot}%{_metainfodir}/org.postgresql.pgadmin4.metainfo.xml
desktop-file-validate %{buildroot}%{_datadir}/applications/%{name}.desktop


%files
%license LICENSE %{name}-%{version}-vendor-licenses.txt
%doc docs/en_US/_build/html
%{_bindir}/%{name}
%{_prefix}/lib/%{name}
# Packaged by separate langpack subpackages
%exclude %{_prefix}/lib/%{name}/pgadmin/translations/*
%{_datadir}/applications/%{name}.desktop
%{_datadir}/icons/hicolor/*/apps/%{name}.png
%{_metainfodir}/org.postgresql.pgadmin4.metainfo.xml


%changelog
* Tue Jan 25 2022 Sandro Mani <manisandro@gmail.com> - 6.4-3
- Generate and add %%{name}-%%{version}-vendor-licenses.txt

* Tue Jan 25 2022 Sandro Mani <manisandro@gmail.com> - 6.4-2
- Add splash screen

* Thu Jan 13 2022 Sandro Mani <manisandro@gmail.com> - 6.4-1
- Update to 6.4

* Fri Dec 24 2021 Sandro Mani <manisandro@gmail.com> - 6.3-2
- Update pgadmin4.cpp

* Fri Dec 17 2021 Sandro Mani <manisandro@gmail.com> - 6.3-1
- Update to 6.3

* Thu Nov 11 2021 Sandro Mani <manisandro@gmail.com> - 6.3-0.1.git3a87e05
- Initial package