#!/usr/bin/make -f

export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

SHELL=/bin/bash

%:
	dh $@

override_dh_auto_build-arch:
	# /usr/src/packages/SOURCES is OBS specific path
	tar xf /usr/src/packages/SOURCES/ch-6.12.8-g67d9c8da.tar.gz --strip-components=1
	#make mrproper
	make ch_defconfig
	make LC_ALL= ARCH=$(KERNEL_MAKE_ARCH) oldconfig
	KCFLAGS="$(KERNEL_KCFLAGS)" make ARCH=$(KERNEL_MAKE_ARCH) $(MAKE_TARGET) -j3

override_dh_auto_install:
	install -d debian/kernel-cloud-hypervisor-guest/usr
	install -d debian/kernel-cloud-hypervisor-guest/usr/share
	install -d debian/kernel-cloud-hypervisor-guest/usr/share/cloud-hypervisor
	install -D -m 644 $(IMAGE) debian/kernel-cloud-hypervisor-guest/usr/share/cloud-hypervisor/$(IMAGE_FNAME)

override_dh_auto_clean:
	make clean

DISTRO := $(shell cat /etc/os-release | grep '^ID=' | sed 's,ID=\(.*\),\1,')
VER := $(shell cat /etc/os-release | grep '^VERSION_ID=' | sed 's,VERSION_ID="\(.*\)",\1,')

ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
    ifeq ($(DISTRO), ubuntu)
    ifneq ($(VER), 18.04)
        KERNEL_KCFLAGS := -Wa,-mx86-used-note=no
    endif
    endif
    ifeq ($(DISTRO), debian)
    ifneq ($(VER), 10)
        KERNEL_KCFLAGS := -Wa,-mx86-used-note=no
    endif
    endif
    KERNEL_CONFIG := linux-config-x86_64
    IMAGE_FNAME := vmlinux.bin
    IMAGE := arch/x86/boot/compressed/$(IMAGE_FNAME)
    MAKE_TARGET := bzImage
    KERNEL_MAKE_ARCH := x86_64
else ifeq ($(DEB_BUILD_ARCH_CPU), arm64)
    KERNEL_CONFIG := linux-config-aarch64
    IMAGE_FNAME := Image
    IMAGE := arch/arm64/boot/$(IMAGE_FNAME)
    MAKE_TARGET := 
    KERNEL_MAKE_ARCH := arm64
else
    $(error unrecognized build host instruction set $(DEB_HOST_ARCH_CPU)!)
endif

