#!/usr/bin/make -f

# for debian/reform-tools.install and for dkms
include /usr/share/dpkg/pkg-info.mk
export DEB_VERSION_UPSTREAM

%:
	dh $@

execute_after_dh_auto_build:
	set -e;                                                               \
	tool2whatis () { case $$1 in                                          \
		reform-boot-config)  echo "choose rootfs to boot from" ;;     \
		reform-chat)         echo "chat on #mnt-reform" ;;            \
		reform-check)        echo "check your setup" ;;               \
		reform-compstat)     echo "system statistics for waybar" ;;   \
		reform-config)       echo "select keyboard and timezone" ;;   \
		reform-display-config) echo "select single/dual display" ;;   \
		reform-flash-rescue) echo "flash rescue image to eMMC" ;;     \
		reform-flash-uboot)  echo "download and flash u-boot" ;;      \
		reform-handbook)     echo "show Reform Handbook" ;;           \
		reform-help)         echo "help with MNT Reform" ;;           \
		reform-hw-setup)     echo "perform hardware tweaks" ;;        \
		reform-migrate)      echo "copy rootfs to device" ;;          \
		reform-mcu-tool)     echo "manage microcontrollers" ;;        \
		reform-pavucontrol)  echo "kill and restart pavucontrol" ;;   \
		reform-setup-encrypted-nvme) echo "setup encrypted SSD" ;;    \
		reform-standby)      echo "suspend/wakeup tweaks" ;;          \
		*) echo "unknown tool: $$1" 2>&1; exit 1 ;;                   \
	esac; };                                                              \
	for tool in bin/* sbin/*; do                                          \
		sec="$$(case $$tool in sbin/*) echo 8;; *) echo 1;; esac)";   \
		env "PATH=$$(dirname "$$tool"):$$PATH" help2man               \
			--section=$$sec                                       \
			--name="$$(tool2whatis "$$(basename "$$tool")")"      \
			--no-info --version-string=$(DEB_VERSION_UPSTREAM)    \
			--no-discard-stderr "$$(basename "$$tool")"           \
			--output="debian/$$(basename "$$tool").$$sec";        \
	done

execute_after_dh_auto_test:
	clang-format lpc/reform2_lpc.c | diff -u lpc/reform2_lpc.c -
	shfmt --posix --simplify --binary-next-line --case-indent --indent 2 --diff bin sbin kernel/* initramfs-tools/*/* flash-kernel/*/*
	black --check --diff bin/reform-compstat libexec/reform-tools/reform-tray.py libexec/reform-tools/reform-wallpaper.py
	black --line-length 120 --check --diff sbin/reform-mcu-tool

override_dh_installsystemd:
	dh_installsystemd --package=reform-tools --name=reform-hw-setup
	dh_installsystemd --package=reform-tools --no-stop-on-upgrade --no-start --name=reform-sleep

override_dh_dkms:
	dh_dkms -V $(DEB_VERSION_UPSTREAM)
