include $(TOPDIR)/rules.mk

PKG_NAME:=docker-ce
PKG_VERSION:=19.03.13
PKG_RELEASE:=1
PKG_LICENSE:=Apache-2.0
PKG_LICENSE_FILES:=components/cli/LICENSE components/engine/LICENSE

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/docker/docker-ce/tar.gz/v$(PKG_VERSION)?
PKG_HASH:=b2ff08675738031b6d6d59a90226657c16726851957c11b5fb85a4598b933b92
PKG_SOURCE_VERSION:=4484c46d9d # SHA1 used within the docker executables

PKG_MAINTAINER:=Gerard Ryan <G.M0N3Y.2503@gmail.com>

# $(1) = path to dependent package 'Makefile'
# $(2) = relevant docker-ce '.installer' file
define EnsureVendoredVersion
	( \
		DEP_VER=$$$$( grep --only-matching --perl-regexp '(?<=PKG_SOURCE_VERSION:=)(.*)' "$(1)" ); \
		VEN_VER=$$$$( grep --only-matching --perl-regexp '(?<=_COMMIT:=)(.*)(?=})' "$(PKG_BUILD_DIR)/components/engine/hack/dockerfile/install/$(2)" ); \
		if [ $$$$VEN_VER != $$$$DEP_VER ]; then \
			echo "ERROR: Expected 'PKG_SOURCE_VERSION:=$$$$VEN_VER' in '$(1)', found 'PKG_SOURCE_VERSION:=$$$$DEP_VER'"; \
			exit 1; \
		fi \
	)
endef

PKG_BUILD_DEPENDS:=golang/host
PKG_BUILD_PARALLEL:=1

GO_PKG:=github.com/docker

include $(INCLUDE_DIR)/package.mk
include ../../lang/golang/golang-package.mk

define Package/docker-ce/config
  source "$(SOURCE)/Config.in"
endef

define Package/docker-ce
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Docker Community Edition
  URL:=https://www.docker.com/
  DEPENDS:=$(GO_ARCH_DEPENDS) @(aarch64||arm||x86_64) +btrfs-progs +ca-certificates +cgroupfs-mount +containerd +libdevmapper +libnetwork +tini \
           +DOCKER_SECCOMP:libseccomp +iptables-mod-extra +kmod-br-netfilter +kmod-ikconfig +kmod-nf-conntrack-netlink +kmod-nf-ipvs +kmod-veth
  USERID:=docker:docker
  MENU:=1
endef

define Package/docker-ce/conffiles
/etc/config/dockerd
endef

define Package/docker-ce/description
  Docker Engine is used by millions enables containerized applications
  to run anywhere consistently on any infrastructure.
endef

define Build/Prepare
	$(Build/Prepare/Default)

	# Verify dependencies are the vendored version
	$(call EnsureVendoredVersion,../containerd/Makefile,containerd.installer)
	$(call EnsureVendoredVersion,../libnetwork/Makefile,proxy.installer)
	$(call EnsureVendoredVersion,../runc/Makefile,runc.installer)
	$(call EnsureVendoredVersion,../tini/Makefile,tini.installer)
endef

define Build/Configure
	# move so GoPackage/Build/Configure will get the correct path
	mv $(PKG_BUILD_DIR)/components/engine $(PKG_BUILD_DIR)/
	mv $(PKG_BUILD_DIR)/components/cli $(PKG_BUILD_DIR)/

	# docker generates files at build time so we'll just symlink for now and call GoPackage/Build/Configure later
	mkdir -p $(GO_PKG_BUILD_DIR)/bin \
			 $(GO_PKG_BUILD_DIR)/src \
			 $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/ \
			 $(GO_BUILD_CACHE_DIR)
	$(LN) $(PKG_BUILD_DIR)/cli $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/cli
	$(LN) $(PKG_BUILD_DIR)/engine $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/docker
endef

ifeq ($(CONFIG_DOCKER_SECCOMP),y)
BUILDTAGS:=seccomp
else
BUILDTAGS:=
endif

define Build/Compile
	( \
		export $(GO_PKG_VARS) \
			GITCOMMIT=$(PKG_SOURCE_VERSION) \
			DOCKER_GITCOMMIT=$(PKG_SOURCE_VERSION) \
			DOCKER_BUILDTAGS='$(BUILDTAGS)' \
			VERSION=$(PKG_VERSION) \
		\
		&& echo "Compiling CLI..." \
		&& cd $(PKG_BUILD_DIR)/cli \
		&& ./scripts/build/binary \
		\
		&& echo "Compiling Engine..." \
		&& cd $(PKG_BUILD_DIR)/engine \
		&& ./hack/make.sh binary \
	)

	# done here to include autogenerated files also
	rm $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/cli
	rm $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/docker
	$(call GoPackage/Build/Configure)
endef

define Package/docker-ce/install
	$(INSTALL_DIR) $(1)/usr/bin/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/cli/build/docker $(1)/usr/bin/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/engine/bundles/binary-daemon/dockerd $(1)/usr/bin/

	$(INSTALL_DIR) $(1)/opt/docker/
	$(INSTALL_DIR) $(1)/usr/share/docker/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/engine/contrib/check-config.sh $(1)/usr/share/docker/

	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/dockerd.init $(1)/etc/init.d/dockerd

	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/etc/config/dockerd $(1)/etc/config/dockerd

	# Must be after systcl 11-br-netfilter.conf from kmod-br-netfilter
	$(INSTALL_DIR) $(1)/etc/sysctl.d
	$(INSTALL_DATA) ./files/etc/sysctl.d/sysctl-br-netfilter-ip.conf \
		$(1)/etc/sysctl.d/12-br-netfilter-ip.conf
endef

define Package/docker-ce/postinst
#!/bin/sh
[ -n "$$IPKG_INSTROOT" ] || {
	/etc/init.d/dockerd enable
	/etc/init.d/dockerd uciadd
	/etc/init.d/dockerd start
}
endef

define Package/docker-ce/prerm
#!/bin/sh
[ -n "$$IPKG_INSTROOT" ] || {
	/etc/init.d/dockerd disable
	/etc/init.d/dockerd stop
	/etc/init.d/dockerd ucidel
}
endef

$(eval $(call BuildPackage,docker-ce))
