Name:             ergo
Version:          2.7.0
Release:          1
Group:            Productivity/Networking/IRC
Summary:          IRCv3 Server
License:          MIT
URL:              https://oragono.io
#Original source:
#https://github.com/ergochat/ergo/tree/master
#Modified source (custom ircd.yaml configuration and systemd unit file):
Source0:	  %{name}-%{version}.tar.gz
#Go source:
Source1:          vendor.tar.gz
###
# Dependencies for openSUSE
###
%if 0%{?suse_version}
BuildRequires:    go >= 1.16
BuildRequires:    golang-packaging systemd-rpm-macros systemd fdupes
Requires(pre):    /usr/sbin/useradd, /usr/bin/getent
%endif
###

###
# Dependencies for Fedora
###
%if 0%{?fedora}
BuildRequires:    golang go-rpm-macros systemd systemd-rpm-macros fdupes
Requires(pre):    /usr/sbin/useradd, /usr/bin/getent
%endif
###

%define __arch_install_post export NO_BRP_STRIP_DEBUG=true

%global provider_prefix github.com/ergochat/ergo
%global import_path %{provider_prefix}

###
# Make go-rpm-macros happy!
###
%if 0%{?fedora}
%global goipath github.com/ergochat/ergo
%global debug_package %{nil}
%endif

###
# Okay openSUSE, let's stretch first!
###
%if 0%{?suse_version}
%{go_nostrip}
%{go_provides}
%endif

###
# Come on Fedora, you go along!
###
%if 0%{?fedora}
%gopkg
%endif

%description
Oragono is a modern IRC server written in Go. 
#A pretty cool one, that is!

%pre
#Introducing Ms. Oragono!
/usr/bin/getent group ergo || /usr/sbin/groupadd -r ergo
/usr/bin/getent passwd ergo || /usr/sbin/useradd -r -g ergo -d /etc/ergo -s /sbin/nologin ergo
%if 0%{?suse_version}
%service_add_pre ergo.service
%endif

%prep
###
# Alright openSUSE, here we go!
###
%setup -q -n %{name}-%{version}

###
# Oh Fedora, you having bit of an headstart, eh?
###
%if 0%{?fedora}
%goprep
%endif
%setup -q -T -D -a 1


%build
export GOFLAGS="-mod=vendor"
###
# "I'm prepping too now!! Don't rush me!", shouts openSUSE
###
%if 0%{?suse_version}
%goprep %{import_path}
%gobuild .
%endif

###
# S'ok! I've got some extra work here...
###
%if 0%{?fedora}
rm -rf "%{_builddir}/%{extractdir0}/_build/src/github.com/ergochat/ergo"
ln -s "%{_builddir}/%{name}-%{version}" "%{_builddir}/%{extractdir0}/_build/src/github.com/ergochat/ergo"
%gobuild -o %{gobuilddir}/bin/%{name} %{goipath}/irc
%endif

%install
###
# And.. installaction!
###
%if 0%{?suse_version}
%goinstall
%gosrc
%endif

###
# "Lame", moans Fedora, "I'm lifting some extra weights, not use some weird macro for my copyjobs!"
###
%if 0%{?fedora}
install -m 0755 -vd %{buildroot}%{_bindir}
install -m 0755 -vp %{gobuilddir}/bin/* %{buildroot}%{_bindir}/
%endif

###
# However after a short fight between the two, they continue doing some tasks in common, and achieve great teamwork.
###
install -d -m 700 %{buildroot}%{_sysconfdir}/%{name}
install -D -m 600 %{_builddir}/%{name}-%{version}/rpm/default.yaml %{buildroot}%{_sysconfdir}/%{name}/ircd.yaml
install -D -m 644 %{_builddir}/%{name}-%{version}/rpm/%{name}.service %{buildroot}%{_unitdir}/%{name}.service
install -d -m 755 %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}

%check
###
# Let's see who made their job well...
###
%if 0%{?suse_version}
%gotest %{import_path}
%endif

%if 0%{?fedora}
%gocheck
%endif

###
# Alright, time to sum things up!!
###

###
# "Hah, I have a nice filelist ready to go!"
###
%if 0%{?suse_version}
%gofilelist
%files -f file.lst
%endif

###
# "Says the one WHO NEEDS a filelist!"
###
%if 0%{?fedora}
%files
%endif

%license LICENSE
%doc README.md
%attr(750,ergo,ergo) %{_bindir}/%{name}
%dir %attr (700,ergo,ergo) %{_sysconfdir}/%{name}
%attr (-,ergo,ergo) %config(noreplace) %{_sysconfdir}/%{name}/ircd.yaml
%{_unitdir}/%{name}.service
%{_sbindir}/rc%{name}

%post
###
# Some finishing touches, Fedora and openSUSE walk hand in hand, through filesystem's wonderland!
###
%if 0%{?suse_version}
%service_add_post ergo.service
%endif
%if 0%{?fedora}
%systemd_post ergo.service
%endif
install -d -o ergo -g ergo -m 700 %{_sysconfdir}/%{name}/.sec
%{_bindir}/%{name} mkcerts --conf %{_sysconfdir}/%{name}/ircd.yaml || true
%{_bindir}/%{name} initdb --conf %{_sysconfdir}/%{name}/ircd.yaml || true
chown ergo:ergo %{_sysconfdir}/%{name}/.sec/fullchain.pem
chown ergo:ergo %{_sysconfdir}/%{name}/.sec/privkey.pem
chown ergo:ergo %{_sysconfdir}/%{name}/ircd.db
chmod 600 %{_sysconfdir}/%{name}/ircd.db
###
# Nice! All the distros cheer in excitement about the newly installed package!
###
echo "Welcome to Ergo!"
echo "To enable the service (auto-start): sudo systemctl enable ergo"
echo "To start the service now: sudo systemctl start ergo"
echo "The configuration is located in /etc/ergo/ircd.yaml"

%preun
%if 0%{?suse_version}
%service_del_preun ergo.service
%endif
%if 0%{?fedora}
%systemd_preun ergo.service
%endif

%postun
#%if 0%{?suse_version}
#%service_del_postun_without_restart ergo.service
#%endif
%if 0%{?fedora}
%systemd_postun ergo.service
%endif
echo "NOTE - The ergo.service does NOT restart automatically."

###
# Rants from some h00man
###
%changelog
* Sun Mar 07 2021 Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net> - 2.7.0-1
- Initial release of the Ergo v2.7.0 RPM package (deprecating the Oragono package)
