# Set QMK_HOME in your environment variables to adjust the path of the firmware
QMK_HOME ?= ${HOME}/qmk_firmware

# QMK keyboard information (author, keyboard, revision)
QMK_KEYBOARD_AUTHOR := binarydiv
QMK_KEYBOARD_NAME := eepyboard
QMK_KEYBOARD_REV ?= rev1

# Directory structure for QMK keyboard files
QMK_KEYBOARD_AUTHOR_DIR := ${QMK_KEYBOARD_AUTHOR}
QMK_KEYBOARD_BASE_DIR := ${QMK_KEYBOARD_AUTHOR}/${QMK_KEYBOARD_NAME}
QMK_KEYBOARD_REV_DIR := ${QMK_KEYBOARD_AUTHOR}/${QMK_KEYBOARD_NAME}/${QMK_KEYBOARD_REV}

# Keymap to use when compiling QMK firmware
QMK_KEYMAP ?= default

# Set default make target
.DEFAULT_GOAL := qmk-compile


# -- QMK commands

# Prerequisite: Check if QMK is installed
.PHONY: _qmk-check-firmware-installed
_qmk-check-firmware-installed:
	@test -d "${QMK_HOME}" || { \
		echo "QMK firmware not found! (QMK_HOME=${QMK_HOME})"; \
		echo "Please install QMK using 'qmk setup' first or set QMK_HOME in environment."; \
		exit 1; \
  	}

# Create symlink in QMK firmware directory to the keyboard files in qmk_keyboards/
.PHONY: _qmk-create-symlink
_qmk-create-symlink: _qmk-check-firmware-installed ${QMK_HOME}/keyboards/${QMK_KEYBOARD_BASE_DIR}

# Only create symlink if it doesn't exist already
${QMK_HOME}/keyboards/${QMK_KEYBOARD_BASE_DIR}:
	mkdir -p "${QMK_HOME}/keyboards/${QMK_KEYBOARD_AUTHOR_DIR}"
	ln -s "$$(realpath qmk_keyboards/${QMK_KEYBOARD_BASE_DIR})" "${QMK_HOME}/keyboards/${QMK_KEYBOARD_BASE_DIR}"

# Compile QMK firmware
.PHONY: qmk-compile
qmk-compile: _qmk-create-symlink
	qmk compile -kb ${QMK_KEYBOARD_REV_DIR} -km ${QMK_KEYMAP}

# Compile and flash QMK firmware to keyboard
.PHONY: qmk-flash
qmk-flash: _qmk-create-symlink
	qmk flash -kb ${QMK_KEYBOARD_REV_DIR} -km ${QMK_KEYMAP}

# Run "qmk config" to set this keyboard as the default (to simplify running qmk commands manually)
.PHONY: qmk-activate
qmk-activate: _qmk-create-symlink
	qmk config user.keyboard=${QMK_KEYBOARD_REV_DIR} user.keymap=${QMK_KEYMAP}
