# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2021/11/02 17:47:57 by gbaconni          #+#    #+#              #
#    Updated: 2022/01/25 12:45:23 by gbaconni         ###   lausanne.ch        #
#                                                                              #
# **************************************************************************** #

DVER = 11.1.0

RAM = 2048
SSD = 31539

CORES = 2
THREADS = 1
SOCKETS = 2

KERNEL = $(shell uname -s)
MACHINE = $(shell uname -m)
FT = $(shell if test -L ~/goinfre; then echo "yes"; else echo "no"; fi)

QEMU86 = qemu-system-x86_64
ifeq ($(KERNEL),Linux)
ifeq ($(MACHINE),x86_64)
	QEMU86 = /usr/bin/qemu-system-x86_64
endif
endif

all: sync

virtualbox:
	@test -d ~/goinfre/VirtualBox\ VMs	|| mkdir -p ~/goinfre/VirtualBox\ VMs
	@test -L ~/VirtualBox\ VMs			|| ln -snf ~/goinfre/VirtualBox\ VMs ~/VirtualBox\ VMs
	@echo /Applications/VirtualBox.app/Contents/MacOS/VBoxManage createmedium disk --filename ~/goinfre/VirtualBox\\ VMs/$$(basename ~)42/$$(basename ~)42.vdi --size $(SSD) --format VDI

signature:
ifeq ($(KERNEL),Darwin)
	@shasum ~/goinfre/VirtualBox\ VMs/$$(basename ~)42/$$(basename ~)42.vdi | cut -d' ' -f1
endif
ifeq ($(KERNEL),Linux)
	@sha1sum ~/goinfre/VirtualBox\ VMs/$$(basename ~)42/$$(basename ~)42.vdi | cut -d' ' -f1
endif

data:
ifeq ($(FT),yes)
	@test -d ~/goinfre/born2beroot || install -d ~/goinfre/born2beroot
	@test -L data || ln -snf ~/goinfre/born2beroot data
else
	@test -d data || install -d data
endif

uefi: data
ifeq ($(KERNEL),Linux)
	@test -f data/efi-virtio.rom 		|| cp /usr/lib/ipxe/qemu/efi-virtio.rom data/efi-virtio.rom
	@test -f data/edk2-aarch64-code.fd	|| cp /usr/share/AAVMF/AAVMF_CODE.fd data/edk2-aarch64-code.fd
	@test -f data/edk2-arm-vars.fd		|| cp /usr/share/AAVMF/AAVMF_VARS.fd data/edk2-arm-vars.fd
	@test -f data/edk2-x86_64-code.fd	|| cp /usr/share/OVMF/OVMF_CODE_4M.fd data/edk2-x86_64-code.fd
	@test -f data/edk2-i386-vars.fd		|| cp /usr/share/OVMF/OVMF_VARS_4M.fd data/edk2-i386-vars.fd
endif
ifeq ($(KERNEL),Darwin)
ifeq ($(FT),yes)
	@test -f data/efi-virtio.rom		|| cp ~/.brew/Cellar/qemu/*/share/qemu/efi-virtio.rom data/efi-virtio.rom
	@test -f data/edk2-aarch64-code.fd	|| cp ~/.brew/Cellar/qemu/*/share/qemu/edk2-aarch64-code.fd data/edk2-aarch64-code.fd
	@test -f data/edk2-arm-vars.fd		|| cp ~/.brew/Cellar/qemu/*/share/qemu/edk2-arm-vars.fd data/edk2-arm-vars.fd
	@test -f data/edk2-x86_64-code.fd	|| cp ~/.brew/Cellar/qemu/*/share/qemu/edk2-x86_64-code.fd data/edk2-x86_64-code.fd
	@test -f data/edk2-i386-vars.fd		|| cp ~/.brew/Cellar/qemu/*/share/qemu/edk2-i386-vars.fd data/edk2-i386-vars.fd
else
	@test -f data/efi-virtio.rom		|| cp /opt/homebrew/Cellar/qemu/*/share/qemu/efi-virtio.rom data/efi-virtio.rom
	@test -f data/edk2-aarch64-code.fd	|| cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-aarch64-code.fd data/edk2-aarch64-code.fd
	@test -f data/edk2-arm-vars.fd		|| cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-arm-vars.fd data/edk2-arm-vars.fd
	@test -f data/edk2-x86_64-code.fd	|| cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-x86_64-code.fd data/edk2-x86_64-code.fd
	@test -f data/edk2-i386-vars.fd		|| cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-i386-vars.fd data/edk2-i386-vars.fd
endif
endif

qemu:
ifeq ($(KERNEL),Linux)
	@which qemu-system-x86_64 >/dev/null 2>&1 || sudo apt-get install -qq -y qemu-utils qemu-system-x86 qemu-system-arm qemu-efi-aarch64 ovmf ipxe-qemu expect
ifeq ($(MACHINE),x86_64)
	sudo setcap -q 'cap_net_bind_service=+ep' /usr/bin/qemu-system-x86_64
endif
endif
ifeq ($(KERNEL),Darwin)
ifeq ($(FT),yes)
	@which brew >/dev/null 2>&1 || curl -fsSL https://rawgit.com/kube/42homebrew/master/install.sh | zsh
	@test -L ~/.brew/bin/qemu-system-x86_64 || (eval "$$(/opt/homebrew/bin/brew shellenv)" && brew install qemu)
else
	@which brew >/dev/null 2>&1 || bash -c "$$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
	@test -L /opt/homebrew/bin/qemu-system-x86_64 || (eval "$$(/opt/homebrew/bin/brew shellenv)" && brew install qemu)
endif
endif

curl:
ifeq ($(KERNEL),Linux)
	@which curl >/dev/null 2>&1 || sudo apt-get install -qq -y curl
endif

debian: data curl
	@test -f data/debian-$(DVER)-amd64-netinst.iso || curl -sLo data/debian-$(DVER)-amd64-netinst.iso https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-$(DVER)-amd64-netinst.iso
	@test -f data/debian-$(DVER)-arm64-netinst.iso || curl -sLo data/debian-$(DVER)-arm64-netinst.iso https://cdimage.debian.org/debian-cd/current/arm64/iso-cd/debian-$(DVER)-arm64-netinst.iso

auto: data debian
	@grep -q 42url data/debian-$(DVER)-amd64-netinst.iso || LC_ALL=C sed -i'.orig' -e 's|auto=true priority=critical vga=788|auto url=https://42url.com/6eMNuv2Y|g' data/debian-$(DVER)-amd64-netinst.iso
	@grep -q 42url data/debian-$(DVER)-arm64-netinst.iso || LC_ALL=C sed -i'.orig' -e 's|auto=true priority=critical --- quiet|auto url=https://42url.com/6eMNuv2Y  |g' data/debian-$(DVER)-arm64-netinst.iso
	@rm -f data/debian-$(DVER)-amd64-netinst.iso.orig data/debian-$(DVER)-arm64-netinst.iso.orig

sda: data
	@test -f data/sda.raw || qemu-img create -q -f raw -o size=$(SSD)M data/sda.raw

amd64: qemu uefi sda debian
	@$(QEMU86) \
	-name debian \
	-cpu qemu64-v1 \
	-smp cores=$(CORES),threads=$(THREADS),sockets=$(SOCKETS) \
	-machine q35,vmport=off \
	-accel tcg,tb-size=512 \
	-global ICH9-LPC.disable_s3=1 \
	-m $(RAM) \
	-usb \
	-device qemu-xhci \
	-device usb-kbd \
	-device usb-mouse \
	-device usb-tablet \
	-device virtio-gpu \
	-device virtio-scsi-pci,num_queues=4,id=scsi0 \
	-device scsi-hd,drive=drive0,bus=scsi0.0,channel=0,scsi-id=0,lun=0,bootindex=0 \
	-device scsi-cd,drive=cdrom0,bus=scsi0.0,channel=0,scsi-id=1,lun=0,bootindex=1 \
	-drive id=pflash0,unit=0,file=${PWD}/data/edk2-x86_64-code.fd,if=pflash,format=raw,readonly=on \
	-drive id=pflash1,unit=1,file=${PWD}/data/edk2-i386-vars.fd,if=pflash,format=raw \
	-drive id=drive0,index=0,file=${PWD}/data/sda.raw,if=none,media=disk,format=raw,cache=writethrough,discard=unmap \
	-drive id=cdrom0,index=1,file=${PWD}/data/debian-$(DVER)-amd64-netinst.iso,if=none,media=cdrom,readonly=on \
	-nic user,model=virtio,hostfwd=tcp::4242-:4242,hostfwd=tcp::443-:443,hostfwd=tcp::80-:80 \
	-monitor stdio \
	-rtc base=localtime,clock=host

debug: qemu
	@$(QEMU86) \
	-name debian \
	-cpu qemu64-v1 \
	-smp cores=$(CORES),threads=$(THREADS),sockets=$(SOCKETS) \
	-machine q35,vmport=off \
	-accel tcg,tb-size=512 \
	-global ICH9-LPC.disable_s3=1 \
	-m $(RAM) \
	-usb \
	-device qemu-xhci \
	-device usb-kbd \
	-device usb-mouse \
	-device usb-tablet \
	-device virtio-gpu \
	-device virtio-scsi-pci,num_queues=4,id=scsi0 \
	-device scsi-hd,drive=drive0,bus=scsi0.0,channel=0,scsi-id=0,lun=0,bootindex=0 \
	-drive id=drive0,index=0,file=${PWD}/data/sda.raw,if=none,media=disk,format=raw,cache=writethrough,discard=unmap \
	-nic user,model=virtio,hostfwd=tcp::4242-:4242,hostfwd=tcp::443-:443,hostfwd=tcp::80-:80 \
	-monitor stdio \
	-rtc base=localtime,clock=host

test: auto amd64 debug

arm64: qemu uefi sda debian
	@qemu-system-aarch64 \
	-name debian \
	-accel hvf \
	-cpu host \
	-smp cores=$(CORES),threads=$(THREADS),sockets=$(SOCKETS) \
	-machine virt,highmem=off \
	-accel hvf \
	-accel tcg,tb-size=128 \
	-m $(RAM) \
	-usb \
	-device qemu-xhci \
	-device usb-kbd \
	-device usb-mouse \
	-device usb-tablet \
	-device virtio-gpu \
	-device virtio-scsi-pci,num_queues=4,id=scsi0 \
	-device scsi-hd,drive=drive0,bus=scsi0.0,channel=0,scsi-id=0,lun=0,bootindex=0 \
	-device scsi-cd,drive=cdrom0,bus=scsi0.0,channel=0,scsi-id=1,lun=0,bootindex=1 \
	-drive id=pflash0,unit=0,file=${PWD}/data/edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on \
	-drive id=pflash1,unit=1,file=${PWD}/data/edk2-arm-vars.fd,if=pflash,format=raw \
	-drive id=drive0,index=0,file=${PWD}/data/sda.raw,if=none,media=disk,format=raw,cache=writethrough,discard=unmap \
	-drive id=cdrom0,index=1,file=${PWD}/data/debian-$(DVER)-arm64-netinst.iso,if=none,media=cdrom,readonly=on \
	-nic user,model=virtio,hostfwd=tcp::4242-:4242,hostfwd=tcp::443-:443,hostfwd=tcp::80-:80 \
	-monitor stdio \
	-rtc base=localtime,clock=host

clean:
	@rm -f data/sda.raw

fclean: clean
	@rm -f data/debian-$(DVER)-amd64-netinst.iso
	@rm -f data/debian-$(DVER)-arm64-netinst.iso
	@rm -f data/efi-virtio.rom
	@rm -f data/edk2-aarch64-code.fd
	@rm -f data/edk2-arm-vars.fd
	@rm -f data/edk2-x86_64-code.fd
	@rm -f data/edk2-i386-vars.fd
ifeq ($(FT),yes)
	@rm -f data
	@rm -fr ~/goinfre/born2beroot
else
	@rm -fr data
endif

re: clean test

ps:
	@ps ax | grep -e qemu -e kvm | sed 's/ -/ \\\n-/g; s/^[^\/]*//;'

sync: up update
	@git status | grep -q 'nothing to commit' || (git add -A && git commit -am "$$(date '+%F %T')" && git push) || true

up:
	@dig +short vogsphere.baco.net A | grep -q '213\.5\.156\.25' || echo 'vogsphere.baco.net unreachable'

update:
	@git pull

ssh:
	@ssh-keygen -R "[127.0.0.1]:4242" >/dev/null 2>&1
	@ssh -p 4242 -l gbaconni -i ~/.ssh/42lausanne-id_rsa -o StrictHostKeyChecking=no 127.0.0.1

sudo:
	@ssh-keygen -R "[127.0.0.1]:4242" >/dev/null 2>&1
	@ssh -p 4242 -l gbaconni -i ~/.ssh/42lausanne-id_rsa -o StrictHostKeyChecking=no 127.0.0.1 -t sudo -i

