##
##  pwmd.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 version
%define       V_pwmd     2.18
%define       V_libpwmd  6.0.3

#   package information
Name:         pwmd
Summary:      Password Manager Daemon
URL:          http://bjk.sourceforge.net/pwmd/
Vendor:       Ben Kibbey
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        System
License:      GPL
Version:      %{V_pwmd}.%{V_libpwmd}
Release:      20120713

#   list of sources
Source0:      http://download.sourceforge.net/bjk/pwmd-%{V_pwmd}.tar.gz
Source1:      http://download.sourceforge.net/bjk/libpwmd-%{V_libpwmd}.tar.gz
Source2:      rc.pwmd
Source3:      pwmd.cfg
Patch0:       pwmd.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, pinentry, apg, pkgconfig, automake, autoconf
PreReq:       OpenPKG, openpkg >= 20160101, pinentry
BuildPreReq:  glib, gcrypt, libassuan, gpg-error, libxml, zlib
PreReq:       glib, gcrypt, libassuan, gpg-error, libxml, zlib

%description
    Password Manager Daemon (PWD) is a daemon that serves data to
    application via a socket. The data is stored in an encrypted XML
    file and the client must provide a key when opening and saving a
    file. It uses a shared memory cache for storing a key so keys aren't
    required for each open or save. Also features a configuration file
    to set the number of encryption iterations, logging and more.

%track
    prog pwmd:pwmd = {
        version   = %{V_pwmd}
        url       = http://sourceforge.net/projects/bjk/files/
        regex     = [^b]pwmd-(__VER__)\.tar\.gz
    }
    prog pwmd:libpwmd = {
        version   = %{V_libpwmd}
        url       = http://sourceforge.net/projects/bjk/files/
        regex     = libpwmd-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %patch -p0

%build
    #   build server (daemon)
    ( cd pwmd-%{V_pwmd}
      aclocal
      automake
      autoconf
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --mandir=%{l_prefix}/man \
          --with-libgcrypt-prefix=%{l_prefix} \
          --with-xml-prefix=%{l_prefix} \
          --with-libassuan-prefix=%{l_prefix} \
          --with-gpg-error-prefix=%{l_prefix} \
          --disable-locking \
          --disable-nls
      %{l_make} %{l_mflags}
    ) || exit $?

    #   build client (library)
    ( cd libpwmd-%{V_libpwmd}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --mandir=%{l_prefix}/man \
          --with-libassuan-prefix=%{l_prefix} \
          --with-gpg-error-prefix=%{l_prefix} \
          --with-pinentry \
          --with-pinentry-path=%{l_prefix}/bin/pinentry \
          --disable-ssh \
          --disable-shared \
          --disable-nls
      %{l_make} %{l_mflags}
    ) || exit $?

%install

    #   install server (daemon)
    ( cd pwmd-%{V_pwmd}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   install client (library)
    ( cd libpwmd-%{V_libpwmd}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

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

    #   install server configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/pwmd

    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_password@;`apg -n1 -a0 -m8 -x8`;g" \
        %{SOURCE pwmd.cfg} $RPM_BUILD_ROOT%{l_prefix}/etc/pwmd/

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

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/pwmd \
        $RPM_BUILD_ROOT%{l_prefix}/var/pwmd/pwmd.db

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

%files -f files

%clean

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

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

