#=============================================================================
# Overview of build options:
#
# End of build options
#-----------------------------------------------------------------------------

##trace

%global debug_package %{nil}
%global build_ldflags %{nil}

%global _smp_mflags -j1

%global FORGEJO_USR   git
%global FORGEJO_GRP   forgejo

Name: forgejo
Version: 10.0.1
Release: 1%{?dist}
Summary: Beyond coding, we forge
License: GPL-3.0-or-later
URL: https://codeberg.org/forgejo/forgejo
Source0: %{URL}/releases/download/v%{version}/forgejo-src-%{version}.tar.gz
Source1: forgejo_app.ini
Source2: forgejo.tmpfiles
Source3: forgejo.service
Source4: forgejo_apache_proxy.conf
Source5: forgejo_nginx_proxy.conf

BuildRequires: golang
BuildRequires: nodejs
BuildRequires: make
BuildRequires: systemd-rpm-macros
Requires: git
Requires: git-lfs
%{?systemd_requires}

%description
Forgejo is a self-hosted lightweight software forge. Easy to install and low
maintenance, it just does the job.


%package apache
Summary:        Apache configuration for %{name}
Group:          Development/Tools
Requires:       %{name}
Requires:       httpd

%description apache
Apache configuration files for %{name}


%package nginx
Summary:        Nginx configuration for %{name}
Group:          Development/Tools
Requires:       %{name}
Requires:       nginx

%description nginx
Nginx configuration files for %{name}


#=============================================================================
#  prep
#-----------------------------------------------------------------------------
%prep
%setup -q -n forgejo-src-%{version}


#=============================================================================
# build
#-----------------------------------------------------------------------------
%build
TAGS="bindata timetzdata sqlite sqlite_unlock_notify" make %{_smp_mflags} build


#=============================================================================
# install
#-----------------------------------------------------------------------------
%install
# create directories
install -d -m0750 %{buildroot}/{etc,run,var/log}/%{name}

# instal gitea binary and rename to forgejo
install -D -m0750 gitea %{buildroot}%{_bindir}/%{name}

# activate config
install -D -m0640 %{SOURCE1} %{buildroot}%{_sysconfdir}/%{name}/app.ini

# install systemd files
install -D -m0644 %{SOURCE2} %{buildroot}%{_tmpfilesdir}/%{name}.conf
install -D -m0644 %{SOURCE3} %{buildroot}%{_unitdir}/%{name}.service

# install apache proxy config
install -D -m0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/httpd/conf.d/forgejo_apache_proxy.conf

# install nginx proxy config
install -D -m0644 %{SOURCE5} %{buildroot}%{_sysconfdir}/nginx/sites-available/forgejo_nginx_proxy.conf

#install -Dm755 gitea --target-directory %%{buildroot}%%{_bindir}
#install -Dm644 contrib/systemd/forgejo.service --target-directory %%{buildroot}%%{_prefix}/lib/systemd/system
#mv %%{buildroot}%%{_bindir}/gitea %%{buildroot}%%{_bindir}/forgejo


#=============================================================================
# files
#-----------------------------------------------------------------------------
%files
%license LICENSE
%doc README.md
%{_bindir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/app.ini
%{_localstatedir}/log/%{name}
%{_tmpfilesdir}/%{name}.conf
%{_unitdir}/%{name}.service
#%%{_prefix}/lib/systemd/system/forgejo.service
#%%{_bindir}/forgejo


#=============================================================================
# files apache
#-----------------------------------------------------------------------------
%files apache
%config(noreplace) %{_sysconfdir}/httpd/conf.d/forgejo_apache_proxy.conf


#=============================================================================
# files nginx
#-----------------------------------------------------------------------------
%files nginx
%config(noreplace) %{_sysconfdir}/nginx/sites-available/forgejo_nginx_proxy.conf


#=============================================================================
# pre
#-----------------------------------------------------------------------------
%pre
getent group %{FORGEJO_GRP} > /dev/null || groupadd -r %{FORGEJO_GRP}
getent passwd %{FORGEJO_USR} > /dev/null || \
  useradd -m -g %{FORGEJO_GRP} -s /usr/bin/bash \
  -c "Forgejo daemon user" -d /home/%{FORGEJO_USR} \
  %{FORGEJO_USR}


#=============================================================================
# post
#-----------------------------------------------------------------------------
%post
if [ ! -f %{_sysconfdir}/%{name}/app.ini ]; then
  install -D -m0640 %{_sharedstatedir}/%{name}/custom/conf/app.ini.working %{_sysconfdir}/%{name}/app.ini
fi

# create required directories
mkdir -m0750 -p /home/%{FORGEJO_USR}/%{name}/conf
mkdir -m0750 -p /home/%{FORGEJO_USR}/%{name}/repos

# add config symlink to /home/git/conf/app.ini for easier backup
if [ -f %{_sysconfdir}/%{name}/app.ini ] ; then
  if [ -e /home/%{FORGEJO_USR}/%{name}/conf/app.ini ] ; then
    rm -f /home/%{FORGEJO_USR}/%{name}/conf/app.ini
  fi
  ln -s %{_sysconfdir}/%{name}/app.ini /home/%{FORGEJO_USR}/%{name}/conf/app.ini
fi

# set hostname in active config
#sed -i -e "s|localhost|`hostname`|" %{_sysconfdir}/%{name}/app.ini

chown -R %{FORGEJO_USR}:%{FORGEJO_GRP} %{_sysconfdir}/%{name}
chown -R %{FORGEJO_USR}:%{FORGEJO_GRP} /home/%{FORGEJO_USR} > /dev/null
chown %{FORGEJO_USR}:%{FORGEJO_GRP} %{_bindir}/%{name}

restorecon -v -F -R /home/%{FORGEJO_USR} > /dev/null
restorecon -v -F -R %{_sysconfdir}/%{name} > /dev/null

# enable httpd_can_network_connect or apache/nginx -> forgejo is blocked
/usr/sbin/setsebool httpd_can_network_connect 1

systemctl daemon-reload
%systemd_post forgejo.service


#=============================================================================
# post apache
#-----------------------------------------------------------------------------
%post apache
%systemd_post httpd.service


#=============================================================================
# post nginx
#-----------------------------------------------------------------------------
%post nginx
%systemd_post nginx.service


#=============================================================================
# preun
#-----------------------------------------------------------------------------
%preun
%systemd_preun forgejo.service


#=============================================================================
# postun
#-----------------------------------------------------------------------------
%postun
%systemd_postun_with_restart forgejo.service


#=============================================================================
# postun apache
#-----------------------------------------------------------------------------
%postun apache
%systemd_postun_with_restart httpd.service


#=============================================================================
# postun nginx
#-----------------------------------------------------------------------------
%postun nginx
%systemd_postun_with_restart nginx.service


#=============================================================================
# changelog
#-----------------------------------------------------------------------------
%changelog
* Sat Feb 08 2025 Patrick Laimbock <patrick@laimbock.com> - 10.0.1-1
- update to version 10.0.1

* Sun Feb 02 2025 Patrick Laimbock <patrick@laimbock.com> - 10.0.0-1
- update to version 10.0.0

* Fri Dec 13 2024 Patrick Laimbock <patrick@laimbock.com> - 9.0.3-1
- update to version 9.0.3

* Wed Oct 30 2024 Patrick Laimbock <patrick@laimbock.com> - 9.0.1-2
- update to version 9.0.1

* Wed Oct 16 2024 Forgejo Release Team <release-team@noreply.codeberg.org>
See https://codeberg.org/forgejo/forgejo/src/branch/forgejo/RELEASE-NOTES.md#9-0-0