%global _hardened_build 1
%define gogs_user git
%define gogs_group git
%define gogs_home /var/lib/gogs/
%define gogs_logdir /var/log/gogs/
%define debug_package %{nil}

Name         : gogs
Version      : 0.11.86
Release      : 1%{dist}
License      : MIT
URL          : http://gogs.io
Summary      : Gogs(Go Git Service) is a painless self-hosted Git Service written in Go.
Source0      : gogs.service
Source1	     : gogs.logrotate
BuildRequires: golang
BuildRequires: git
Requires     : git


%description
Gogs(Go Git Service) is a painless self-hosted Git Service written in Go.


%prep
%setup -T -q -c %{name}-%{version}
export GOPATH=`pwd`
go get -u -v github.com/gogs/gogs


%build
export GOPATH=`pwd`
cd src/github.com/gogs/gogs
git checkout tags/v%{version}
go build -ldflags '-extldflags -s -w -buildid %{version}'
sed -i 's|^STATIC_ROOT_PATH =$|STATIC_ROOT_PATH = /usr/share/gogs/templates/default/|g' conf/app.ini
sed -i 's|^ROOT =$|ROOT = /var/lib/gogs/|g' conf/app.ini
sed -i 's|^APP_DATA_PATH = data$|APP_DATA_PATH = /var/lib/gogs/data|g' conf/app.ini
sed -i 's|^PROVIDER_CONFIG = data/sessions$|PROVIDER_CONFIG = /var/lib/gogs/data/sessions|g' conf/app.ini
sed -i 's|^AVATAR_UPLOAD_PATH = data/avatars$|AVATAR_UPLOAD_PATH = /var/lib/gogs/data/avatars|g' conf/app.ini
sed -i 's|^PATH = data/attachments$|PATH = /var/lib/gogs/data/attachments|g' conf/app.ini
sed -i 's|^ROOT_PATH =$|ROOT_PATH  = /var/log/gogs|g' conf/app.ini
sed -i 's|^TEMP_PATH = data/tmp/uploads$|TEMP_PATH = /var/lib/gogs/data/tmp/uploads|g' conf/app.ini


%install
install -p -D -m 0644 %{SOURCE0} %{buildroot}%{_unitdir}/gogs.service
install -p -D -m 0644 src/github.com/gogs/gogs/conf/app.ini %{buildroot}%{_sysconfdir}/gogs/app.ini
cp -r src/github.com/gogs/gogs/conf/locale %{buildroot}%{_sysconfdir}/gogs/
cp -r src/github.com/gogs/gogs/conf/license %{buildroot}%{_sysconfdir}/gogs/
cp -r src/github.com/gogs/gogs/conf/label %{buildroot}%{_sysconfdir}/gogs/
cp -r src/github.com/gogs/gogs/conf/readme %{buildroot}%{_sysconfdir}/gogs/
install -p -D -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/gogs
mkdir -p %{buildroot}%{_datadir}/%{name}/templates/default/
cp -r src/github.com/gogs/gogs/templates %{buildroot}%{_datadir}/%{name}/templates/default/
cp -r src/github.com/gogs/gogs/public %{buildroot}%{_datadir}/%{name}/templates/default/
install -p -D -m 0555 src/github.com/gogs/gogs/gogs %{buildroot}%{_bindir}/gogs
install -p -d -m 0700 %{buildroot}%{gogs_home}
install -p -d -m 0700 %{buildroot}%{gogs_logdir}


%pre
getent group %{gogs_group} > /dev/null || groupadd -r %{gogs_group}
getent passwd %{gogs_user} > /dev/null || \
    useradd -r -d %{gogs_home} -g %{gogs_group} \
    -s /bin/bash -c "Gogs user" %{gogs_user}
exit 0


%post
%systemd_post gogs.service


%preun
%systemd_preun gogs.service


%postun
%systemd_postun gogs.service


%files
%attr(775,%{gogs_user},%{gogs_group}) %dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/app.ini
%{_sysconfdir}/%{name}/locale/*
%{_sysconfdir}/%{name}/license/*
%{_sysconfdir}/%{name}/label/*
%{_sysconfdir}/%{name}/readme/*
%{_sysconfdir}/logrotate.d/%{name}
%{_bindir}/gogs
%{_unitdir}/gogs.service
%{_datadir}/%{name}/templates/
%attr(700,%{gogs_user},%{gogs_group}) %dir %{gogs_home}
%attr(700,%{gogs_user},%{gogs_group}) %dir %{gogs_logdir}


%changelog

