SRC_DIR := src
SRC := $(wildcard $(SRC_DIR)/*.c)

BUILD_DIR := build

ASSETS := assets
TARGET := $(BUILD_DIR)/hello

INCLUDE := /usr/include/

CTOOLS := $(SRC_DIR)/ctools
LIBFT_DIR := $(CTOOLS)/libft
LIBFT := $(LIBFT_DIR)/lib/libft.a

LIB := $(LIBFT) $(LIBGPIOD)

TOOLCHAIN_DIR := $(HOME)/.local/toolchains
TOOLCHAIN := $(TOOLCHAIN_DIR)/aarch64-linux-musl-cross
TOOLCHAIN_BIN := $(TOOLCHAIN)/bin
MUSL_LD := $(TOOLCHAIN_BIN)/aarch64-linux-musl-ld
CC := $(TOOLCHAIN_BIN)/aarch64-linux-musl-gcc

LIBGPIOD_INCLUDE := $(TOOLCHAIN)/include
LIBGPIOD := $(TOOLCHAIN)/lib/libgpiod.a

CFLAGS := -Wall -Wextra -Werror -I$(LIBGPIOD_INCLUDE)

DOCKER_CMD := /bin/sh #-c 'hello "host.docker.internal" "8000"'

MINIROOTFS := $(ASSETS)/minirootfs.tar.gz


LINKER_FLAGS := -L$(TOOLCHAIN)/lib -lgpiod

ALPINE := alpine
ALPINE_KERNEL := alpine/boot/vmlinuz-rpi
ALPINE_INITFRAM_FS := alpine/boot/initramfs-rpi
ALPINE_BIN := $(ALPINE)/bin
ALPINE_TAR := $(ASSETS)/alpine-rpi-aarch64.gz
ALPINE_HEADLESS := $(ASSETS)/headless.apkovl.tar.gz

ALPINE_CUSTOM_IMG := $(BUILD_DIR)/alpine-custom.img

PLATFORM := arm64
QEMU_VM := qemu-system-aarch64
QEMU_IMG := qemu-img

SSH_AUTH_KEYS := $(ALPINE)/authorized_keys

all: $(TARGET)

$(TARGET): $(SRC) $(BUILD_DIR) $(LIB) $(LIBGPIOD)
	$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LIB) $(LIBGPIOD)

get: $(ASSETS) $(ALPINE_TAR) $(ALPINE_HEADLESS)
	rm -rf $(ALPINE)/*
	tar xvf $(ALPINE_TAR) -C $(ALPINE)
	cp $(ALPINE_HEADLESS) $(ALPINE)

sync: $(TARGET)
	rsync -avz root@pi:/media/mmcblk0p1/ ./image
	rsync -avz ./build/* root@pi:/root/build/

img: get $(ALPINE) $(BUILD_DIR)
	$(QEMU_IMG) create -f raw $(ALPINE_CUSTOM_IMG) 512M
	mformat -i $(ALPINE_CUSTOM_IMG) -F ::
	mcopy -i $(ALPINE_CUSTOM_IMG) -s $(ALPINE)/* ::

$(SSH_AUTH_KEYS):
	cp ~/.ssh/authorized_keys $(ALPINE)

bootable: $(ALPINE) $(WPA_CONFIG) get
	tar czf $(MINIROOTFS) -C $(ALPINE) ./

$(ALPINE_HEADLESS):
	wget https://github.com/macmpi/alpine-linux-headless-bootstrap/raw/98728731299f262eab270941c522138d6c5cea73/headless.apkovl.tar.gz -O $(ALPINE_HEADLESS)

$(LIBGPIOD): $(TOOLCHAIN)
	rm -f $(LIBGPIOD)
	git clone https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git && \
	cd libgpiod && \
	export ac_cv_func_malloc_0_nonnull=yes && \
	export ac_cv_func_realloc_0_nonnull=yes && \
	./autogen.sh && \
	./configure --host=aarch64-linux-musl \
				--prefix=$(TOOLCHAIN) \
				--enable-tools=no \
				--enable-bindings-cxx=no \
				--disable-shared \
				--enable-static && \
	make && make install && \
	cd .. && \
	rm -rf libgpiod


$(TOOLCHAIN): | $(TOOLCHAIN_DIR)
	wget https://musl.cc/aarch64-linux-musl-cross.tgz
	tar xvf aarch64-linux-musl-cross.tgz -C $(TOOLCHAIN_DIR)
	rm aarch64-linux-musl-cross.tgz

$(TOOLCHAIN_DIR):
	mkdir -p $@

$(ALPINE_TAR):
	wget https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/aarch64/alpine-rpi-3.22.1-aarch64.tar.gz -O $(ALPINE_TAR)

$(LIB):
	make aarch64 -C $(LIBFT_DIR)

$(BUILD_DIR):
	mkdir -p $@

$(ALPINE):
	mkdir -p $@

$(ASSETS):
	mkdir -p $@

clean:
	rm -rf $(BUILD_DIR) $(ALPINE) $(ASSETS)
	make fclean -C $(LIBFT_DIR)

.PHONY: sync
