#!/usr/bin/make -f

DH_VERBOSE := 1
DH_GOPKG := github.com/cri-o/cri-o
BUILDDIR := $(shell pwd)
DESTDIR := $(BUILDDIR)/debian/cri-o
PREFIX := /usr
BINDIR := $(DESTDIR)$(PREFIX)/bin
UNITDIR := $(DESTDIR)$(PREFIX)/lib/systemd/system
SYSCONFDIR := $(DESTDIR)/etc/default
BUILDTAGS := 'apparmor seccomp containers_image_ostree_stub exclude_graphdriver_btrfs exclude_graphdriver_devicemapper containers_image_openpgp'
GO_MD2MAN := /usr/bin/go-md2man

define CRIO_CONF_OVERRIDE
[crio.runtime.runtimes.runc]
runtime_path = "/usr/lib/cri-o-runc/sbin/runc"
runtime_type = "oci"
runtime_root = "/run/runc"
endef
export CRIO_CONF_OVERRIDE

ifneq (,$(wildcard /usr/lib/go-1.19/bin/go))
        GO_BINARY := /usr/lib/go-1.19/bin/go
else
        GO_BINARY := /usr/bin/go
endif
GO := GOPATH=$(BUILDDIR) GO111MODULE=off $(GO_BINARY)
GO_BUILD := "$(GO) build"
UPSTREAM_TAG=v1.26.4

%:
	dh_clean
	make clean
	rm -rf $(BUILDDIR)/obj-*-linux-gnu $(BUILDDIR)/debian/cri-o/*
	rm -rf $(BUILDDIR)/src $(BUILDDIR)/crio.conf $(BUILDDIR)/pkg/linux_*
	dh $@ --buildsystem=golang --with=golang

override_dh_auto_configure:
	# Include vendored dependencies.
	cp -rp $(BUILDDIR)/vendor $(BUILDDIR)/src
	ln -s $(BUILDDIR) $(BUILDDIR)/src/$(DH_GOPKG)

override_dh_auto_build:
	make \
	   GOPATH=$(BUILDDIR) \
	   GO_BUILD=$(GO_BUILD) \
	   BUILDTAGS=$(BUILDTAGS) \
	   PROJECT=$(DH_GOPKG) \
	   binaries docs

override_dh_auto_test:

override_dh_auto_install:
	make \
	   PREFIX=$(DESTDIR)/usr \
	   CRICTL_CONFIG_DIR=$(DESTDIR)/etc \
	   ETCDIR_CRIO=$(DESTDIR)/etc/crio \
	   GO_MD2MAN=$(GO_MD2MAN) \
	   GOPATH=$(BUILDDIR) \
	   GO_BUILD=$(GO_BUILD) \
	   CRICTL_CONFIG_DIR=$(DESTDIR)/etc \
	   install.bin-nobuild \
	   install.completions \
	   install.config \
	   install.man-nobuild \
	   install.systemd
	# install binaries
	install -dp $(DESTDIR)/etc/cni/net.d
	install -p -m 644 contrib/cni/10-crio-bridge.conflist $(DESTDIR)/etc/cni/net.d/100-crio-bridge.conflist
	install -p -m 644 contrib/cni/99-loopback.conflist $(DESTDIR)/etc/cni/net.d/200-loopback.conflist
	install -dp $(SYSCONFDIR)
	install -p -m 644 contrib/sysconfig/crio $(SYSCONFDIR)
	# install override file
	echo "$$CRIO_CONF_OVERRIDE" >$(DESTDIR)/etc/crio/crio.conf.d/01-crio-runc.conf
	# changes to unit and config files
	sed -i 's/sysconfig/default/g' $(SYSCONFDIR)/crio
	sed -i 's/sysconfig/default/g' $(UNITDIR)/crio.service
	sed -i 's/sysconfig/default/g' $(UNITDIR)/crio-wipe.service
	sed -i 's/\/usr\/local/\/usr/g' $(UNITDIR)/crio.service
	sed -i 's/\/usr\/local/\/usr/g' $(UNITDIR)/crio-wipe.service

override_dh_golang:

