# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as 
# published by the Free Software Foundation here:
# (http://www.gnu.org/licenses/gpl-2.0.html)
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

SHELL = /bin/bash

export SRCPKG = $(shell echo $(PWD)|sed 's/.*\///')
export ARCH = x86_64
export URL = https://www.kernel.org/
REPO = core
BRIEF = The Linux kernel
DESC = Linux is a clone of the operating system Unix, written from scratch \
by Linus Torvalds with assistance from a loosely-knit team of hackers \
across the Net.
export SNAPVER = $(shell grep '^CONFIG_LOCALVERSION=' SNAP/config| \
	sed 's/.*="\-\|"$$//g')

ARCHIVE := $(PWD)/SRC/$(shell ls SRC|egrep '(bz2|gz|tar|xz)$$'|tail -1)
TYPE := $(shell file -ib $(ARCHIVE)|cut -d';' -f1|tr -d '\n')
SRCDIR := $(shell tar -tf $(ARCHIVE)|head -1|sed 's/\/.*//')
PATCHDIR := $(PWD)/SRC/patches
export KERNVER := $(shell echo $(SRCDIR)|egrep -o '\-[0-9].*'|sed 's/^-//')
export VERSION := $(KERNVER)-$(SNAPVER)
export DEPENDS = grub,mkinitramfs,linux-firmware
export BUILDDEPS = bc,gcc

# This overrids the package file name in Makefile.snaplinux
# This is necessary due to the kernel version string being
# a part of the package name

PACKAGE = $(shell echo $(PWD)|sed 's/.*\///')-$(SNAPVER)
PKGFILE = $(PACKAGE).snap

LINUX-DOC := $(PWD)/linux-doc-$(KERNVER)
LINUX-DOC-ROOT := $(LINUX-DOC)/ROOT
LINUX-FIRMWARE := $(PWD)/linux-firmware
LINUX-FIRMWARE-ROOT := $(LINUX-FIRMWARE)/ROOT
LINUX-HEADERS := $(PWD)/linux-headers-$(KERNVER)-$(SNAPVER)
LINUX-HEADERS-ROOT := $(LINUX-HEADERS)/ROOT
LINUX-LIBC := $(PWD)/linux-libc
LINUX-LIBC-ROOT := $(LINUX-LIBC)/ROOT

include /usr/share/snap/Makefile.snaplinux

$(LINUX-DOC-ROOT): $(LINUX-HEADERS-ROOT)
	@install -v -d $(LINUX-DOC)/SNAP && \
	install -v -d $(LINUX-DOC-ROOT) && \
	install -d -m 755 \
		$(LINUX-DOC-ROOT)/usr/share/doc/linux-$(KERNVER) && \
	cp -r $(SRCDIR)/Documentation/* \
		$(LINUX-DOC-ROOT)/usr/share/doc/linux-$(KERNVER) && \
	find $(LINUX-DOC-ROOT) -name \*.txt -o -name \*.html \
		-exec gzip {} \; && \
	cd $(LINUX-DOC) && make -f ../Makefile.linux-doc && mv *.snap ../

$(LINUX-HEADERS-ROOT): $(LINUX-LIBC-ROOT)
	@HEADERS=$(LINUX-HEADERS-ROOT)/usr/src/linux-headers-$(VERSION) && \
	install -v -d $(LINUX-HEADERS)/SNAP && \
	install -v -d $(LINUX-HEADERS-ROOT) && \
	install -v -d -m 755 $$HEADERS && \
	install -v -d -m 755 $$HEADERS/arch/x86/kernel && \
	install -v -d -m 755 $$HEADERS/arch/x86/include && \
	install -v -d -m 755 $(LINUX-HEADERS-ROOT)/lib/modules/$(VERSION) && \
	ln -sf /usr/src/linux-headers-$(VERSION) \
		$(LINUX-HEADERS-ROOT)/lib/modules/$(VERSION)/build && \
	cd $(SRCDIR) && find {include,arch/x86*} \( -name \*.h -o \
		-name Kbuild -o -name Makefile -o -name \*.conf \) \
		-exec cp --parents {} $$HEADERS \; && \
	find scripts/ -type f -exec cp --parents {} $$HEADERS \; && \
	cp {.config,Makefile,Module.symvers} $$HEADERS && \
	cp -vp arch/x86/kernel/asm-offsets.s \
		$$HEADERS/arch/x86/kernel/asm-offsets.s && \
	cd $(LINUX-HEADERS) && make -f ../Makefile.linux-headers && \
		mv *.snap ../

$(LINUX-LIBC-ROOT): $(LINUX-FIRMWARE-ROOT)
	install -v -d $(LINUX-LIBC)/SNAP && \
	install -v -d $(LINUX-LIBC-ROOT) && \
	install -v -d -m 755 $(LINUX-LIBC-ROOT)/usr && \
	cd $(SRCDIR) && make headers_install \
		INSTALL_HDR_PATH=$(LINUX-LIBC-ROOT)/usr && \
	cd $(LINUX-LIBC) && make -f ../Makefile.linux-libc && mv *.snap ../

$(LINUX-FIRMWARE-ROOT): $(SRCDIR)/arch/x86_64/boot/bzImage
	@install -v -d $(LINUX-FIRMWARE)/SNAP && \
	install -v -d $(LINUX-FIRMWARE-ROOT)/lib/firmware && \
	cd $(SRCDIR) && make firmware_install \
		INSTALL_FW_PATH=$(LINUX-FIRMWARE-ROOT)/lib/firmware && \
	cd $(LINUX-FIRMWARE) && make -f \
		../Makefile.linux-firmware && mv *.snap ../

$(SRCDIR)/Makefile: $(ARCHIVE)
	@if [ '$(TYPE)' == 'application/x-bzip2' ]; then \
		tar -jxf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-gzip' ]; then \
		tar -zxf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-tar' ]; then \
		tar -xf $(ARCHIVE); \
	elif [ '$(TYPE)' == 'application/x-xz' ]; then \
		tar -xf $(ARCHIVE); \
	else \
		echo 'Unable to determine archive type'; \
		exit 1; \
	fi
	@touch $(SRCDIR)/Makefile

$(SRCDIR)/arch/x86_64/boot/bzImage: $(SRCDIR)/Makefile
	@cd $(SRCDIR) && make mrproper && \
	cp $(PWD)/SNAP/config .config && CONFIG_MODVERSIONS=1 make

$(ROOT): $(LINUX-DOC-ROOT)
	@if [ -d $(ROOT) ]; then \
		touch $(ROOT); \
	else \
		mkdir -v $(ROOT); \
	fi

	@cd $(SRCDIR); \
	make modules_install INSTALL_MOD_PATH=$(ROOT) && \
	rm $(ROOT)/lib/modules/$(VERSION)/{build,source,modules.dep*} && \
	rm -rf $(ROOT)/lib/firmware && \
	install -d -m 755 $(ROOT)/boot && \
	install -d -m 755 $(ROOT)/etc/modprobe.d && \
	install -d -m 755 $(ROOT)/usr/share/linux-$(VERSION) && \
	install -m 644 .config $(ROOT)/boot/config-$(VERSION) && \
	install -m 644 System.map \
		$(ROOT)/boot/System.map-$(VERSION) && \
	install -m 644 arch/x86_64/boot/bzImage \
		$(ROOT)/boot/vmlinuz-$(VERSION) && \
	install -m 644 ../SNAP/usb.conf $(ROOT)/usr/share/linux-$(VERSION) && \
	install -m 644 Module.symvers $(ROOT)/lib/modules/$(VERSION)

test: $(ROOT)
	@cd $(SRCDIR); \
	make test

clean:
	@rm -rvf $(ROOT) \
		$(LINUX-DOC) \
		$(LINUX-FIRMWARE) \
		$(LINUX-LIBC) \
		$(LINUX-HEADERS) \
		$(SNAPINFO) \
		$(MANIFEST) \
		$(FILES) \
		$(SRCDIR)

