# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2021/11/02 17:47:57 by gbaconni          #+#    #+#              #
#    Updated: 2021/11/04 08:46:53 by gbaconni         ###   lausanne.ch        #
#                                                                              #
# **************************************************************************** #

DVER = 11.1.0

CPU = 8
RAM = 2048
SSD = 31539

all: sync

virtualbox:
	@test -d ~/goinfre/VirtualBox\ VMs || mkdir -p ~/goinfre/VirtualBox\ VMs
	@ln -snf ~/goinfre/VirtualBox\ VMs ~/VirtualBox\ VMs

brew:
	@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-img || (eval "$$(/opt/homebrew/bin/brew shellenv)" && brew install qemu)

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

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

sda:
	@test -f sda.raw || dd if=/dev/zero of=sda.raw count=0 bs=1m seek=$(SSD) >/dev/null 2>&1

uefi:
	@test -f efi-virtio.rom || cp /opt/homebrew/Cellar/qemu/*/share/qemu/efi-virtio.rom efi-virtio.rom
	@test -f edk2-aarch64-code.fd || cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-aarch64-code.fd edk2-aarch64-code.fd
	@test -f edk2-arm-vars.fd || cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-arm-vars.fd edk2-arm-vars.fd
	@test -f edk2-x86_64-code.fd || cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-x86_64-code.fd edk2-x86_64-code.fd
	@test -f edk2-i386-vars.fd || cp /opt/homebrew/Cellar/qemu/*/share/qemu/edk2-i386-vars.fd edk2-i386-vars.fd

amd64: sda debian uefi
	@qemu-system-x86_64 \
	-name debian \
	-cpu qemu64-v1 \
	-smp $(CPU) \
	-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}/edk2-x86_64-code.fd,if=pflash,format=raw,readonly=on \
	-drive id=pflash1,unit=1,file=${PWD}/edk2-i386-vars.fd,if=pflash,format=raw \
	-drive id=drive0,index=0,file=${PWD}/sda.raw,if=none,media=disk,format=raw,cache=writethrough,discard=unmap \
	-drive id=cdrom0,index=1,file=${PWD}/debian-$(DVER)-amd64-netinst.iso,if=none,media=cdrom,readonly=on \
	-nic user,model=virtio \
	-monitor stdio \
	-rtc base=localtime,clock=host

debug:
	@qemu-system-x86_64 \
	-name debian \
	-cpu qemu64-v1 \
	-smp $(CPU) \
	-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}/sda.raw,if=none,media=disk,format=raw,cache=writethrough,discard=unmap \
	-nic user,model=virtio \
	-monitor stdio \
	-rtc base=localtime,clock=host

test: auto amd64 debug

arm64: sda debian uefi
	@qemu-system-aarch64 \
	-name debian \
	-accel hvf \
	-cpu host \
	-smp $(CPU) \
	-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}/edk2-aarch64-code.fd,if=pflash,format=raw,readonly=on \
	-drive id=pflash1,unit=1,file=${PWD}/edk2-arm-vars.fd,if=pflash,format=raw \
	-drive id=drive0,index=0,file=${PWD}/sda.raw,if=none,media=disk,format=raw,cache=writethrough,discard=unmap \
	-drive id=cdrom0,index=1,file=${PWD}/debian-$(DVER)-arm64-netinst.iso,if=none,media=cdrom,readonly=on \
	-nic user,model=virtio \
	-monitor stdio \
	-rtc base=localtime,clock=host

clean:
	@rm -f sda.raw

fclean: clean
	@rm -f debian-$(DVER)-amd64-netinst.iso
	@rm -f debian-$(DVER)-arm64-netinst.iso

re: clean test

ps:
	@ps ax | grep qemu | 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

