BOLD_ECHO = @ tput bold; echo $1; tput sgr0
SKELETIZE := ../scripts/generate-skeletons.py

EX_C = $(foreach x,$(wildcard $1/*.c),$(notdir $x))

EX_IN := $(wildcard Exercices/*/*.c)
EX_OUT := $(foreach e,$(EX_IN:Exercices/%=Chamilo/%),$(dir $e)exos/$(notdir $e))

# Generate the student archives
all:
	$(call BOLD_ECHO, "Compilation des sujets...")
	@ $(MAKE) -C Sujet

	$(call BOLD_ECHO, "Nettoyage des sorties existantes...")
	@ $(RM) -r Chamilo
	@ mkdir -p Chamilo/TP1/exos Chamilo/TP23/exos
	@ mkdir -p Chamilo/TP1/assembler/tests
	@ mkdir -p Chamilo/TP23/emulator/tests
	@ echo done

	$(call BOLD_ECHO, "Génération des exercices...")
	@ $(MAKE) --no-print-directory chamilo_ex

	$(call BOLD_ECHO, "Génération des codes étudiants TP1...")
	@ $(SKELETIZE) Vprof/assembler/main.c --output-file Chamilo/TP1/assembler/main.c
	cp Divers/Makefile.asm Chamilo/TP1/assembler/Makefile
	cp Divers/README-etudiants-asm.md Chamilo/TP1/assembler/README.md
	cp Vprof/test.py Chamilo/TP1/assembler
	cp Vprof/tests/units/add-simple.s Chamilo/TP1/assembler/tests

	$(call BOLD_ECHO, "Génération des codes étudiants TP23...")
	@ $(SKELETIZE) Vprof/emulator/main.c --output-file Chamilo/TP23/emulator/main.c
	@ $(SKELETIZE) Vprof/emulator/emulator.h --output-file Chamilo/TP23/emulator/emulator.h
	@ $(SKELETIZE) Vprof/emulator/machine.c --output-file Chamilo/TP23/emulator/machine.c
	@ $(SKELETIZE) Vprof/emulator/emulate.c --output-file Chamilo/TP23/emulator/emulate.c
	cp Divers/Makefile.emu Chamilo/TP23/emulator/Makefile
	cp Divers/README-etudiants-emu.md Chamilo/TP23/emulator/README.md
	cp Vprof/test.py Chamilo/TP23/emulator
	cp Vprof/tests/units/add-simple.s Chamilo/TP23/emulator/tests

	$(call BOLD_ECHO, "Génération du programme mystère TP23...")
	@ $(MAKE) -C Vprof/tests-extra/logo
	cp Vprof/tests-extra/logo/blob.hex Chamilo/TP23/emulator

	$(call BOLD_ECHO, "Copie des divers documents...")
	cp Sujet/IN330_2024_TP1.pdf          Chamilo
	cp Sujet/IN330_2024_TP23.pdf         Chamilo
	cp Docs/doc_riscv_projetC.pdf        Chamilo
	cp Divers/IN330_2024_PlanningTP.pdf  Chamilo

	$(call BOLD_ECHO, "Génération des archives...")
	(cd Chamilo && zip -qr IN330_2024_TP1.zip TP1)
	(cd Chamilo && zip -qr IN330_2024_TP23.zip TP23)

	$(call BOLD_ECHO, "Génération des documents annexes...")
	@ $(MAKE) -C SlidesIntro
	# TODO: docs (generates lots of output)

chamilo_ex: $(EX_OUT)

Chamilo/TP1/exos/%.c: Exercices/TP1/%.c
	@ $(SKELETIZE) $< --output-file $@
Chamilo/TP23/exos/%.c: Exercices/TP23/%.c
	@ $(SKELETIZE) $< --output-file $@
