File sing-box-ref1nd.spec of Package sing-box-ref1nd

%global         _build_id_links none
%global         debug_package %{nil}
%global         upstream_version 1.14.0-alpha.5-reF1nd
%global         software_name sing-box

Name:           %{software_name}-ref1nd
Version:        %{lua:print((rpm.expand("%upstream_version"):gsub("-", ".")))}
Release:        1%{?dist}
Summary:        The universal proxy platform

License:        GPL-3.0-or-later
URL:            https://sing-box.sagernet.org/
Source0:        https://github.com/reF1nd/sing-box/archive/refs/tags/v%{upstream_version}.tar.gz
Source1:        %{software_name}-%{upstream_version}-vendor.tar.gz

BuildRequires:  git, curl, tar, gcc, make, golang, systemd-rpm-macros
%ifnarch riscv64
BuildRequires:  mold
%endif
%{?systemd_requires}
Provides:       %{software_name} = %{version}-%{release}
Conflicts:      %{software_name}
Obsoletes:      %{name} < %{version}-%{release}

%description
The universal proxy platform.

%package        bash-completion
Summary:        Bash completion for %{name}
BuildArch:      noarch
Requires:       %{name} = %{version}-%{release}
Requires:       bash-completion
Supplements:    (%{name} and bash-completion)
Conflicts:      %{software_name}-bash-completion
Obsoletes:      %{name}-bash-completion < %{version}-%{release}
%description    bash-completion
Bash command line completion support for %{name}.

%package        zsh-completion
Summary:        Zsh completion for %{name}
BuildArch:      noarch
Requires:       %{name} = %{version}-%{release}
Requires:       zsh
Supplements:    (%{name} and zsh)
Conflicts:      %{software_name}-zsh-completion
Obsoletes:      %{name}-zsh-completion < %{version}-%{release}
%description    zsh-completion
Zsh command line completion support for %{name}.

%package        fish-completion
Summary:        Fish completion for %{name}
BuildArch:      noarch
Requires:       fish
Supplements:    (%{name} and fish)
Conflicts:      %{software_name}-fish-completion
Obsoletes:      %{name}-fish-completion < %{version}-%{release}
%description fish-completion
Fish command line completion support for %{name}.

%prep
%autosetup -p1 -n %{software_name}-%{upstream_version}

# Use vendored Go modules to minimize network access during build.
# (In mock/Koji, network is typically disabled; without vendor/ this will fail.)
tar -xf %{SOURCE1}
if [ ! -d vendor ]; then
    echo "ERROR: vendor/ directory not found. Provide a vendored modules tarball via Source1." >&2
    exit 1
fi

%build
_base_tags=with_gvisor,with_quic,with_wireguard,with_utls,with_tailscale,with_clash_api,with_acme,with_dhcp
%ifarch riscv64
_tags=$_base_tags
%else
_tags=$_base_tags,with_naive_outbound
%endif

# Prefer offline build: do not use module proxy or sumdb.
export GOPROXY=off
export GOSUMDB=off
export GOMODCACHE="$PWD/.gomodcache"

%ifnarch riscv64
# Use mold for Cronet compatibility. Cronet is built with Clang/LLVM;
# GNU ld incorrectly rejects libcronet.a as incompatible.
# mold is a modern linker that handles LLVM artifacts correctly and is faster than lld.
    export CGO_LDFLAGS="-fuse-ld=mold"
%endif

CGO_ENABLED=1 go build \
    -v \
    -trimpath \
    -buildmode=pie \
    -mod=vendor \
    -modcacherw \
    -buildvcs=false \
    -tags "$_tags" \
    -ldflags "
        -X \"github.com/sagernet/sing-box/constant.Version=%{upstream_version}\"
        -s -w -buildid= -checklinkname=0" \
    ./cmd/%{software_name}

echo "u %{software_name} - \"Sing-box Service\" - -" > "release/config/%{software_name}.sysusers"

install -d completions
./%{software_name} completion bash > completions/bash
./%{software_name} completion fish > completions/fish
./%{software_name} completion zsh  > completions/zsh

echo -n \
'// Allow sing-box to set domain and default-route
polkit.addRule(function(action, subject) {
    if ((action.id == "org.freedesktop.resolve1.set-domains" ||
         action.id == "org.freedesktop.resolve1.set-default-route" ||
         action.id == "org.freedesktop.resolve1.set-dns-servers") &&
         subject.user == "sing-box") {
        return polkit.Result.YES;
    }
});' > release/config/%{software_name}.rules

%install
install -Dsm755 %{software_name}                    -t %{buildroot}%{_bindir}
install -Dm644 "release/config/config.json"         -t %{buildroot}%{_sysconfdir}/%{software_name}
install -Dm644 "release/config/%{software_name}.service"   -t %{buildroot}/usr/lib/systemd/system
install -Dm644 "release/config/%{software_name}@.service" -t %{buildroot}/usr/lib/systemd/system
install -Dm644 "release/config/%{software_name}.sysusers"    %{buildroot}/usr/lib/sysusers.d/%{software_name}.conf
install -Dm644 "release/config/%{software_name}.rules"       %{buildroot}%{_datadir}/polkit-1/rules.d/%{software_name}.rules
install -Dm644 "completions/bash"                    %{buildroot}%{_datadir}/bash-completion/completions/%{software_name}
install -Dm644 "completions/fish"                    %{buildroot}%{_datadir}/fish/vendor_completions.d/%{software_name}.fish
install -Dm644 "completions/zsh"                     %{buildroot}%{_datadir}/zsh/site-functions/_%{software_name}
install -dm755 %{buildroot}%{_datadir}/%{software_name}

%post
%if 0%{?suse_version}
%service_add_post %{software_name}.service %{software_name}@.service
%else
%systemd_post %{software_name}.service %{software_name}@.service
%endif

%preun
%if 0%{?suse_version}
%service_del_preun %{software_name}.service %{software_name}@.service
%else
%systemd_preun %{software_name}.service %{software_name}@.service
%endif

%postun
%if 0%{?suse_version}
%service_del_postun %{software_name}.service %{software_name}@.service
%else
%systemd_postun %{software_name}.service %{software_name}@.service
%endif

%files
%license LICENSE
%{_bindir}/%{software_name}
%dir %{_sysconfdir}/%{software_name}
%config(noreplace) %{_sysconfdir}/%{software_name}/config.json
/usr/lib/systemd/system/%{software_name}*.service
/usr/lib/sysusers.d/%{software_name}.conf
%dir %{_datadir}/polkit-1/rules.d
%{_datadir}/polkit-1/rules.d/%{software_name}.rules
%dir %{_datadir}/%{software_name}

%files bash-completion
%{_datadir}/bash-completion/completions/%{software_name}

%files zsh-completion
%dir %{_datadir}/zsh
%dir %{_datadir}/zsh/site-functions
%{_datadir}/zsh/site-functions/_%{software_name}

%files fish-completion
%dir %{_datadir}/fish
%dir %{_datadir}/fish/vendor_completions.d
%{_datadir}/fish/vendor_completions.d/%{software_name}.fish

%changelog
* Mon Dec 22 2025 Sim0n <aur.direction446@aleeas.com> - 1.13.0.alpha.32.reF1nd-3
- carry libcronet.so

* Sun Dec 21 2025 Sim0n <aur.direction446@aleeas.com>  - 1.13.0.alpha.32.reF1nd-2
- pure go mod

* Sat Aug 16 2025 Sim0n <aur.direction446@aleeas.com> - 1.13.0-alpha.2-reF1nd
- Change config file install behavior

* Sat Aug 16 2025 Sim0n <aur.direction446@aleeas.com> - 1.13.0-alpha.2-reF1nd
- Bump version
- Change default config file name

* Thu Aug 14 2025 Sim0n <aur.direction446@aleeas.com> - 1.13.0-alpha.1-reF1nd
- Bump version

* Tue Aug 12 2025 Sim0n <aur.direction446@aleeas.com> - 1.12.1-reF1nd
- Bump version

* Mon Aug 04 2025 Sim0n <aur.direction446@aleeas.com> - 1.12.0-reF1nd1
- Add golang to BuildRequires and remove manual go download.
- Add gcc and make to BuildRequires for CGO support.

* Fri Aug 01 2025 Sim0n <aur.direction446@aleeas.com> - 1.12.0.rc.3.reF1nd-1
- Initial package release
openSUSE Build Service is sponsored by