%global _hardened_build 1
%define mattermost_user mattermost
%define mattermost_group mattermost
%define mattermost_home /var/lib/mattermost
%define debug_package %{nil}

Name         : mattermost
Version      : 5.37.4
Release      : 1%{dist}
License      : MIT
URL          : https://mattermost.io
Summary      : Open source Slack-alternative in Golang and React
Source0      : mattermost.service
Source1      : mattermost.logrotate
BuildRequires: golang
BuildRequires: git
BuildRequires: optipng
BuildRequires: gifsicle
BuildRequires: libwebp-tools
BuildRequires: pngquant


%description
Mattermost is a flexible, open source messaging platform
that meets even the most demanding privacy and security standards.


%prep
%setup -T -q -c %{name}-%{version}
export GOPATH=`pwd`
mkdir -p src/github.com/mattermost/
cd src/github.com/mattermost/
git clone https://github.com/mattermost/mattermost-server/
git clone https://github.com/mattermost/mattermost-webapp/



%build
export GOPATH=`pwd`
export GO111MODULE=on
pushd src/github.com/mattermost/mattermost-server/
git checkout v%{version}
git submodule update --init --recursive
#go get -d ./...
%ifarch ppc64 ppc64le
sed 's/amd64/%{_arch}/g' -i build/release.mk
%endif
make build-linux
popd
pushd src/github.com/mattermost/mattermost-webapp/
source /opt/rh/devtoolset-10/enable
git checkout v%{version}
rm -rf node_modules/
rm -rf package-lock.json
npm install --force #--save
### ### ### # we need to patch optipng from npm
### ### ### git clone https://github.com/imagemin/optipng-bin node_modules/optipng-bin/
### ### ### sed 's/--with-system-zlib/--with-system-zlib --with-system-libpng/' -i node_modules/optipng-bin/lib/install.js
### ### ### rm -rf node_modules/optipng-bin/.git/
### ### ### cp %{_bindir}/optipng node_modules/optipng-bin/vendor/optipng
### ### ### rm -rf node_modules/pngquant-bin
### ### ### git clone https://github.com/imagemin/pngquant-bin node_modules/pngquant-bin
### ### ### cp %{_bindir}/pngquant node_modules/pngquant-bin/vendor/pngquant
### ### ### rm -rf node_modules/imagemin-pngquant
### ### ### git clone https://github.com/imagemin/imagemin-pngquant node_modules/imagemin-pngquant
### ### ### npm rebuild
### ### ### %ifarch ppc64 ppc64le
### ### ### # because optipng doesn't build on ppc64 or ppc64le
### ### ### cp %{_bindir}/optipng node_modules/optipng-bin/vendor/optipng
### ### ### cp %{_bindir}/gifsicle node_modules/gifsicle/vendor/gifsicle
### ### ### cp %{_bindir}/cwebp node_modules/cwebp-bin/vendor/cwebp
### ### ### cp %{_bindir}/dwebp node_modules/cwebp-bin/vendor/dwebp
### ### ### cp %{_bindir}/pngquant node_modules/pngquant-bin/vendor/pngquant
### ### ### %endif
### ### ### sed "/import favicon16x16 from 'images\/favicon\/favicon-16x16.png';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/import favicon32x32 from 'images\/favicon\/favicon-32x32.png';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/import redDotFavicon32x32 from 'images\/favicon\/favicon-reddot-32x32.png';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/import redDotFavicon96x96 from 'images\/favicon\/favicon-reddot-96x96.png';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/const link16x16 = document.querySelector<HTMLLinkElement>('link\[rel="icon"\]\[sizes="16x16"\]');/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/const link32x32 = document.querySelector<HTMLLinkElement>('link\[rel="icon"\]\[sizes="32x32"\]');/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/link16x16!.href = typeof redDotFavicon16x16 === 'string' ? redDotFavicon16x16 : '';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/link32x32!.href = typeof redDotFavicon32x32 === 'string' ? redDotFavicon32x32 : '';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/link16x16!.href = typeof favicon16x16 === 'string' ? favicon16x16 : '';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
### ### ### sed "/link32x32!.href = typeof favicon32x32 === 'string' ? favicon32x32 : '';/d" -i components/favicon_title_handler/favicon_title_handler.tsx
npm run build
popd


%install
mkdir -p %{buildroot}%{mattermost_home}/bin/
install -p -D -m 0555 src/github.com/mattermost/mattermost-server/bin/mattermost %{buildroot}%{mattermost_home}/bin/mattermost
#install -p -D -m 0555 src/github.com/mattermost/mattermost-server/bin/platform %{buildroot}%{mattermost_home}/bin/mattermost-platform
install -p -D -m 0555 src/github.com/mattermost/mattermost-server/bin/interface_generator %{buildroot}%{mattermost_home}/bin/mattermost-interface_generator
mkdir -p %{buildroot}%{mattermost_home}/config/
#cp -r  src/github.com/mattermost/mattermost-server/config/* %{buildroot}%{mattermost_home}/config/
#cp -r %{buildroot}%{mattermost_home}/config/default.json %{buildroot}%{mattermost_home}/config/config.json
cp -r src/github.com/mattermost/mattermost-server/templates/ %{buildroot}%{mattermost_home}/
cp -r src/github.com/mattermost/mattermost-server/fonts/ %{buildroot}%{mattermost_home}/
cp -r src/github.com/mattermost/mattermost-server/i18n/ %{buildroot}%{mattermost_home}/
cp -r src/github.com/mattermost/mattermost-webapp/dist/ %{buildroot}%{mattermost_home}/client/
mkdir -p %{buildroot}%{mattermost_home}/data/
mkdir -p %{buildroot}%{mattermost_home}/logs/
mkdir -p %{buildroot}%{mattermost_home}/plugins/
install -p -D -m 0644 %{SOURCE0} %{buildroot}%{_unitdir}/mattermost.service
install -p -D -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/mattermost


%pre
getent group %{mattermost_group} > /dev/null || groupadd -r %{mattermost_group}
getent passwd %{mattermost_user} > /dev/null || \
    useradd -r -d %{mattermost_home} -g %{mattermost_group} \
    -s /bin/bash -c "Mattermost user" %{mattermost_user}
exit 0


%post
%systemd_post mattermost.service


%preun
%systemd_preun mattermost.service


%postun
%systemd_postun mattermost.service


%files
%defattr(-,%{mattermost_user},%{mattermost_group},-)
%attr(555,%{mattermost_user},%{mattermost_group}) %{mattermost_home}/bin/mattermost
#%attr(555,%{mattermost_user},%{mattermost_group}) %{mattermost_home}/bin/mattermost-platform
%attr(555,%{mattermost_user},%{mattermost_group}) %{mattermost_home}/bin/mattermost-interface_generator
#%config(noreplace) %{mattermost_home}/config/config.json
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%{mattermost_home}/config/
%{mattermost_home}/templates/
%{mattermost_home}/fonts/
%{mattermost_home}/i18n/
%{mattermost_home}/client/
%{mattermost_home}/data/
%{mattermost_home}/logs/
%{mattermost_home}/plugins/
%attr(644,root,root) %{_unitdir}/mattermost.service


%changelog
