##
##  gogs.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
%define       V_gogs_base  0.11.91
%define       V_gogs_snap  20191004

#   package information
Name:         gogs
Summary:      Git Hosting Service
URL:          http://gogs.io/
Vendor:       GoGits
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      MIT
Version:      %{V_gogs_base}.%{V_gogs_snap}
Release:      20191004

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/gogs/gogs-%{V_gogs_snap}.tar.xz
Source1:      rc.gogs
Source2:      gogs-serv.c
Source3:      gogs-update.sh
Source4:      app.ini
Patch0:       gogs.patch

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

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

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

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

%build
    #   patch sources
    %{l_shtool} subst %{l_value -s -a} \
        src/github.com/gogs/gogs/models/ssh_key.go

    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/gogs/gogs
        go build -tags "sqlite" -v -o gogs
    ) || exit $?

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

    #   remove spaces from filenames
    (   cd src/github.com/gogs/gogs/conf/license
        mv "Affero General Public License v1.0"      AGPL-1.0
        mv "Apache License 1.0"                      Apache-1.0
        mv "Apache License 1.1"                      Apache-1.1
        mv "Apache License 2.0"                      Apache-2.0
        mv "Artistic License 1.0"                    Artistic-1.0
        mv "Artistic License 2.0"                    Artistic-2.0
        mv "BSD 2-clause License"                    BSD-2C
        mv "BSD 3-clause License"                    BSD-3C
        mv "BSD 4-clause License"                    BSD-4C
        mv "BSD license"                             BSD
        mv "Creative Commons CC0 1.0 Universal"      CC0-1.0
        mv "Eclipse Public License 1.0"              EPL-1.0
        mv "GNU Affero General Public License v3.0"  AGPL-3.0
        mv "GNU Free Documentation License v1.1"     FDL-1.1
        mv "GNU Free Documentation License v1.2"     FDL-1.2
        mv "GNU Free Documentation License v1.3"     FDL-1.3
        mv "GNU General Public License v1.0"         GPL-1.0
        mv "GNU General Public License v2.0"         GPL-2.0
        mv "GNU General Public License v3.0"         GPL-3.0
        mv "GNU Lesser General Public License v2.1"  LGPL-2.1
        mv "GNU Lesser General Public License v3.0"  LGPL-3.0
        mv "GNU Library General Public License v2.0" LGPL-2.0
        mv "ISC license"                             ISC
        mv "MIT License"                             MIT
        mv "Mozilla Public License 1.0"              MPL-1.0
        mv "Mozilla Public License 1.1"              MPL-1.1
        mv "Mozilla Public License 2.0"              MPL-2.0
        rm -f "Abstyles License"
        rm -f "Academic Free License v1.1"
        rm -f "Academic Free License v1.2"
        rm -f "Academic Free License v2.0"
        rm -f "Academic Free License v2.1"
        rm -f "Academic Free License v3.0"
        rm -f "Creative Commons Zero v1.0 Universal"
        rm -f "Educational Community License v1.0"
        rm -f "Educational Community License v2.0"
    ) || exit $?
    (   cd src/github.com/gogs/gogs/conf/gitignore
        rm -f Clojure.gitignore
        rm -f Fortran.gitignore
        mv "C Sharp" "C-Sharp.gitignore"
    ) || 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/gogs \
        $RPM_BUILD_ROOT%{l_prefix}/share/gogs \
        $RPM_BUILD_ROOT%{l_prefix}/var/gogs/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/gogs/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/gogs/home \
        $RPM_BUILD_ROOT%{l_prefix}/var/gogs/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/gogs/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/gogs/repo

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

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

    #   install run-time files
    (   cd src/github.com/gogs/gogs
        cp -r conf public templates \
            $RPM_BUILD_ROOT%{l_prefix}/share/gogs/
    ) || exit $?

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

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

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

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

%files -f files

%clean

%post
    #   after upgrade, restart service
    if [ $1 -eq 1 ]; then
        #   display information about next steps
        (   echo "After starting Gogs with"
            echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc gogs 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} gogs status 2>/dev/null`
        [ ".$gogs_active" = .yes ] && %{l_rc} gogs restart
    fi
    exit 0

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

