##
##  gitea.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:         gitea
Summary:      Git Hosting Service
URL:          http://gitea.io/
Vendor:       Gitea Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      MIT
Version:      1.24.6
Release:      20250911

#   list of sources
Source0:      https://github.com/go-gitea/gitea/releases/download/v%{version}/gitea-src-%{version}.tar.gz
Source1:      rc.gitea
Source2:      gitea-serv.c
Source3:      gitea-update.sh
Source4:      app.ini

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

%description
    Gitea is a hosting service for the Git version control system.

%track
    prog gitea = {
        version   = %{version}
        url       = https://github.com/go-gitea/gitea/releases
        regex     = tree/v(\d+\.\d+\.\d+)
    }

%prep
    %setup -q -c -n gitea

%build
    #   build program
    (   cd gitea-src-*
        export HOME=`pwd`
        echo "%{version}" >VERSION
        STORED_VERSION=1 \
        TAGS="bindata sqlite sqlite_unlock_notify" \
        %{l_make} %{l_mflags} build
    ) || exit $?

    #   build the setuid wrapper
    cp %{SOURCE gitea-serv.c} .
    %{l_shtool} subst %{l_value -s -a} gitea-serv.c
    %{l_cc} %{l_cflags} %{l_ldflags} -o gitea-serv gitea-serv.c

    #   remove spaces from filenames
    (   cd src/github.com/go-gitea/gitea/options/license
        rm -f "Updating the SPDX Licenses"
    ) || exit $?

%install
    #   create directory hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/gitea \
        $RPM_BUILD_ROOT%{l_prefix}/share/gitea/conf \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/home \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/repo \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitea/lfs

    #   install program
    %{l_shtool} install -c -s -m 755 \
        gitea-src-*/gitea $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install setuid wrapper script
    %{l_shtool} install -c -s -m 755 \
        gitea-serv $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install run-time files
    cp -r gitea-src-*/assets \
        gitea-src-*/options \
        gitea-src-*/public \
        gitea-src-*/templates \
        $RPM_BUILD_ROOT%{l_prefix}/share/gitea/

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

    #   provide links to final location
    ln -s ../../../etc/gitea/app.ini \
        $RPM_BUILD_ROOT%{l_prefix}/share/gitea/conf/app.ini
    ln -s ../../var/gitea/data \
        $RPM_BUILD_ROOT%{l_prefix}/share/gitea/data
    ln -s ../../sbin/gitea \
        $RPM_BUILD_ROOT%{l_prefix}/share/gitea/gitea

    #   install "gitea update" wrapper
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE gitea-update.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/gitea-update

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

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

%files -f files

%clean

%post
    #   after upgrade, restart service
    if [ $1 -eq 1 ]; then
        #   display information about next steps
        (   echo "After starting Gitea with"
            echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc gitea start"
            echo "you should configure it by visiting:"
            echo "    http://localhost:3000/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        eval `%{l_rc} gitea status 2>/dev/null`
        [ ".$gitea_active" = .yes ] && %{l_rc} gitea restart
    fi
    exit 0

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

