%bcond_with btrfs

# docker
%global goipath_moby github.com/docker/docker
%global git_moby https://%%{goipath_moby}
%global shortcommit_moby  60b9add

# cli
%global goipath_cli github.com/docker/cli
%global git_cli https://%%{goipath_cli}
#%%global commit_cli baeda1f82a10204ec5708d5fbba130ad76cfee49
#%%global shortcommit_cli %%(c=%%{commit_cli}; echo ${c:0:7})

# tini
%global git_tini https://github.com/krallin/tini
%global commit_tini 0b44d3665869e46ccbac7414241b8256d6234dc4
%global shortcommit_tini %(c=%{commit_tini}; echo ${c:0:7})

Name:           docker
Version:        26.0.1
Release:        1%{?dist}
Summary:        The open-source application container engine
License:        Apache-2.0
Source0:        %{git_moby}/archive/v%{version}/moby-%{version}.tar.gz
Source1:        %{git_cli}/archive/v%{version}/cli-%{version}.tar.gz
Source2:        %{git_tini}/archive/%{commit_tini}/tini-%{shortcommit_tini}.tar.gz
Source3:        docker.service
Source4:        docker.sysconfig
Source5:        docker-systemd-sysusers.conf
Source6:        generate-docs.sh

# Seperate file containing virtual provides for bundled deps that's %%include'd in the specfile.
Source100:      provides.spec.inc

URL:            https://www.docker.com

ExclusiveArch:  %{golang_arches}

%if %{with btrfs}
BuildRequires:  pkgconfig(libbtrfsutil)
%endif
BuildRequires:  pkgconfig(devmapper)
BuildRequires:  golang
BuildRequires:  go-rpm-macros
BuildRequires:  go-md2man
BuildRequires:  pkgconfig(libseccomp) >= 2.3.0
BuildRequires:  make
BuildRequires:  pkgconfig(audit)
BuildRequires:  pkgconfig(systemd)
BuildRequires:  systemd-rpm-macros
BuildRequires:  firewalld-filesystem

# Build dependencies for tini
BuildRequires:  cmake
BuildRequires:  glibc-static

# required packages on install
Requires:       container-selinux
Requires:       containerd
Requires:       iptables
Requires:       pigz
Requires:       runc
Requires:       systemd
Requires:       tar
Requires:       xz

# Resolves: rhbz#1165615
Requires:       device-mapper-libs >= 1.02.90-1

Provides:       moby = %{version}-%{release}
Provides:       moby-engine = %{version}-%{release}
Provides:       docker-latest = %{version}-%{release}

# conflicting packages
Conflicts:      docker-ce
Conflicts:      docker-ce-cli
Conflicts:      docker-common
Conflicts:      docker-ee
Conflicts:      docker-engine-cs
Conflicts:      docker-io
Conflicts:      podman-docker

%include %{SOURCE100}

%description
Docker is an open source project to build, ship and run any application as a
lightweight container.

Docker containers are both hardware-agnostic and platform-agnostic. This means
they can run anywhere, from your laptop to the largest EC2 compute instance and
everything in between - and they don't require you to use a particular
language, framework or packaging system. That makes them great building blocks
for deploying and scaling web apps, databases, and backend services without
depending on a particular stack or provider.


%prep
%setup -q -a 1 -a 2 -n moby-%{version}

# correct rpmlint errors for bash completion
sed -i '/env bash/d' cli-%{version}/contrib/completion/bash/docker
cp %{SOURCE6} cli-%{version}/scripts/docs/generate-man.sh
%build
mkdir -p _build/bin

%global _root %{_builddir}/moby-%{version}

export CGO_CFLAGS="-I%{_includedir}/openssl3 %{build_cflags}"
export CGO_CXXFLAGS="-I%{_includedir}/openssl3 %{build_cxxflags}"
export GOFLAGS='-buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-compressdwarf=false -ldflags=-B=gobuildid'
export DISABLE_WARN_OUTSIDE_CONTAINER=1

# build tini (installed as docker-init)
(
        cd tini-%{commit_tini}
        %cmake
        make tini-static -C "%{__cmake_builddir}"
)

%global buildtime %(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")
# build engine
(
        mkdir -p src/github.com/docker
        # Link source and vendored deps into local GOPATH.
        ln -fns ../../.. src/%{goipath_moby}
        # Build using source and vendored deps in local GOPATH.
        #export GOPATH="${PWD}"
        export AUTO_GOPATH=1
        export GO111MODULE=off
        export LDFLAGS="-w"
        export LDFLAGS+=" -X github.com/docker/docker/dockerversion.Version=%{version}"
        export LDFLAGS+=" -X github.com/docker/docker/dockerversion.GitCommit=%{shortcommit_moby}"
        export LDFLAGS+=" -X github.com/docker/docker/dockerversion.IAmStatic=false"
        export LDFLAGS+=" -X 'github.com/docker/docker/dockerversion.BuildTime=%{buildtime}'"
        export DOCKER_BUILDTAGS="seccomp selinux journald"
        export BUILDTAGS="${DOCKER_BUILDTAGS}"
        export GOBUILDTAGS="${BUILDTAGS}"
        ln -s {vendor,go}.mod
        ln -s {vendor,go}.sum
        #gobuild -o _build/bin/dockerd %{goipath_moby}/cmd/dockerd
        VERSION=%{version} DOCKER_GITCOMMIT=%{shortcommit_moby} hack/make.sh dynbinary
        mv bundles/dynbinary-daemon/dockerd _build/bin/
        mv bundles/dynbinary-daemon/docker-proxy _build/bin/
)

# build cli
(
        cd cli-%{version}
        mkdir -p src/github.com/docker
        # Link source and vendored deps into local GOPATH.
        ln -fns ../../.. src/%{goipath_cli}
        # export DISABLE_WARN_OUTSIDE_CONTAINER=1
        # Build using source and vendored deps in local GOPATH.
        export GOPATH="${PWD}"
        #export GO111MODULE=off
        export LDFLAGS="\
            -w \
            -X \"github.com/docker/cli/cli/version.GitCommit=%{shortcommit_cli}\" \
            -X \"github.com/docker/cli/cli/version.BuildTime=%{buildtime}\" \
            -X \"github.com/docker/cli/cli/version.Version=%{version}\" \
    "
        export BUILDTAGS="pkcs11"
        export GOBUILDTAGS="${BUILDTAGS}"
        %gobuild -o ../_build/bin/docker %{goipath_cli}/cmd/docker
        # make VERSION=%%{version} GITCOMMIT=%%{shortcommit_cli} dynbinary

	scripts/docs/generate-man.sh
)

%install
# install binary
install -Dpm 755 _build/bin/docker _build/bin/dockerd -t %{buildroot}%{_bindir}/

# install proxy
install -Dpm 755 _build/bin/docker-proxy -t %{buildroot}%{_libexecdir}/docker/

# install tini
install -Dpm 755 tini-%{commit_tini}/%{__cmake_builddir}/tini-static %{buildroot}%{_libexecdir}/docker/docker-init

# install udev rules
install -Dpm 644 contrib/udev/80-docker.rules -t %{buildroot}%{_usr}/lib/udev/rules.d/

# add init scripts
install -Dpm 644 %{SOURCE3} contrib/init/systemd/docker.socket -t %{buildroot}%{_unitdir}/

# for additional args
install -Dpm 644 %{SOURCE4} %{buildroot}%{_sysconfdir}/sysconfig/docker

# Install sysusers configuration
install -Dpm 0644 %{SOURCE5} %{buildroot}%{_sysusersdir}/docker.conf

# add bash, zsh, and fish completions
install -Dpm 644 cli-%{version}/contrib/completion/bash/docker -t %{buildroot}%{_datadir}/bash-completion/completions/
install -Dpm 644 cli-%{version}/contrib/completion/zsh/_docker -t %{buildroot}%{_datadir}/zsh/site-functions/
install -Dpm 644 cli-%{version}/contrib/completion/fish/docker.fish -t %{buildroot}%{_datadir}/fish/vendor_completions.d/

# install manpages
install -Dpm 644 cli-%{version}/man/man1/*.1 -t %{buildroot}%{_mandir}/man1/
install -Dpm 644 cli-%{version}/man/man5/*.5 -t %{buildroot}%{_mandir}/man5/
install -Dpm 644 cli-%{version}/man/man8/*.8 -t %{buildroot}%{_mandir}/man8/

# add nano files
install -Dpm 644 contrib/syntax/nano/Dockerfile.nanorc -t %{buildroot}%{_datadir}/nano/

for cli_file in LICENSE MAINTAINERS NOTICE README.md; do
    cp "cli-%{version}/$cli_file" "cli-$cli_file"
done

%pre
%sysusers_create_compat %{SOURCE5}

%post
%systemd_post docker.service docker.socket
%firewalld_reload

%preun
%systemd_preun docker.service docker.socket

%postun
%systemd_postun_with_restart docker.service

%files
%license LICENSE cli-LICENSE
%doc AUTHORS CONTRIBUTING.md MAINTAINERS NOTICE README.md
%doc cli-MAINTAINERS cli-NOTICE cli-README.md
%config(noreplace) %{_sysconfdir}/sysconfig/docker
%{_bindir}/docker
%{_bindir}/dockerd
%dir %{_libexecdir}/docker/
%{_libexecdir}/docker/docker-proxy
%{_libexecdir}/docker/docker-init
%{_usr}/lib/udev/rules.d/80-docker.rules
%{_unitdir}/docker.service
%{_unitdir}/docker.socket
%{_sysusersdir}/docker.conf
%{_datadir}/bash-completion/completions/docker
%{_mandir}/man1/docker*.1*
%{_mandir}/man5/{Dockerfile,docker-config-json}.5*
%{_mandir}/man8/dockerd.8*
%{_datadir}/zsh/site-functions/_docker
%{_datadir}/fish/vendor_completions.d/docker.fish
%dir %{_datadir}/nano
%{_datadir}/nano/Dockerfile.nanorc

