##
##  pks.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:         pks
Summary:      OpenPGP Key Server
URL:          http://pks.sourceforge.net/
Vendor:       Marc Horowitz et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        PGP
License:      GPL
Version:      0.9.6
Release:      20080101

#   package options
%option       with_fsl  yes

#   list of sources
Source0:      http://download.sourceforge.net/pks/pks-%{version}.tar.gz
Source1:      fsl.pks
Source2:      rc.pks
Source3:      pksd.conf
Source4:      pks-mail.c
Source5:      pks.html
Source6:      pks-apache.conf
Patch0:       pks.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101, MTA
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    PKS is an OpenPGP (RFC 2440) compliant Public Key server, originally
    developed by Marc Horowitz for http://pgp.mit.edu/.

%track
    prog pks = {
        version   = %{version}
        url       = http://sourceforge.net/projects/pks/files/
        regex     = pks-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure and build programs
    echo "ac_cv_path_SENDMAIL=%{l_prefix}/sbin/sendmail" >config.cache
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_fsl_ldflags}" \
    LIBS="%{l_fsl_ldflags} %{l_fsl_libs}" \
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/pks \
        --localstatedir=%{l_prefix}/var/pks \
        --datadir=%{l_prefix}/share/pks
    %{l_make} %{l_mflags}

    #   build suid wrapper for pks-mail
    %{l_cc} %{l_cflags} %{l_ldflags} '-DL_PREFIX="%{l_prefix}"' \
        -o pks-mail %{SOURCE pks-mail.c}

%install

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

    #   strip installation files and adjust paths
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include/db2
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/db2
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/db_*
    rm -f $RPM_BUILD_ROOT%{l_prefix}/var/pks/index.html
    mv  $RPM_BUILD_ROOT%{l_prefix}/bin/pks-queue-run.sh \
        $RPM_BUILD_ROOT%{l_prefix}/bin/pks-queue-run
    mv  $RPM_BUILD_ROOT%{l_prefix}/var/pks/incoming \
        $RPM_BUILD_ROOT%{l_prefix}/var/pks/queue
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   install HTML top-level page
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/pub/pks
    %{l_shtool} install -c -m 644 \
        %{SOURCE pks.html} $RPM_BUILD_ROOT%{l_prefix}/pub/pks/index.html

    #   install suid wrapper for pks-mail
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/pks
    mv  $RPM_BUILD_ROOT%{l_prefix}/bin/pks-mail.sh \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/pks/pks-mail
    %{l_shtool} install -c -s -m 755 \
        pks-mail $RPM_BUILD_ROOT%{l_prefix}/bin/

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

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

    #   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.pks} \
        $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/fsl/fsl.pks' \
        '%config %{l_prefix}/etc/pks/pksd.conf' \
        '%attr(4755,%{l_rusr},%{l_mgrp}) %{l_prefix}/bin/pks-mail' \
        '%dir %attr(755,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/pks' \
        '%dir %attr(755,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/pks/db' \
        '%dir %attr(755,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/pks/queue'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} pks status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} pks stop 2>/dev/null
    exit 0

%post
    if [ $1 -eq 1 ]; then
        #   create initial database
        su - %{l_rusr} -c "$RPM_INSTALL_PREFIX/bin/pksclient \
            $RPM_INSTALL_PREFIX/var/pks/db create" >/dev/null 2>&1
        for key in $RPM_INSTALL_PREFIX/etc/openpkg/*.pgp; do \
            su - %{l_rusr} -c "$RPM_INSTALL_PREFIX/bin/pksclient \
                $RPM_INSTALL_PREFIX/var/pks/db add $key" >/dev/null 2>&1
        done

        #   activate in MTA configuration
        aliases_file=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_file`
        aliases_update=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_update`
        if [ ".$aliases_file" != . ]; then
            ( echo "pks:             pks-request"
              echo "pks-request:     \"|$RPM_INSTALL_PREFIX/bin/pks-mail $RPM_INSTALL_PREFIX/etc/pks/pksd.conf\""
              echo "pks-daemon:      MAILER-DAEMON"
              echo "pks-admin:       postmaster"
              echo "pgp-public-keys: pks-request"
              echo "pgp:             pks-request"
            ) | %{l_rpmtool} config -a -i "$RPM_INSTALL_PREFIX:pks" $aliases_file
        fi
        if [ ".$aliases_update" != . ]; then
            eval $aliases_update
        fi
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}
        [ ".$pks_active" = .yes ] && %{l_rc} pks start
    fi
    exit 0

%preun
    #   before erase, stop service
    [ $1 -eq 0 ] || exit 0
    %{l_rc} pks stop 2>/dev/null

    #   deactivate in MTA configuration
    aliases_file=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_file`
    aliases_update=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_update`
    if [ ".$aliases_file" != . ]; then
        %{l_rpmtool} config -r -i "$RPM_INSTALL_PREFIX:pks" $aliases_file
    fi
    if [ ".$aliases_update" != . ]; then
        eval $aliases_update
    fi

    #   remove generated files
    rm -f $RPM_INSTALL_PREFIX/var/pks/pksd.ctl >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/pks/pks.log* >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/pks/pks-queue-run.pid >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/pks/queue/* >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/pks/db/* >/dev/null 2>&1 || true
    exit 0

