%global _hardened_build 1
%define grafana_user grafana
%define grafana_group grafana
%define grafana_home /var/lib/grafana
%define grafana_logdir /var/log/grafana
#%define debug_package %{nil}

Name         : grafana
Version      : 7.1.3
Release      : 1%{dist}
License      : MIT
URL          : http://gogs.io
Summary      : The tool for beautiful monitoring and metric analytics & dashboards
Source0      : grafana.service
Source1      : grafana.sysconfig
Source2      : grafana.logrotate
BuildRequires: golang
BuildRequires: git
Requires     : git


%description
The open platform for beautiful
analytics and monitoring


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


%build
export GOPATH=`pwd`
cd src/github.com/grafana/grafana
git checkout tags/v%{version}
go mod download
go run build.go setup
go run build.go build


%install
install -p -D -m 0644 %{SOURCE0} %{buildroot}%{_unitdir}/grafana.service
mkdir -p %{buildroot}%{_sysconfdir}/grafana/
cp -r src/github.com/grafana/grafana/conf/* %{buildroot}%{_sysconfdir}/grafana/
#install -p -D -m 0644 src/github.com/grafana/grafana/conf/* %{buildroot}%{_sysconfdir}/grafana/
install -p -D -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/sysconfig/grafana
install -p -D -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/logrotate.d/grafana
mkdir -p %{buildroot}%{_datadir}/%{name}/
cp -r src/github.com/grafana/grafana/public/ %{buildroot}%{_datadir}/%{name}/
cp -r src/github.com/grafana/grafana/scripts/ %{buildroot}%{_datadir}/%{name}/
cp -r src/github.com/grafana/grafana/tools/ %{buildroot}%{_datadir}/%{name}/
echo %{version} > %{buildroot}%{_datadir}/%{name}/VERSION
install -p -D -m 0555 src/github.com/grafana/grafana/bin/linux-%{_arch}/grafana-server %{buildroot}%{_sbindir}/grafana-server
install -p -D -m 0555 src/github.com/grafana/grafana/bin/linux-%{_arch}/grafana-cli %{buildroot}%{_bindir}/grafana-cli
install -p -d -m 0700 %{buildroot}%{grafana_home}
install -p -d -m 0700 %{buildroot}%{grafana_logdir}


%pre
getent group %{grafana_group} > /dev/null || groupadd -r %{grafana_group}
getent passwd %{grafana_user} > /dev/null || \
    useradd -r -d %{grafana_home} -g %{grafana_group} \
    -s /bin/bash -c "Grafana user" %{grafana_user}
exit 0


%post
%systemd_post grafana.service


%preun
%systemd_preun grafana.service


%postun
%systemd_postun grafana.service


%files
%attr(775,%{grafana_user},%{grafana_group}) %{_sysconfdir}/%{name}/
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%{_sbindir}/grafana-server
%{_bindir}/grafana-cli
%{_unitdir}/grafana.service
%{_datadir}/%{name}/VERSION
%{_datadir}/%{name}/public/
%{_datadir}/%{name}/scripts/
%{_datadir}/%{name}/tools/


%changelog
