%bcond_with systemd

%global orig_version 1.1.0
%global git_date     20200404

Summary:        A simple FastCGI server for CGI applications.
Summary(ja):    CGIアプリケーションを動作させるためのシンプルなFastCGIサーバ
Name:           fcgiwrap
Version:        %{orig_version}.git%{git_date}
Release:        2%{?_dist_release}%{?with_systemd:.systemd}
Group:          System Environment/Daemons
Vendor:         Project Vine
Distribution:   Vine Linux
License:        GPL+
URL:            https://github.com/gnosek/fcgiwrap
Source0:        %{name}-%{orig_version}.tar.gz
Source1:        fcgiwrap_params
Source2:        fcgiwrap.init
Source3:        fcgiwrap.sysconfig
Source4:        %{name}@.service
Source5:        %{name}@.socket
Source6:        fcgiwrap.sysconfig.systemd
Patch0:         fcgiwrap-HEAD.patch
# https://github.com/gnosek/fcgiwrap/pull/39
Patch1:         %{name}-1.1.0-use_pkg-config_libsystemd.patch
# https://github.com/gnosek/fcgiwrap/pull/43
Patch2:         %{name}-1.1.0-declare_cgi_error_noreturn.patch
# https://github.com/gnosek/fcgiwrap/pull/44
Patch3:         %{name}-1.1.0-fix_kill_param_sequence.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires: fcgi-devel
%if %{with systemd}
BuildRequires:  systemd-devel
%{?systemd_requires}
%else
Requires: spawn-fcgi
Requires(post): /sbin/chkconfig
Requires(preun): /sbin/chkconfig
Requires(preun): /sbin/service
%endif

%description
 fcgiwrap is a simple server for running CGI applications over FastCGI.
It hopes to provide clean CGI support to Nginx (and other web servers
that may need it).

%description -l ja
  fcgiwrapは、Nginx等のCGIをサポートしていないWebサーバ上でCGIアプリケー
ションを動作させるための、シンプルなFastCGIサーバです。

%package nginx
Summary: A Nginx configuration file to use fcgiwrap
Group: System Environment/Daemons
Requires: %{name} = %{version}-%{release}
Requires: nginx

%description nginx
 This package contains a Nginx configuration file to use fcgiwrap.


%prep
%setup -q -n fcgiwrap-%{orig_version}
%patch0 -p1
%if %{with systemd}
%patch1 -p1
%endif
%patch2 -p1
%patch3 -p1

autoreconf -i

%build
%if %{with systemd}
%configure --prefix="" --with-systemd
%else
%configure
%endif

make

%install
rm -rf %{buildroot}
make DESTDIR=%{buildroot} install

if [ -d %{buildroot}%{_prefix}%{_prefix} ]; then
  mv %{buildroot}%{_prefix}%{_prefix}/* %{buildroot}%{_prefix}/
fi
mkdir -p %{buildroot}%{_sysconfdir}/{nginx,sysconfig}
mkdir -p %{buildroot}%{_initdir}/
install -m644 %{SOURCE1} %{buildroot}%{_sysconfdir}/nginx/

%if %{with systemd}
# Remove the default systemd files 
rm -f %{buildroot}%{_unitdir}/fcgiwrap.service
rm -f %{buildroot}%{_unitdir}/fcgiwrap.socket

# Install our own systemd config files
install -Dm 644 %{SOURCE4} %{buildroot}%{_unitdir}/%{name}@.service
install -Dm 644 %{SOURCE5} %{buildroot}%{_unitdir}/%{name}@.socket
install -m644 %{SOURCE6} %{buildroot}%{_sysconfdir}/sysconfig/fcgiwrap
%else
install -m755 %{SOURCE2} %{buildroot}%{_initdir}/fcgiwrap
install -m644 %{SOURCE3} %{buildroot}%{_sysconfdir}/sysconfig/fcgiwrap
%endif

%clean
rm -rf %{buildroot}

%post
%if %{with systemd}
%systemd_post %{name}@.service
%systemd_post %{name}@.socket
%else
/sbin/chkconfig --add fcgiwrap
%endif

%preun
%if %{with systemd}
%systemd_preun %{name}@.service
%systemd_preun %{name}@.socket
%else
if [ $1 -eq 0 ]; then
  /sbin/service fcgiwrap stop > /dev/null 2>&1
  /sbin/chkconfig --del fcgiwrap
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart %{name}@.service
%systemd_postun_with_restart %{name}@.socket
%else
if [ $1 -ge 1 ]; then
  /sbin/service fcgiwrap condrestart 2>&1 >/dev/null
fi
%endif

%files
%defattr(-,root,root)
%license COPYING
%doc README.rst
%{_sbindir}/fcgiwrap
%{_mandir}/man8/fcgiwrap.8*
%config(noreplace) %{_sysconfdir}/sysconfig/fcgiwrap
%if %{with systemd}
%{_unitdir}/%{name}@.service
%{_unitdir}/%{name}@.socket
%else
%attr(0755,root,root) %{_initdir}/fcgiwrap
%endif

%files nginx
%defattr(-,root,root)
%config(noreplace) %{_sysconfdir}/nginx/fcgiwrap_params

%changelog
* Sun Apr 05 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.1.0.git20200404-2
- fixed %%changelog.

* Sat Apr 04 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.1.0.git20200404-1
- updated to git HEAD.
- added systemd stuff (disabled as default).

* Sun Jan 12 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.1.0-1
- new upstream release.

* Fri Apr 27 2012 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.0.3-2
- fixed configurations.

* Thu May 19 2011 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.0.3-1
- initial built.