%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.13.2
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


%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`
cd src/github.com/mattermost/mattermost-server/
git checkout tags/v%{version}
go get -d ./...
%ifarch ppc64 ppc64le
sed 's/amd64/%{_arch}/g' -i build/release.mk
%endif
make build-linux
cd ../mattermost-webapp
git checkout tags/v%{version}
npm install
%ifarch ppc64 ppc64le
# because optipng doesn't build on ppc64 or ppc64le
cp %{_bindir}/optipng node_modules/optipng-bin/vendor/optipng
%endif
npm run build


%install
mkdir -p %{buildroot}%{mattermost_home}/bin/
install -p -D -m 0555 bin/mattermost %{buildroot}%{mattermost_home}/bin/mattermost
install -p -D -m 0555 bin/platform %{buildroot}%{mattermost_home}/bin/mattermost-platform
install -p -D -m 0555 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
