#!/usr/bin/make -f

BOOTSCR = /boot/boot.scr

SRC =  $(wildcard src/*.conf)

DIS =  $(wildcard disable-*)
ENA += $(wildcard enable-*)

NEG =  $(subst enable-,%disable-,$(ENA))
NEG += $(subst disable-,%enable-,$(DIS))
NCONF = $(foreach conf,$(NEG), $(conf).conf)
FSRC = $(sort $(filter-out $(NCONF),$(SRC)))

.PHONY:	all force enable-% disable-%

all: $(BOOTSCR)


help:
	@echo ""
	@echo "	make          --- Generate boot.scr file if is needed"
	@echo "	make force    --- Generate boot.scr file unconditionally"
	@echo ""
	@echo "	make enable-wifi|enable-bluetooth    --- Enable device"
	@echo "	make disable-wifi|disable-bluetooth  --- Disable device"
	@echo ""



$(BOOTSCR): $(SRC)
	@sed '/^[[:blank:]]*\(#[^|].*\)*$$/d' $(FSRC) > /tmp/boot.cmd
	@mkimage -C none -A arm -T script -d /tmp/boot.cmd $@
	@rm -f /tmp/boot.cmd


force:
	@$(MAKE) -B all

enable-%:
	@rm -f $(subst enable-,disable-,$@)
	@find src -name \*$@.conf -exec touch $@ \;
	@find src -name \*$(subst enable-,disable-,$@).conf -exec touch $@ \;
	@$(MAKE) force

disable-%:
	@rm -f $(subst disable-,enable-,$@)
	@find src -name \*$@.conf -exec touch $@ \;
	@find src -name \*$(subst disable-,enable-,$@).conf -exec touch $@ \;
	@$(MAKE) force
