##
##  nuster.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  5.3.0.23
%define       V_dist  5.3.0.23
%define       V_major 5.3

#   package information
Name:         nuster
Summary:      Web-Caching Proxy Based on HAProxy
URL:          https://github.com/jiangwenyuan/nuster
Vendor:       Jiang Wenyuan
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Networking
License:      GPL/LGPL
Version:      %{V_opkg}
Release:      20201221

#   package options
%option       with_fsl  yes
%option       with_ssl  yes
%option       with_zlib yes
%option       with_lua  yes
%option       with_pcre yes

#   list of sources
Source0:      https://github.com/jiangwenyuan/nuster/archive/v%{V_dist}.tar.gz
Source1:      nuster.cfg
Source2:      fsl.nuster
Source3:      rc.nuster
Patch0:       nuster.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif
%if "%{with_zlib}" == "yes"
BuildPreReq:  zlib
PreReq:       zlib
%endif
%if "%{with_lua}" == "yes"
BuildPreReq:  lua
PreReq:       lua
%endif
%if "%{with_pcre}" == "yes"
BuildPreReq:  pcre
PreReq:       pcre
%endif

%description
    HAProxy is a free, very fast and reliable solution offering high
    availability, load balancing, and proxying for TCP and HTTP-based
    applications. It is particularly suited for web sites crawling under
    very high loads while needing persistence or Layer7 processing.
    Supporting tens of thousands of connections is clearly realistic
    with todays hardware.

%track
    prog nuster = {
        version   = %{V_dist}
        url       = https://github.com/jiangwenyuan/nuster/releases
        regex     = (__VER__)\.tar\.gz
    }

%prep
    %setup -q -n nuster-%{V_dist}
    %patch -p0

%build
    #   build program
    target="generic"
    case "%{l_platform -t}" in
        *-freebsd* ) target="freebsd"   ;;
        *-linux*   ) target="linux2628" ;;
        *-sunos*   ) target="solaris"   ;;
    esac
    %{l_make} %{l_mflags} \
        TARGET=$target \
        CC="%{l_cc}" \
        CFLAGS="%{l_cflags -O}" \
        ADDINC="%{l_cppflags}" \
        LDFLAGS="%{l_ldflags}" \
        ADDLIB="%{l_fsl_ldflags} %{l_fsl_libs}" \
%if "%{with_ssl}" == "yes"
        USE_OPENSSL=1 \
        SSL_INC=%{l_prefix}/include \
        SSL_LIB=%{l_prefix}/lib \
%endif
%if "%{with_zlib}" == "yes"
        USE_ZLIB=1 \
        ZLIB_INC=%{l_prefix}/include \
        ZLIB_LIB=%{l_prefix}/lib \
%endif
%if "%{with_lua}" == "yes"
        USE_LUA=1 \
        LUA_INC=%{l_prefix}/include/lua \
        LUA_LIB=%{l_prefix}/lib \
%endif
%if "%{with_pcre}" == "yes"
        USE_PCRE=1 \
        PCRE_INC=%{l_prefix}/include \
        PCRE_LIB=%{l_prefix}/lib \
        USE_STATIC_PCRE=1
%endif

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/man/cat1 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/nuster \
        $RPM_BUILD_ROOT%{l_prefix}/var/nuster

    #   install program
    %{l_shtool} install -c -s -m 755 \
        haproxy $RPM_BUILD_ROOT%{l_prefix}/sbin/nuster
    %{l_shtool} install -c -m 644 \
        doc/configuration.txt $RPM_BUILD_ROOT%{l_prefix}/man/cat1/nuster.1

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE nuster.cfg} $RPM_BUILD_ROOT%{l_prefix}/etc/nuster/

    #   install run-command script
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE rc.nuster} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install OSSP fsl configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.nuster} $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

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

%files -f files

%clean

