##
##  caddy.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
%define       V_caddy_opkg  1.0.0b1
%define       V_caddy_base  1.0.0-beta1
%define       V_caddy_snap  20190410

#   package information
Name:         caddy
Summary:      HTTP Web Server
URL:          http://caddyserver.com/
Vendor:       Matt Holt
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      Apache
Version:      %{V_caddy_opkg}.%{V_caddy_snap}
Release:      20190410

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/caddy/caddy-%{V_caddy_snap}.tar.xz
Source1:      rc.caddy
Source2:      Caddyfile

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Caddy is a web-server with the following features: Easy
    configuration with Caddyfile; Automatic HTTPS via Let's Encrypt;
    Caddy obtains and manages all cryptographic assets for you; HTTP/2
    enabled by default; Virtual hosting for hundreds of sites per server
    instance, including TLS SNI; Experimental QUIC support for those
    that like speed; TLS session ticket key rotation for more secure
    connections; Brilliant extensibility so Caddy can be customized for
    your needs.

%track
    prog caddy = {
        version   = %{V_caddy_base}
        url       = https://github.com/mholt/caddy/releases
        regex     = v(\d+\.\d+(?:\.\d+)?(?:-beta\d+)?)\.tar\.gz
    }

%prep
    %setup -q -n caddy

%build
    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/mholt/caddy
        rm -f sigtrap_windows.go
        go build -x -o caddy/caddy caddy/main.go
    ) || exit $?

%install
    #   create directory hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/caddy \
        $RPM_BUILD_ROOT%{l_prefix}/var/caddy/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/caddy/log

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/mholt/caddy/caddy/caddy \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} caddy status 2>/dev/null`
        [ ".$caddy_active" = .yes ] && %{l_rc} caddy restart
    fi
    exit 0

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

