##
##  chrony.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:         chrony
Summary:      Network Time Protocol (NTP) Server
URL:          https://chrony-project.org/
Vendor:       Richard P. Curnow, Miroslav Lichva
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      GPL
Version:      4.8
Release:      20250828

#   list of sources
Source0:      https://chrony-project.org/releases/chrony-%{version}.tar.gz
Source1:      rc.chrony
Source2:      chrony.conf

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

%description
    Chrony is a versatile implementation of the Network Time Protocol
    (NTP). It can synchronise the system clock with NTP servers,
    reference clocks (e.g. GPS receiver), and manual input using
    wristwatch and keyboard. It can also operate as an NTPv4 (RFC 5905)
    server and peer to provide a time service to other computers in the
    network.

%track
    prog chrony = {
        version   = %{version}
        url       = https://chrony-project.org/download.html
        regex     = chrony-(\d+\.\d+(?:\.\d+)?)\.tar\.gz
    }

%prep
    %setup -q

%build
    #   configure program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc/chrony \
        --localstatedir=%{l_prefix}/var/chrony \
        --chronyrundir=%{l_prefix}/var/chrony/run \
        --chronyvardir=%{l_prefix}/var/chrony/run \
        --with-pidfile=%{l_prefix}/var/chrony/run/chrony.pid \
        --with-sendmail=%{l_prefix}/sbin/sendmail \
        --enable-readline \
        --with-readline \
        --without-editline \
        --with-readline-includes=%{l_prefix}/include \
        --with-readline-library=%{l_prefix}/lib \
        --with-ncurses-library=%{l_prefix}/lib

    #   build program
    %{l_make} %{l_mflags -O}

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

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

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/chrony
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE chrony.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/chrony/

    #   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.chrony} $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/chrony/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/chrony/log

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/chrony/chrony.conf' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/chrony/*'

%files -f files

%clean

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

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

