all:
	@echo "Usage: make <commande>"
	@echo ""
	@echo "Commandes disponibles:"
	@echo "- worm_inc : version modifiée des programmes, avec insertion de detect_init()"
	@echo "              et bibliothèque de détection de pannes liée statiquement"
	@echo "- worm_shared : version modifiée des programmes, avec insertion de detect_init()"
	@echo "                 et bibliothèque de détection de pannes liée dynamiquement"
	@echo "- worm_preload: version non modifiée des programmes, avec préchargement de la "
	@echo "                 bibliothèque de détection de pannes, et execution de celle-ci."
	
worm_inc: clean server
	gcc -ggdb -o worms_mod libdetect.c worms_mod.c -lcurses -lrt -lpthread -Wall
	gcc -ggdb -o test_mod libdetect.c test_mod.c  -lrt -lpthread -Wall
	@echo -e "\n*  Pour executer, taper: "
	@echo -e "*\n*    ./worms_mod"
	@echo -e "*\n*  ou "
	@echo -e "*\n*    ./test_mod\n*"

worm_shared: clean server
	gcc -fPIC -Wall -ggdb -c libdetect.c
	gcc -g -shared -Wl,-soname,libdetect.so.0 \
	    -o libdetect.so.0.0 libdetect.o -lc
	/sbin/ldconfig -n .
	ln -sf libdetect.so.0 libdetect.so
	gcc -Wall -ggdb -o test_shr test_mod.c -L. -lrt -lpthread -ldetect
	gcc -Wall -ggdb -o worms_shr worms_mod.c -L. -lcurses -lrt -lpthread -ldetect
	@echo -e "\n*  Pour executer, taper: "
	@echo -e "*\n*    LD_LIBRARY_PATH="." ./worms_shr"
	@echo -e "*\n*  ou "
	@echo -e "*\n*    LD_LIBRARY_PATH="." ./test_shr\n*"

# run with:
#   LD_LIBRARY_PATH="." ./command


worm_preload: clean server
	gcc -fPIC -Wall -ggdb -c libdetect.c  -D_GYR_PRELOAD
	gcc -g -shared -Wl,-soname,libdetect.so.0 \
	    -o libdetect.so.0.0 libdetect.o -lc
	/sbin/ldconfig -n .
	ln -sf libdetect.so.0 libdetect.so
	gcc -ggdb -o worms worms.c -lcurses -lrt -lpthread -Wall
	gcc -ggdb -o test test.c  -lrt -lpthread -Wall
	@echo -e "\n*  Pour executer, taper: "
	@echo -e "*\n*    LD_LIBRARY_PATH="." LD_PRELOAD=libdetect.so ./worms"
	@echo -e "*\n*  ou "
	@echo -e "*\n*    LD_LIBRARY_PATH="." LD_PRELOAD=libdetect.so ./test\n*"

server:
	gcc -ggdb -o detector-server detector-server.c -Wall

clean:
	rm -f *.so*
	rm -f *.o
	rm -f detector-server
	rm -f test test_mod test_shr
	rm -f worms worms_mod worms_shr