TARGET = @TARGET@
SBINDIR = @SBINDIR@
INCDIR =  @INCDIR@
ETCDIR = @ETCDIR@
MANDIR = @MANDIR@

all: executer-mcctrl executer-mcoverlayfs executer-user mckernel mck-tools

executer-mcctrl:
	+@(cd executer/kernel/mcctrl; $(MAKE) modules)

executer-mcoverlayfs:
	+@(cd executer/kernel/mcoverlayfs; $(MAKE) modules)

executer-user:
	+@(cd executer/user; $(MAKE))

mckernel:
	+@case "$(TARGET)" in \
	    attached-mic | builtin-x86 | builtin-mic | smp-x86 | smp-arm64) \
		(cd kernel; $(MAKE)) \
		;; \
	    *) \
		echo "unknown target $(TARGET)" >&2 \
		exit 1 \
		;; \
	esac

mck-tools:
	+@(cd tools/mcstat; $(MAKE))

install:
	@(cd executer/kernel/mcctrl; $(MAKE) install)
	@(cd executer/kernel/mcoverlayfs; $(MAKE) install)
	@(cd executer/user; $(MAKE) install)
	@case "$(TARGET)" in \
	    attached-mic | builtin-x86 | builtin-mic | smp-x86 | smp-arm64) \
		(cd kernel; $(MAKE) install) \
		;; \
	    *) \
		echo "unknown target $(TARGET)" >&2 \
		exit 1 \
		;; \
	esac
	@case "$(TARGET)" in \
		smp-x86 | smp-arm64) \
		mkdir -p -m 755 $(SBINDIR); \
		install -m 755 arch/x86_64/tools/mcreboot-smp-x86.sh $(SBINDIR)/mcreboot.sh; \
		install -m 755 arch/x86_64/tools/mcstop+release-smp-x86.sh $(SBINDIR)/mcstop+release.sh; \
		install -m 755 arch/x86_64/tools/mcoverlay-destroy-smp-x86.sh $(SBINDIR)/mcoverlay-destroy.sh; \
		install -m 755 arch/x86_64/tools/mcoverlay-create-smp-x86.sh $(SBINDIR)/mcoverlay-create.sh; \
		install -m 755 arch/x86_64/tools/eclair-dump-backtrace.exp $(SBINDIR)/eclair-dump-backtrace.exp;\
		mkdir -p -m 755 $(ETCDIR); \
		install -m 644 arch/x86_64/tools/irqbalance_mck.service $(ETCDIR)/irqbalance_mck.service; \
		install -m 644 arch/x86_64/tools/irqbalance_mck.in $(ETCDIR)/irqbalance_mck.in; \
		mkdir -p -m 755 $(INCDIR); \
		install -m 644 kernel/include/swapfmt.h $(INCDIR); \
		mkdir -p -m 755 $(MANDIR)/man1; \
		install -m 644 arch/x86_64/tools/mcreboot.1 $(MANDIR)/man1/mcreboot.1; \
		;; \
	    *) \
		echo "unknown target $(TARGET)" >&2 \
		exit 1 \
		;; \
	esac
	@(cd tools/mcstat/; $(MAKE) install)

clean:
	@(cd executer/kernel/mcctrl; $(MAKE) clean)
	@(cd executer/kernel/mcoverlayfs; $(MAKE) clean)
	@(cd executer/user; $(MAKE) clean)
	@case "$(TARGET)" in \
	    attached-mic | builtin-x86 | builtin-mic | smp-x86 | smp-arm64) \
		(cd kernel; $(MAKE) clean) \
		;; \
	    *) \
		echo "unknown target $(TARGET)" >&2 \
		exit 1 \
		;; \
	esac
	@(cd tools/mcstat; $(MAKE) clean)
