##
##  fossil.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   2.10
%define       V_dist   2.10
%define       V_subdir 2.10

#   package information
Name:         fossil
Summary:      Distributed Version Control System
URL:          http://www.fossil-scm.org/
Vendor:       D. Richard Hipp
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        SCM
License:      GPL
Version:      %{V_opkg}
Release:      20191228

#   list of sources
Source0:      http://www.fossil-scm.org/fossil/uv/fossil-src-%{V_dist}.tar.gz
Source1:      fossil.cgi

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

%description
    Fossil is a distributed software revision control system that
    includes an integrated Wiki and an integrated Bug-Tracking system,
    all in a single, easy-to-use, stand-alone executable. It stores its
    repositories in a single SQLite database.

%track
    prog fossil = {
        version   = %{V_dist}
        url       = http://www.fossil-scm.org/fossil/uv/download.html
        regex     = fossil-src-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n fossil-%{V_subdir}

%build
    echo "D 2012-07-22 00:00:00" >manifest
    echo "0000000000" >manifest.uuid
    libs="-lz -lssl -lcrypto"
    case "%{l_platform -t}" in
        *-sunos* ) libs="$libs -lrt -lsocket -lnsl" ;;
    esac
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags} -Dpqueue_insert=pqueue_insert_fossil" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="$libs" \
    ./configure \
        --prefix=%{l_prefix}
    %{l_make} %{l_mflags}

%install
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/cgi \
        $RPM_BUILD_ROOT%{l_prefix}/var/fossil
    %{l_shtool} install -c -s -m 755 \
        fossil $RPM_BUILD_ROOT%{l_prefix}/bin/
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE fossil.cgi} $RPM_BUILD_ROOT%{l_prefix}/cgi/fossil
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%dir %attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/fossil'

%files -f files

%clean

%post
    if [ ".$1" = .1 ]; then
        su - %{l_nusr} -c \
            "LC_CTYPE=C; export LC_CTYPE; umask 077; \
            rm -f $RPM_INSTALL_PREFIX/var/fossil/fossil.db; \
            $RPM_INSTALL_PREFIX/bin/fossil new \
                $RPM_INSTALL_PREFIX/var/fossil/fossil.db" 2>&1
        ( echo "The Fossil repository can be reached under the URL"
          echo "  http://localhost/openpkg-cgi/fossil"
          echo "In order to operate with it you have to create users in it."
          echo "For this either use the Web interface under Setup->User or"
          echo "configure the repository from the command-line:"
          echo "  $RPM_INSTALL_PREFIX/bin/fossil user new setup"
          echo "  $RPM_INSTALL_PREFIX/bin/fossil user capabilities setup 's'"
          echo "  $RPM_INSTALL_PREFIX/bin/fossil user default setup"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        rm -f $RPM_INSTALL_PREFIX/var/fossil/* >/dev/null 2>&1 || true
    fi
    exit 0

