##
##  dkron.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_dkron_base 4.0.7
%define       V_dkron_snap 20250924

#   package information
Name:         dkron
Summary:      Distributed Cron Service
URL:          http://dkron.io/
Vendor:       Victor Castell
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      LGPL
Version:      %{V_dkron_base}.%{V_dkron_snap}
Release:      20250924

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/dkron/dkron-%{V_dkron_snap}.tar.xz
Source1:      rc.dkron
Source2:      dkron.json
Patch0:       dkron.patch

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

%description
    Dkron is a system service that runs scheduled jobs at given
    intervals or times, just like the cron unix service but distributed
    in several machines in a cluster. If a machine fails (the leader), a
    follower will take over and keep running the scheduled jobs without
    human intervention. Dcron is based on EtcD or Consul for backend
    storage.

%track
    prog dkron:base = {
        version   = %{V_dkron_base}
        url       = https://github.com/victorcoder/dkron/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }
    prog dkron:snap = {
        version   = %{V_dkron_snap}
        url       = http://download.openpkg.org/components/versioned/dkron/
        regex     = dkron-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n dkron
    %patch -p0

%build
    #   patch paths
    %{l_shtool} subst %{l_value -s -a} \
        src/github.com/victorcoder/dkron/cmd/dkron.go

    #   build program
    export GOPATH=`pwd`
    export GO111MODULE=on
    export GOCACHE=`pwd`
    cd src/github.com/victorcoder/dkron
    go build -v -o dkron.exe main.go

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

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/victorcoder/dkron/dkron.exe \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/dkron

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

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

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

%files -f files

%clean

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

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

