%bcond_with     systemd

%define         uid             23
%define         gid             23

%define _unpackaged_files_terminate_build 1

Summary:        The Squid proxy caching server
Summary(ja):    Squid ウェブプロキシキャッシュ
Name:           squid
Epoch:          7
Version:        5.2
Release:        1%{?_dist_release}%{?with_systemd:.systemd}
Group:          servers
Vendor:         Project Vine
Distribution:   Vine Linux

License:        GPLv2+
URL:            http://www.squid-cache.org
Source0:        http://www.squid-cache.org/Versions/v5/%{name}-%{version}.tar.xz
Source1:        %{name}.init
Source2:        %{name}.logrotate
Source3:        %{name}.sysconfig
Source4:        %{name}.pam
Source5:        %{name}.service
Source6:        cache_swap.sh

Patch201:       squid-4.0.11-config.patch
Patch202:       squid-3.1.0.9-location.patch
Patch203:       squid-3.0.STABLE1-perlpath.patch
Patch204:       squid-3.5.9-include-guards.patch
# revert this upstream patch - https://bugzilla.redhat.com/show_bug.cgi?id=1936422
# workaround for #1934919
Patch205:       squid-5.0.5-symlink-lang-err.patch
Patch206:       squid-5.2-openssl3.patch
# fix -lto build failure
Patch207:       squid-5.2-test-store-cppsuite.patch

BuildRoot:      %{_tmppath}/%{name}-%{version}-root
BuildRequires:  openldap-devel pam-devel openssl-devel krb5-devel
BuildRequires:  cyrus-sasl-devel libcap-devel libtdb-devel
Requires:       bash >= 2.0
Requires(pre):  shadow-utils
%if %{with systemd}
%{?systemd_requires}
%else
Requires(post): %{_syssbindir}/chkconfig
Requires(preun): %{_syssbindir}/chkconfig
%endif

Conflicts:      squid30
Obsoletes:      squid-novm

%description
Squid is a high-performance proxy caching server for Web clients,
supporting FTP, gopher, and HTTP data objects. Unlike traditional
caching software, Squid handles all requests in a single,
non-blocking, I/O-driven process. Squid keeps meta data and especially
hot objects cached in RAM, caches DNS lookups, supports non-blocking
DNS lookups, and implements negative caching of failed requests.

Squid consists of a main server program squid, a Domain Name System
lookup program (dnsserver), a program for retrieving FTP data
(ftpget), and some management and client tools.

%description -l ja
Squid はウェブクライアントのための高性能のプロキシキャッシュサーバで、
FTP, gopher, HTTP データオブジェクトに対応しています。これまでの
キャッシュソフトウェアと違い、 Squid は全ての要求を、単一の
ブロックしない I/O ドリブンのプロセスで処理します。 Squid はメタデータや
ホットなオブジェクトを RAM に保持し、 DNS 問い合わせをキャッシュします。
ブロック無しの DNS 問い合わせをサポートし、失敗した要求のネガティブ
キャッシングが実装されています。

Squid は、メインのサーバプログラム squid と、 Domain Name System
問い合わせプログラム (dnsserver) と、 FTP データを取得するプログラム
(ftpget) と、いくつかの管理用またはクライアントツールから成ります。


%debug_package


%prep
%setup -q
%patch201 -p1 -b .config
%patch202 -p1 -b .location
%patch203 -p1 -b .perlpath
%patch204 -p0 -b .include-guards
%patch205 -p1 -R -b .symlink-lang-err
%patch206 -p1 -b .openssl3
%patch207 -p1 -b .flto


%build
%configure \
   --libexecdir=%{_libdir}/%{name} \
   --datadir=%{_datadir}/%{name} \
   --sysconfdir=%{_sysconfdir}/%{name} \
   --with-logdir=%{_localstatedir}/log/%{name} \
   --disable-dependency-tracking \
   --enable-eui \
   --enable-follow-x-forwarded-for \
   --enable-arp-acl \
   --enable-auth \
   --enable-auth-basic="DB,fake,getpwnam,LDAP,NCSA,PAM,POP3,SASL,SMB,SMB_LM" \
   --enable-auth-ntlm="SMB_LM,fake" \
   --enable-auth-digest="file,LDAP" \
   --enable-auth-negotiate="kerberos" \
   --enable-external-acl-helpers="LDAP_group,time_quota,session,unix_group,wbinfo_group,kerberos_ldap_group" \
   --enable-cache-digests \
   --enable-cachemgr-hostname=localhost \
   --enable-delay-pools \
   --enable-epoll \
   --enable-icap-client \
   --enable-ident-lookups \
   %ifnarch ppc64 ia64 x86_64 s390x
   --with-large-files \
   %endif
   --enable-linux-netfilter \
   --enable-referer-log \
   --enable-removal-policies="heap,lru" \
   --enable-snmp \
   --enable-ssl \
   --enable-ssl-crtd \
   --enable-storeio="aufs,diskd,ufs,rock" \
   --enable-diskio \
   --enable-useragent-log \
   --enable-wccpv2 \
   --enable-internal-dns \
   --with-aio \
   --with-default-user="%{name}" \
   --with-filedescriptors=16384 \
   --with-dl \
   --with-openssl \
   --with-pthreads \
   --disable-arch-native \
   --disable-security-cert-validators \
   --disable-strict-error-checking \
%if %{with systemd}
   --with-pidfile='/run/squid.pid' \
%endif
  %{nil}

# workaround to build squid v5
mkdir -p src/icmp/tests
mkdir -p tools/squidclient/tests
mkdir -p tools/tests

%{__make} \
    %{?_smp_mflags} \
    DEFAULT_SWAP_DIR="%{_localstatedir}/cache/%{name}"


%install
[ "${RPM_BUILD_ROOT}" != "/" ] && %{__rm} -rf ${RPM_BUILD_ROOT}

%{__make} install \
    DESTDIR="${RPM_BUILD_ROOT}" \
    DEFAULT_SWAP_DIR="%{_localstatedir}/cache/%{name}"

echo "
#
# This is %{_sysconfdir}/httpd/conf.d/%{name}.conf
#

ScriptAlias /Squid/cgi-bin/cachemgr.cgi %{_libdir}/%{name}/cachemgr.cgi

# Only allow access from localhost by default
<Location /Squid/cgi-bin/cachemgr.cgi>
 order allow,deny
 allow from localhost.localdomain
 # Add additional allowed hosts as needed
 # allow from .example.com
</Location>" > ${RPM_BUILD_ROOT}/%{name}.httpd.tmp

mkdir -p ${RPM_BUILD_ROOT}%{_initdir}
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/logrotate.d
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/sysconfig
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/pam.d
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf.d/

%if %{with systemd}
install -Dpm644 %{SOURCE5} ${RPM_BUILD_ROOT}%{_unitdir}/%{name}.service
install -Dpm755 %{SOURCE6} ${RPM_BUILD_ROOT}%{_libexecdir}/%{name}/cache_swap.sh
%else
install -m 755 %{SOURCE1} ${RPM_BUILD_ROOT}%{_initdir}/%{name}
%endif
install -m 644 %{SOURCE2} ${RPM_BUILD_ROOT}%{_sysconfdir}/logrotate.d/%{name}
install -m 644 %{SOURCE3} ${RPM_BUILD_ROOT}%{_sysconfdir}/sysconfig/%{name}
install -m 644 %{SOURCE4} ${RPM_BUILD_ROOT}%{_sysconfdir}/pam.d/%{name}
install -m 644 ${RPM_BUILD_ROOT}/%{name}.httpd.tmp ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf.d/%{name}.conf
mkdir -p ${RPM_BUILD_ROOT}%{_localstatedir}/log/%{name}
mkdir -p ${RPM_BUILD_ROOT}%{_localstatedir}/cache/%{name}
chmod 644 contrib/url-normalizer.pl contrib/user-agents.pl
iconv -f ISO88591 -t UTF8 ChangeLog -o ChangeLog.tmp
mv -f ChangeLog.tmp ChangeLog

# remove unpackaged files from the buildroot
rm -f ${RPM_BUILD_ROOT}/%{name}.httpd.tmp


%clean
[ "${RPM_BUILD_ROOT}" != "/" ] && %{__rm} -rf ${RPM_BUILD_ROOT}


%pretrans
for l in zh-cn zh-tw; do
  if [ -d /usr/share/squid/errors/$l -a ! -L /usr/share/squid/errors/$l ]; then
    rm -rf /usr/share/squid/errors/$l
  fi
done

%pre
if ! getent group %{name} >/dev/null 2>&1; then
  /usr/sbin/groupadd -g %{gid} %{name}
fi

if ! getent passwd %{name} >/dev/null 2>&1 ; then
  /usr/sbin/useradd -g %{gid} -u %{uid} -d %{_localstatedir}/cache/%{name} -r -s /sbin/nologin %{name} >/dev/null 2>&1 || exit 1 
fi

for i in %{_localstatedir}/log/%{name} %{_localstatedir}/cache/%{name} ; do
        if [ -d $i ] ; then
                for adir in `find $i -maxdepth 0 \! -user %{name}`; do
                        chown -R %{name}:%{name} $adir
                done
        fi
done

exit 0

%post
%if %{with systemd}
%systemd_post %{name}.service
%else
if ! /sbin/chkconfig %{name}; then
  /sbin/chkconfig --add %{name}
fi
%endif

%preun
%if %{with systemd}
%systemd_preun %{name}.service
%else
if [ "$1" -eq "0" -o -x /bin/systemctl ]; then
  %{_initdir}/%{name} stop
  rm -f %{_localstatedir}/log/%{name}/*
  /sbin/chkconfig --del %{name}
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart %{name}.service
%else
if [ "$1" -gt 1 ]; then
  %{_initdir}/%{name} condrestart
fi
%endif

%triggerin -- samba-common
/usr/sbin/usermod -a -G wbpriv %{name} >/dev/null 2>&1 || \
    chgrp %{name} %{_localstatedir}/cache/samba/winbindd_privileged >/dev/null 2>&1 || :


%files
%defattr(-,root,root,-)
# Directory
%dir %{_sysconfdir}/%{name}/
%dir %{_libdir}/%{name}
%dir %{_datadir}/%{name}
%attr(0750,%{name},%{name}) %dir %{_localstatedir}/log/%{name}
%attr(0750,%{name},%{name}) %dir %{_localstatedir}/cache/%{name}
# Program Files
%attr(0755,root,root) %{_sbindir}/%{name}
%attr(0755,root,root) %{_bindir}/*
%attr(0755,root,root) %{_libdir}/%{name}/*
# Config Files
%if %{with systemd}
%attr(0644,root,root) %{_unitdir}/%{name}.service
%dir %{_libexecdir}/%{name}
%attr(0755,root,root) %{_libexecdir}/%{name}/*
%else
%attr(0755,root,root) %{_initdir}/%{name}
%endif
%attr(0640,root,%{name}) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
%attr(0644,root,%{name}) %config(noreplace) %{_sysconfdir}/%{name}/cachemgr.conf
%config(noreplace) %{_sysconfdir}/%{name}/mime.conf
%config(noreplace) %{_sysconfdir}/%{name}/errorpage.css
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/httpd/conf.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
%config(noreplace) %{_sysconfdir}/pam.d/%{name}
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
# Sample Config Files
%config %{_sysconfdir}/%{name}/%{name}.conf.default
%config %{_sysconfdir}/%{name}/cachemgr.conf.default
%config %{_sysconfdir}/%{name}/mime.conf.default
%config %{_sysconfdir}/%{name}/errorpage.css.default
# Data Files
%{_datadir}/%{name}/*
# Document Files
%license COPYING
%doc README ChangeLog QUICKSTART
%doc contrib/url-normalizer.pl contrib/user-agents.pl
%{_mandir}/man8/*
%{_mandir}/man1/*
%{_sysconfdir}/%{name}/%{name}.conf.documented


%changelog
* Wed Oct 06 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.2-1
- new upstream release.
- replaced all patches.
- built with openssl-3.0.0.

* Wed May 12 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.15-1
- new upstream release.

* Tue Mar 02 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.14-1
- new upstream release.

* Fri Sep 04 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.13-1
- new upstream release.

* Fri Jul 10 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.12-1
- new upstream release.

* Thu Apr 30 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.11-1
- new upstream release.
- added systemd support (disabled as default).

* Mon Feb 10 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.10-1
- new upstream release.

* Mon Dec 16 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.9-1
- new upstream release.
- added "-Wno-cpp" to CFLAGS/CXXFLAGS for glibc >= 2.30.

* Thu Aug 29 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.8-1
- new upstream release.

* Tue Nov 20 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.4-1
- new upstream release.
- update patches.

* Tue Jan 09 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5.27-2
- enabled a build option: --enable-internal-dns.

* Wed Sep 06 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5.27-1
- new upstream release.
- updated Patch203.

* Wed Jun 29 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5.19-1
- new upstream release.

* Fri Apr 29 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5.17-1
- new upstream release.

* Tue Apr 12 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5.16-1
- new upstream release.

* Thu Jun 18 2015 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.5.5-1
- new upstream release.

* Mon Jul  7 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.4.6-1
- new upstream release.

* Sat Jan 12 2013 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.23-1
- new upstream release

* Sat Dec 08 2012 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.22-1
- new upstream release

* Thu Nov 22 2012 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.21-1
- new upstream release

* Thu Aug 23 2012 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.20-1
- new upstream release

* Mon Oct 17 2011 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.16-1
- new upstream release
- add BuildRequires - libcap-devel

* Fri Jun 10 2011 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.12-2
- files in libexec moved to libdir

* Tue May 17 2011 Masahiro INOUE <miyabi.-.inoue@nifty.com> 3.1.12-1
- initial build for Vine Linux
- This package is based on Vine Linux - squid-3.0.STABLE25-2
