##
##  delegate.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:         delegate
Summary:      Multi-purpose Application-Level Network Gateway
URL:          http://www.delegate.org/
Vendor:       Yutaka Sato
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      Freely distributable via publicly accessible on-line media
Version:      9.9.7
Release:      20100401

#   list of sources
Source0:      http://www.delegate.org/anonftp/DeleGate/delegate%{version}.tar.gz
Source1:      rc.delegate
Source2:      delegated.cf
Patch0:       delegate.patch

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

%description
    DeleGate is a multi-purpose application level gateway, or a proxy
    server. It mediates communication of various protocols (HTTP, FTP,
    NNTP, POP, Telnet, etc.), applying cache and conversion for mediated
    data, controlling access from clients and routing toward servers. It
    translates protocols between clients and servers, merging several
    servers into a single server view with aliasing and filtering.
    Besides proxy, DeleGate can be used as a simple origin server for
    some protocols (HTTP, FTP and NNTP).

%track
    prog delegate = {
        version   = %{version}
        url       = http://www.delegate.org/anonftp/DeleGate/
        regex     = delegate(\d+\.\d+\.\d+)\.tar\.(gz|bz2)
    }

%prep
    %setup -q -n delegate%{version}
    %patch -p0
    %{l_shtool} subst \
        -e 's;^\(const char \*DELEGATE_OWNER *=\) *.*$;\1 "%{l_rusr}/%{l_rgrp}"\;;' \
        -e 's;^\(const char \*DELEGATE_ETCDIR *=\) *.*$;\1 "%{l_prefix}/etc/delegate"\;;' \
        -e 's;^\(const char \*DELEGATE_CONFIG *=\) *.*$;\1 "%{l_prefix}/etc/delegate/delegated.cf"\;;' \
        -e 's;^\(const char \*DELEGATE_DGROOT *=\) *.*$;\1 "%{l_prefix}/var/delegate"\;;' \
        -e 's;^\(const char \*DELEGATE_VARDIR *=\) *.*$;\1 "%{l_prefix}/var/delegate"\;;' \
        -e 's;^\(const char \*DELEGATE_ACTDIR *=\) *.*$;\1 "%{l_prefix}/var/delegate/act"\;;' \
        -e 's;^\(const char \*DELEGATE_PIDFILE *=\) *.*$;\1 "%{l_prefix}/var/delegate/delegate.pid"\;;' \
        -e 's;^\(const char \*DELEGATE_LOGDIR *=\) *.*$;\1 "%{l_prefix}/var/delegate/log"\;;' \
        -e 's;^\(const char \*DELEGATE_ABORTLOG *=\) *.*$;\1 "%{l_prefix}/var/delegate/log/abort.log"\;;' \
        -e 's;^\(const char \*DELEGATE_LOGFILE *=\) *.*$;\1 "%{l_prefix}/var/delegate/log/common.log"\;;' \
        -e 's;^\(const char \*DELEGATE_ERRORLOG *=\) *.*$;\1 "%{l_prefix}/var/delegate/log/error.log"\;;' \
        -e 's;^\(const char \*DELEGATE_PROTOLOG *=\) *.*$;\1 "%{l_prefix}/var/delegate/log/proto.log"\;;' \
        -e 's;^\(const char \*DELEGATE_TRACELOG *=\) *.*$;\1 "%{l_prefix}/var/delegate/log/trace.log"\;;' \
        -e 's;^\(const char \*DELEGATE_WORKDIR *=\) *.*$;\1 "%{l_prefix}/var/delegate/work"\;;' \
        -e 's;^\(const char \*DELEGATE_TMPDIR *=\) *.*$;\1 "%{l_prefix}/var/delegate/tmp"\;;' \
        src/conf.c

%build
    ( %{l_shtool} echo -e "ADMIN = %u@%h%d"
      echo "CC = %{l_cc}"
      echo "CFLAGS = %{l_cflags -O}"
    ) >src/DELEGATE_CONF
    %{l_make} %{l_mflags -O}

%install

    #   install DeleGate files
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/delegate \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/share/delegate/docs
    %{l_shtool} mkdir -f -p -m 700 \
        $RPM_BUILD_ROOT%{l_prefix}/var/delegate/act \
        $RPM_BUILD_ROOT%{l_prefix}/var/delegate/adm \
        $RPM_BUILD_ROOT%{l_prefix}/var/delegate/cache \
        $RPM_BUILD_ROOT%{l_prefix}/var/delegate/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/delegate/work \
        $RPM_BUILD_ROOT%{l_prefix}/var/delegate/tmp
    %{l_shtool} install -c -m 755 \
        src/delegated $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 644 \
        doc/Manual.htm $RPM_BUILD_ROOT%{l_prefix}/share/delegate/docs/manual.html

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

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

    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std} \
        '%config                         %{l_prefix}/etc/delegate/*' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate/act' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate/adm' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate/cache' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate/log' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate/work' \
        '%attr(0700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/delegate/tmp'

%files -f files

%clean

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

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

