# https://github.com/code-ready/admin-helper
%global goipath         github.com/containers/gvisor-tap-vsock
%global goname          gvisor-tap-vsock
Version:                0.4.0

%gometa

%global gobuilddir %{_builddir}/%{archivename}/_build

# debuginfo is not supported on RHEL with Go packages
%global debug_package %{nil}
%global _enable_debug_package 0

%global common_description %{expand:
gvisor-based usermode networking stack for use by container runtimes}


%global golicenses    LICENSE
%global godocs        *.md

Name:           %{goname}
Release:        1%{?dist}
Summary:        gvisor-based usermode networking stack
License:        APLv2
URL:            %{gourl}
Source0:        %{gosource}
Source1:        gvisor-tap-vsock-forwarder.service
Source2:        crc-tap0.nmconnection
Patch1:         0001-Update-gvisor-dependencies-to-latest.patch
Patch2:         0002-vm-Make-network-configuration-optional.patch
Patch3:         0003-Add-debug-logs.patch
Patch4:         0004-configure-vm-network-interface-with-nmcli-systemd.patch
Patch5:         0005-build-Vendor-makefat-code.patch

#generate_buildrequires
#go_generate_buildrequires

BuildRequires: git-core
BuildRequires: go-srpm-macros
BuildRequires: make
BuildRequires: systemd-rpm-macros

Requires: NetworkManager
Requires: systemd

%description
%{common_description}

%gopkg

%prep
# with fedora macros: goprep -k
%autosetup -S git -n %{archivename}
install -m 0755 -vd "$(dirname %{gobuilddir}/src/%{goipath})"
ln -fs "$(pwd)" "%{gobuilddir}/src/%{goipath}"

%build
export GOFLAGS="-a -mod=vendor"
make LDFLAGS="-ldflags \"-B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \n')\"" cross
make LDFLAGS="-ldflags \"-B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \n')\""

%install
# with fedora macros: gopkginstall
install -m 0755 -vd %{buildroot}%{_bindir}
install -m 0755 -vp %{gobuilddir}/src/%{goipath}/bin/vm %{buildroot}%{_bindir}/gvisor-tap-vsock-proxy
install -d %{buildroot}%{_unitdir}
install -m 0644 -vp %{SOURCE1} %{buildroot}%{_unitdir}
install -d %{buildroot}%{_sysconfdir}/NetworkManager/system-connections
install -m 0600 -vp %{SOURCE2} %{buildroot}%{_sysconfdir}/NetworkManager/system-connections/crc-tap0.nmconnection

install -d %{buildroot}%{_datadir}/%{name}-redistributable/{linux,macos,windows}
install -m 0755 -vp %{gobuilddir}/src/%{goipath}/bin/gvproxy-linux %{buildroot}%{_datadir}/%{name}-redistributable/linux/gvproxy
install -m 0755 -vp %{gobuilddir}/src/%{goipath}/bin/gvproxy-windows.exe %{buildroot}%{_datadir}/%{name}-redistributable/windows/gvproxy.exe
install -m 0755 -vp %{gobuilddir}/src/%{goipath}/bin/gvproxy-darwin %{buildroot}%{_datadir}/%{name}-redistributable/macos/gvproxy

%check
# with fedora macros: gocheck
export GOFLAGS="-mod=vendor"
#go test ./...

%files
%license %{golicenses}
%doc
%{_bindir}/*
%{_sysconfdir}/NetworkManager/system-connections/crc-tap0.nmconnection
%{_unitdir}/gvisor-tap-vsock-forwarder.service
%{_datadir}/%{name}-redistributable/*

#gopkgfiles

%changelog
* Tue May 31 2022 Anjan Nath <kaludios@gmail.com> - 0.0.11-1
- Update to admin-helper 0.0.11
- install macOS arm64 builds

* Tue May 17 2022 Christophe Fergeau <cfergeau@redhat.com> - 0.0.11-1
- Update to admin-helper 0.0.11

* Mon Jan 31 2022 Christophe Fergeau <cfergeau@redhat.com> - 0.0.9-1
- Update to admin-helper 0.0.9

* Wed Feb 03 2021 Christophe Fergeau <cfergeau@redhat.com> - 0.0.2-1
- Initial import in Fedora