##
##  confd.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_opkg  0.16.0
%define       V_dist  20210103

#   package information
Name:         confd
Summary:      Configuration Management Server
URL:          http://www.confd.io/
Vendor:       Kelsey Hightower
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      MIT
Version:      %{V_opkg}.%{V_dist}
Release:      20210103

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/confd/confd-%{V_dist}.tar.xz
Source1:      confd.toml
Source2:      rc.confd
Source3:      example.toml
Source4:      example.conf.tmpl

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

%description
    ConfD is a lightweight configuration management tool focusing on:
    keeping local configuration files up-to-date by polling EtcD, Consul
    or Vault and processing template resources; reloading applications
    to pick up new config file changes.

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

%prep
    %setup -q -n confd

%build
    #   patch path to configuration file
    %{l_shtool} subst \
        -e 's;/etc/confd/confd.toml;%{l_prefix}/etc/confd/confd.toml;' \
        src/github.com/kelseyhightower/confd/config.go

    #   build program
    export GOPATH=`pwd`
    ( cd src/github.com/kelseyhightower/confd
      mkdir bin
      go build -v -o bin/confd .
    ) || exit $?

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

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

    #   install configuration files
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE confd.toml} $RPM_BUILD_ROOT%{l_prefix}/etc/confd/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE example.toml} $RPM_BUILD_ROOT%{l_prefix}/etc/confd/conf.d/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE example.conf.tmpl} $RPM_BUILD_ROOT%{l_prefix}/etc/confd/templates/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.confd} $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/confd/conf.d/*' \
        '%config %{l_prefix}/etc/confd/templates/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/confd/*'

%files -f files

%clean

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

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

