##
##  h2o.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:         h2o
Summary:      Optimized HTTP/1 and HTTP/2 Server
URL:          https://h2o.examp1e.net/
Vendor:       DeNA Co., Ltd.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      MIT
Version:      2.0.4
Release:      20160915

#   list of sources
Source0:      https://github.com/h2o/h2o/archive/v%{version}.tar.gz
Source1:      h2o.conf
Source2:      rc.h2o

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, pkgconfig, bison
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  wslay, zlib, libuv, openssl, ruby
PreReq:       wslay, zlib, libuv, openssl, ruby

%description
    H2O is a new generation HTTP server providing quicker response
    to users when compared to older generation of web servers. The
    server takes full advantage of HTTP/2 features including prioritized
    content serving and server push, promising outstanding experience to
    the visitors of your web site.

%track
    prog h2o = {
        version   = %{version}
        url       = https://github.com/h2o/h2o/releases
        regex     = v(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q

%build
    DESTDIR=$RPM_BUILD_ROOT%{l_prefix} \
    cmake \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags} %{l_cppflags}" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags}" \
        -DWITH_BUNDLED_SSL="OFF" \
        -DOPENSSL_INCLUDE_DIR="%{l_prefix}/include" \
        -DOPENSSL_CRYPTO_LIBRARY="%{l_prefix}/lib/libcrypto.a" \
        -DOPENSSL_SSL_LIBRARY="%{l_prefix}/lib/libssl.a" \
        -DLIBUV_INCLUDE_DIRS="%{l_prefix}/include" \
        -DLIBUV_LIBRARY_DIRS="%{l_prefix}/lib" \
        -DWSLAY_INCLUDE_DIRS="%{l_prefix}/include" \
        -DWSLAY_LIBRARY_DIRS="%{l_prefix}/lib" \
        -DZLIB_INCLUDE_DIRS="%{l_prefix}/include" \
        -DZLIB_LIBRARY_DIRS="%{l_prefix}/lib" \
        -DZLIB_LIBRARY="%{l_prefix}/lib/libz.a" \
        .
    %{l_make} %{l_mflags -O}

%install
    #   install program
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

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

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/h2o \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/var/h2o/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/h2o/log

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

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

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

%files -f files

%clean

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

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

