%undefine _debugsource_packages
%global _default_patch_fuzz 2

Name:		gitea
Version:	1.21.7
Release:	1%{dist}
Summary:	Git with a cup of tea, painless self-hosted git service
License:	MIT
Group:		Development/Other
URL:		https://gitea.io/
Source0:	https://github.com/go-gitea/gitea/archive/v%{version}.tar.gz#/%{name}-%{version}.tar.gz
Source6:    gitea.sysusers
Source10:   gitea.service
Source11:   gitea.service.d.conf
Source12:   app-gitea.ini
Patch0:     make-version.patch
Requires:   git-core
BuildRequires:	git
BuildRequires:	golang >= 1.21
BuildRequires:  make
BuildRequires:  pam-devel
BuildRequires:  pkgconfig(sqlite3)
BuildRequires:  pkgconfig(openssl) >= 3.0
BuildRequires:  npm >= 1:8.19
BuildRequires:  nodejs >= 1:18.0
BuildRequires:  systemd-rpm-macros

%description
The goal of this project is to make the easiest, fastest, and most painless way
of setting up a self-hosted Git service. It is similar to GitHub, Bitbucket,
and Gitlab. Gitea is a fork of Gogs.

%prep
%autosetup -p1

%build
export GOPATH="`pwd`/.godeps"
export CGO_CFLAGS="$CGO_CFLAGS -I%{_includedir}/openssl3"
export LDFLAGS='-X "code.gitea.io/gitea/modules/setting.StaticRootPath=%{_datadir}/gitea/web" -X "code.gitea.io/gitea/modules/setting.AppWorkPath=%{_localstatedir}/lib/gitea/" -X "code.gitea.io/gitea/modules/setting.CustomConf=%{_sysconfdir}/gitea/app.ini" -X "main.MakeVersion=GNU Make 4.3" -X "main.Version=%{version}" -X "main.Tags=bindata tidb sqlite sqlite_unlock_notify pam"'
TAGS="bindata tidb sqlite sqlite_unlock_notify pam" make VERSION=%{version} generate all

%install
mkdir -p %{buildroot}%{_localstatedir}/lib/%{name}
mkdir -p %{buildroot}%{_localstatedir}/log/%{name}
mkdir -p %{buildroot}%{_sysconfdir}/systemd/system/%{name}.service.d/
install -Dm 0755 %{name} %{buildroot}%{_bindir}/%{name}
install -Dm 0640 %{SOURCE10} %{buildroot}%{_unitdir}/%{name}.service
install -Dm 0640 %{SOURCE11} %{buildroot}%{_sysconfdir}/systemd/system/%{name}.service.d/port.conf
install -Dm 0660 %{SOURCE12} %{buildroot}%{_sysconfdir}/%{name}/app.ini

install -p -D -m 0644 %{SOURCE6} \
    %{buildroot}%{_sysusersdir}/%{name}.conf

mkdir -p %{buildroot}%{_tmpfilesdir}
cat > %{buildroot}%{_tmpfilesdir}/%{name}.conf <<EOF
d /run/%{name} 0755 gitea gitea -
EOF

install -d -m 755 %{buildroot}%{_datadir}/%{name}/web
cp -r options public templates %{buildroot}%{_datadir}/%{name}/web

%pre
%sysusers_create_compat %{SOURCE6}

%post
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf
%systemd_post gitea.service 

%preun
%systemd_preun gitea.service

%postun
%systemd_postun gitea.service

%files
%doc custom/conf/app.example.ini
%doc CHANGELOG.md CODE_OF_CONDUCT.md CONTRIBUTING.md README*.md SECURITY.md
%{_bindir}/%{name}
%dir %attr(0700,%name,%name) %{_localstatedir}/lib/%{name}
%dir %attr(0700,%name,%name) %{_localstatedir}/log/%{name}
%dir %{_sysconfdir}/%{name}
%config(noreplace) %attr(0660,root,%name) %{_sysconfdir}/%{name}/app.ini
%config(noreplace) %attr(0644,root,root) %{_sysconfdir}/systemd/system/%{name}.service.d/port.conf
%{_datadir}/%{name}/web
%{_unitdir}/%{name}.service
%{_tmpfilesdir}/%{name}.conf
%{_sysusersdir}/%{name}.conf

%changelog
* Tue Feb 27 2024 Raven <raven@sysadmins.ws> - 1.21.7-1
- update to 1.20.7

* Mon Feb 26 2024 Raven <raven@sysadmins.ws> - 1.21.6-2
- fix overwriting of gitea.service.d/port.conf

* Mon Feb 26 2024 Raven <raven@sysadmins.ws> - 1.21.6-1
- update to 1.20.6

* Wed Feb 21 2024 Raven <raven@sysadmins.ws> - 1.21.5-5
- add systemd scriptlets

* Wed Feb 21 2024 Raven <raven@sysadmins.ws> - 1.21.5-4
- add CAP_NET_BIND_SERVICE

* Tue Feb 20 2024 Raven <raven@sysadmins.ws> - 1.21.5-3
- package static assets

* Tue Feb 20 2024 Raven <raven@sysadmins.ws> - 1.21.5-2
- update default configs

* Tue Feb 20 2024 Raven <raven@sysadmins.ws> - 1.21.5-1
- Initial build
