# Semana 13: RAID y la interfaz de archivos.
#   make         compila todo
#   make test    arma un árbol de prueba en /tmp/w13 y corre cada programa
#   make clean   borra los binarios
CFLAGS = -Wall -Wextra -g
PROGS  = raid5 shareoff atomic mystat myls mytail myfind
# Las soluciones aún no publicadas (code/.ocultos) no están en el repositorio:
# se omiten los programas sin fuente y sus líneas en las pruebas.
PROGS := $(foreach p,$(PROGS),$(if $(wildcard $(p).c),$(p)))
RUN    = $(if $(wildcard $(1).c),./$(1),@: sin $(1).c)
T      = /tmp/w13

all: $(PROGS)

$(PROGS): %: %.c
	$(CC) $(CFLAGS) -o $@ $<

# Árbol de prueba: archivo, enlace duro, enlace simbólico, enlace roto,
# subdirectorios y un archivo de 2000 líneas.
setup:
	rm -rf $(T)
	mkdir -p $(T)/docs/notas $(T)/src
	seq -f "línea %g" 1 2000 > $(T)/log.txt
	echo hola > $(T)/docs/a.txt
	ln $(T)/docs/a.txt $(T)/docs/b.txt
	ln -s a.txt $(T)/docs/sim
	ln -s nada.txt $(T)/docs/roto
	touch $(T)/src/main.c $(T)/src/util.c $(T)/docs/notas/clase.md
	chmod 600 $(T)/docs/notas/clase.md

test: $(PROGS) setup
	./raid5 2
	./shareoff $(T)/off.txt
	$(call RUN,mystat) $(T)/docs/a.txt $(T)/docs/sim
	$(call RUN,mystat) $(T)/docs
	$(call RUN,myls) $(T)/docs
	$(call RUN,myls) -l $(T)/docs
	$(call RUN,mytail) -3 $(T)/log.txt
	$(call RUN,myfind) $(T)
	$(call RUN,myfind) $(T) -name '*.c'
	$(call RUN,myfind) $(T) -type l
	./atomic $(T)/config.txt "version 1"
	./atomic $(T)/config.txt "version 2" crash || true
	cat $(T)/config.txt
	ls $(T)
	./atomic $(T)/config.txt "version 2"
	cat $(T)/config.txt

clean:
	rm -f $(PROGS)

.PHONY: all setup test clean
