##
##  varnish.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_opkg 6.0.1
%define       V_dist 6.0.1

#   package information
Name:         varnish
Summary:      High-Performance HTTP Accelerator
URL:          http://varnish-cache.org/
Vendor:       Poul-Henning Kamp et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Web
License:      BSD
Version:      %{V_opkg}
Release:      20180831

#   list of sources
Source0:      http://varnish-cache.org/_downloads/varnish-%{V_dist}.tgz
Source1:      rc.varnish
Source2:      varnish.vcl
Patch0:       varnish.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, make, pkgconfig, docutils
PreReq:       OpenPKG, openpkg >= 20160101, gcc
BuildPreReq:  ncurses, pcre
PreReq:       ncurses, pcre

%description
    Varnish was written from the ground up to be a high performance
    HTTP caching reverse proxy. The configuration is specified using
    a domain-specific language called VCL, the Varnish Configuration
    Language. The varnishd(1) management process translates the VCL code
    to C and compiles it to a shared object, which is then dynamically
    linked into the server process.

    Varnish is targeted primarily at the FreeBSD 6 and Linux 2.6
    platforms, and will take full advantage of the advanced I/O features
    offered by these operating systems.

%track
    prog varnish = {
        version   = %{V_dist}
        url       = https://varnish-cache.org/releases
        regex     = varnish-(\d+\.\d+\.\d+)\.tgz
    }

%prep
    #   platform checks
    case "%{l_platform -t}" in
        *-sunos5.1[01]* ) ;;
        *-freebsd* ) ;;
        *-linux2.6* | *-linux3.* ) ;;
        * )
            set +x
            echo "Platform \"%{l_platform -t}\" not supported." |\
                %{l_rpmtool} msg -b -t error
            exit 1
            ;;
    esac

    #   unpack and patch Varnish distribution
    %setup -q -n varnish-%{V_dist}
    %patch -p0

%build
    #   ensure OpenPKG %{l_cc} is being used for VCL code generation
    %{l_shtool} subst \
        -e 's;exec cc;exec %{l_cc};g' \
        configure

    #   configure package
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O}"
    export CPPFLAGS="%{l_cppflags ncurses .}"
    export LDFLAGS="%{l_ldflags}"
    export LIBS="-lexecinfo -lm"
    case "%{l_platform -t}" in
        *-sunos* ) LIBS="$LIBS -lsocket -lnsl" ;;
    esac
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man

    #   build package
    %{l_make} %{l_mflags}

%install

    #   install package
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/varnish

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/varnish
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE varnish.vcl} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/varnish/

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.varnish} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   strip installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/varnish/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/varnish'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} varnish status 2>/dev/null`
    [ ".$varnish_active" = .yes ] && %{l_rc} varnish restart
    exit 0

%preun
    #   before erase, stop service
    [ $1 -eq 0 ] || exit 0
    %{l_rc} varnish stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/varnish/* >/dev/null 2>&1 || true
    exit 0

