##
##  lighttpd.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 information
Name:         lighttpd
Summary:      Flexible High-Performance Web Server
URL:          http://www.lighttpd.net/
Vendor:       Jan Kneschke
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Web
License:      MIT-style
Version:      1.4.71
Release:      20230528

#   package options
%option       with_fsl    yes
%option       with_pcre   yes
%option       with_bzip2  no
%option       with_ipv6   no
%option       with_mysql  no
%option       with_ldap   no
%option       with_dav    no
%option       with_ssl    no
%option       with_lua    no

#   list of sources
Source0:      http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-%{version}.tar.gz
Source1:      lighttpd.conf
Source2:      rc.lighttpd
Source3:      fsl.lighttpd

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  zlib
PreReq:       zlib
%if "%{with_bzip2}" == "yes"
BuildPreReq:  bzip2
PreReq:       bzip2
%endif
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql, zlib
PreReq:       mysql, zlib
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap, openssl
PreReq:       openldap, openssl
%endif
%if "%{with_pcre}" == "yes"
BuildPreReq:  pcre
PreReq:       pcre
%endif
%if "%{with_dav}" == "yes"
BuildPreReq:  libxml, sqlite
PreReq:       libxml, sqlite
%endif
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif
%if "%{with_lua}" == "yes"
BuildPreReq:  lua
PreReq:       lua
%endif

%description
    lighttpd is a secure, fast, compliant and very flexible web-server
    which has been optimized for high-performance environments. It has
    a very low memory footprint compared to other webservers and takes
    care of cpu-load. Its advanced feature-set (FastCGI, CGI, Auth,
    output compression, URL-rewriting and many more) make lighttpd the
    perfect web-server software for every server that is suffering load
    problems. As lighttpd is hard to pronounce you might call it lighty
    instead.

%track
    prog lighttpd = {
        version   = %{version}
        url       = http://www.lighttpd.net/download/
        regex     = lighttpd-(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q
    %{l_shtool} subst \
        -e 's;\(/etc/lighttpd/lighttpd.conf\);%{l_prefix}\1;' \
        doc/lighttpd.1

%build
    #   configure program
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O}"
%if "%{with_lua}" == "yes"
    export CPPFLAGS="%{l_cppflags lua .}"
%else
    export CPPFLAGS="%{l_cppflags}"
%endif
    export CXXFLAGS="%{l_cxxflags -O}"
%if "%{with_lua}" == "yes"
    export LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}"
%else
    export LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}"
%endif
    export LIBS="%{l_fsl_libs}"
%if "%{with_ldap}" == "yes"
    LIBS="$LIBS -lldap -llber -lssl -lcrypto"
%endif
    ./configure \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/lighttpd \
        --libdir=%{l_prefix}/lib/lighttpd \
%if "%{with_ipv6}" == "yes"
        --enable-ipv6 \
%else
        --disable-ipv6 \
%endif
%if "%{with_ldap}" == "yes"
        --with-ldap \
%else
        --without-ldap \
%endif
%if "%{with_mysql}" == "yes"
        --with-mysql=%{l_prefix}/bin/mysql_config \
%else
        --without-mysql \
%endif
%if "%{with_dav}" == "yes"
        --with-webdav-props \
        --without-webdav-locks \
%endif
%if "%{with_pcre}" == "yes"
        --with-pcre \
%else
        --without-pcre \
%endif
%if "%{with_bzip2}" == "yes"
        --with-bzip2 \
%else
        --without-bzip2 \
%endif
%if "%{with_lua}" == "yes"
        --with-lua \
%else
        --without-lua \
%endif
%if "%{with_ssl}" == "yes"
        --with-openssl=%{l_prefix}
%else
        --without-openssl
%endif

    #   build program
    %{l_make} %{l_mflags -O}

%install

    #   create directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/lighttpd \
        $RPM_BUILD_ROOT%{l_prefix}/var/lighttpd/log

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

    #   install default configuration
    %{l_shtool} install -c -m 640 %{l_value -s -a} \
        -e 's;@l_openpkg_release@;%{l_openpkg_release -F "OpenPKG-%%t"};' \
        %{SOURCE lighttpd.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/lighttpd/

    #   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.lighttpd} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std} \
        '%config   %{l_prefix}/etc/fsl/fsl.lighttpd' \
        '%config %attr(0600,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/lighttpd/*' \
        '%dir    %attr(0775,%{l_mgrp},%{l_ngrp}) %{l_prefix}/var/lighttpd/log'

%files -f files

%clean

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

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

