# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2022 Frédéric Pierret (fepitre) <frederic@invisiblethingslab.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

DIST ?= fc32
DIST_VER = $(subst fc,,$(DIST))

BUILDER_DIR ?= /builder
PLUGINS_DIR ?= $(BUILDER_DIR)/plugins
INSTALLER_DIR ?= $(PLUGINS_DIR)/installer
INSTALLER_KICKSTART ?= $(INSTALLER_DIR)/conf/qubes-kickstart.cfg
CREATEREPO := $(shell which createrepo_c createrepo 2>/dev/null |head -1)

ifdef QUBES_RELEASE
	ISO_VERSION ?= $(QUBES_RELEASE)
else
	ISO_VERSION ?= $(shell date +%Y%m%d)
endif

ifneq (,$(ISO_FLAVOR))
ISO_NAME := Qubes-$(ISO_VERSION)-$(ISO_FLAVOR)-x86_64
else
ISO_NAME := Qubes-$(ISO_VERSION)-x86_64
endif
ISO_VOLID := $(shell echo $(ISO_NAME) | tr a-z A-Z | tr -s -c [:alnum:]'\n' - | head -c 32)

BASE_DIR := $(INSTALLER_DIR)/work/$(ISO_VERSION)/x86_64
TMP_DIR:= $(BUILDER_DIR)/tmp

DNF := /usr/bin/dnf
DNF_ROOT := $(BUILDER_DIR)/dnfroot
DNF_REPO := $(DNF_ROOT)/etc/yum.repos.d/installer.repo
DNF_PACKAGES := $(DNF_ROOT)/tmp/packages.list
DNF_OPTS := -y --releasever=$(DIST_VER) --installroot=$(DNF_ROOT) --config=$(DNF_ROOT)/etc/dnf/dnf.conf

LORAX := /usr/sbin/lorax
LORAX_PACKAGES := $(DNF_ROOT)/tmp/lorax_packages.list
LORAX_OPTS := --product "Qubes OS" --variant "qubes" --macboot --force --rootfs-size=4
LORAX_OPTS += --version "$(ISO_VERSION)" --release "Qubes OS $(ISO_VERSION)" --volid $(ISO_VOLID)
LORAX_OPTS += --workdir $(INSTALLER_DIR)/work/work/x86_64 --logfile $(INSTALLER_DIR)/work/logs/lorax-x86_64.log
LORAX_OPTS += --repo $(DNF_REPO) --skip-branding --disablerepo=fedora --disablerepo=fedora-updates --disablerepo=updates --disablerepo='qubes-*'

ifeq ($(ISO_USE_KERNEL_LATEST),1)
LORAX_OPTS += --installpkgs kernel-latest --excludepkgs kernel
endif

ifdef QUBES_RELEASE
	LORAX_OPTS += --isfinal
endif

MKISOFS := /usr/bin/xorriso -as mkisofs
# common mkisofs flags
MKISOFS_OPTS := -v -U -J --joliet-long -R -T -m repoview -m boot.iso
# x86 boot args
MKISOFS_OPTS += -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table
# efi boot args
MKISOFS_OPTS += -eltorito-alt-boot -e images/efiboot.img -no-emul-boot

#
# CAGE -> MOCK
#
iso-prepare:
	#
	# Prepare repositories
	#

	rm -rf $(TMP_DIR)

	# Copy the comps file
	mkdir -p $(TMP_DIR)
	cp $(INSTALLER_DIR)/meta-packages/comps/comps-dom0.xml $(TMP_DIR)/comps.xml
	if [ "$(ISO_USE_KERNEL_LATEST)" == 1 ]; then \
		sed -i 's#optional">kernel-latest#mandatory">kernel-latest#g' $(TMP_DIR)/comps.xml; \
	fi;

	mkdir /tmp/qubes-installer
	mount --bind $(INSTALLER_DIR) /tmp/qubes-installer

	mkdir -p /tmp/qubes-installer/yum/installer/rpm
	rm -rf $(INSTALLER_DIR)/yum/installer/repodata
	$(CREATEREPO) -q -g $(TMP_DIR)/comps.xml /tmp/qubes-installer/yum/installer

	mkdir -p $(BUILDER_DIR)/repository /tmp/qubes-installer/yum/qubes-dom0
	rm -rf $(BUILDER_DIR)/repository/repodata
	$(CREATEREPO) -q -g $(TMP_DIR)/comps.xml $(BUILDER_DIR)/repository
	mount --bind $(BUILDER_DIR)/repository /tmp/qubes-installer/yum/qubes-dom0

	#
	# Prepare DNF
	#

	# Destination directory for RPM
	mkdir -p $(BASE_DIR)/os/Packages

	# Create default DNF conf
	mkdir -p $(DNF_ROOT)/etc/dnf
	cp $(INSTALLER_DIR)/yum/dnf.conf $(DNF_ROOT)/etc/dnf/

	# Copy Fedora key to DNF installroot
	mkdir -p $(DNF_ROOT)/etc/pki/rpm-gpg
	cp $(INSTALLER_DIR)/qubes-release/RPM-GPG-KEY-fedora-$(DIST_VER)-primary $(DNF_ROOT)/etc/pki/rpm-gpg

iso-parse-kickstart:
	mkdir -p $(DNF_ROOT)/etc/yum.repos.d $(DNF_ROOT)/tmp
	$(INSTALLER_DIR)/scripts/ksparser --ks $(INSTALLER_KICKSTART) --extract-repo-conf-to $(DNF_REPO) --extract-packages-to $(DNF_PACKAGES)

iso-parse-tmpl:
	$(INSTALLER_DIR)/scripts/tmplparser --repo $(DNF_REPO) --extract-packages-to $(LORAX_PACKAGES)

#
# CAGE
#

iso-packages-anaconda:
	$(DNF) $(DNF_OPTS) clean all
	umask 022; $(DNF) $(DNF_OPTS) --downloaddir=$(BASE_DIR)/os/Packages --downloadonly install $(shell cat $(DNF_PACKAGES))
	pushd $(BASE_DIR)/os/ && $(CREATEREPO) -q -g $(TMP_DIR)/comps.xml .

iso-packages-lorax:
	$(DNF) $(DNF_OPTS) clean all
	umask 022; $(DNF) $(DNF_OPTS) --downloaddir=$(INSTALLER_DIR)/yum/installer/rpm --downloadonly install $(shell cat $(LORAX_PACKAGES))
	pushd $(INSTALLER_DIR)/yum/installer && $(CREATEREPO) -q -g $(TMP_DIR)/comps.xml --update .

#
# CAGE -> MOCK
#
iso-installer-lorax:
	$(LORAX) $(LORAX_OPTS) $(BASE_DIR)/os

iso-installer-mkisofs:
	mkdir -p $(BASE_DIR)/iso/
	chmod og+rX -R $(BASE_DIR)/os/
	$(MKISOFS) $(MKISOFS_OPTS) -V $(ISO_VOLID) -o $(BASE_DIR)/iso/$(ISO_NAME).iso $(BASE_DIR)/os/
	/usr/bin/isohybrid -u $(BASE_DIR)/iso/$(ISO_NAME).iso
	/usr/bin/implantisomd5  $(BASE_DIR)/iso/$(ISO_NAME).iso


