# This just makes sure that GNU make is being used, and not BSD make
#if exists(/notafile)
ifeq ' != ''0'
#else
.error Please use GNU make
#endif
endif

# ---- PROJECT SETUP ----
# Project name
NAME := shell

# Turn on -Wall
CFLAGS += -Wall

# Set the compiler

# ---- DIRECTORY SETUP ----
ifneq ($(findstring cs.rpi.edu,$(shell hostname)),)
# Set up my directories
CFLAGS += -I/cs/kantrn/local/include
LDFLAGS += -L/cs/kantrn/local/lib

# Set up the local system directories
CFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib

# Fix the shared linker search path
LDFLAGS += -rpath /cs/kantrn/local/lib

endif

# ---- FILE LISTS ----
# This is some magic to extract the file list from the manifest in the README
define GETFILES_PL
while(<>) { if(/^Manifest/i) { last; } } while(<>) { if(/^\s*$/) { last; } /^(.+):/; push @f,$1; } print "@f";
endef
#FILES := ${shell perl -e '${value GETFILES_PL}' README}
FILES := $(wildcard *.c *.h) README Makefile leakcheck.sh reproto.pl MANIFEST
C_FILES := $(filter %.c,$(FILES))
H_FILES := $(filter %.h,$(FILES))

# ---- DEFAULT BUILD TARGETS ----
# Default target
all: $(if $(wildcard .debug),debug,$(if $(wildcard .trace),trace,$(NAME)))

# Build a version that shows more debugging output
debug: CPPFLAGS += -DEBUG
debug: CFLAGS += -g
debug: $(NAME) 
	@touch .debug

# Build a version that shows lots of debugging output
trace: CPPFLAGS += -DEBUG=2
trace: CFLAGS += -g
trace: $(NAME)
	@touch .trace

$(NAME): $(C_FILES:.c=.o)

# ---- OTHER USEFUL TARGETS
.PHONY: all debug trace clean distclean dist nuke info

# Remove object files
clean:
	-rm -f *.o $(NAME) .debug .trace
	-rm -rf disttemp*

# Remove all files not mentioned in the manifest
distclean: clean
	$(foreach file,$(wildcard *),$(if $(findstring $(file),$(FILES)),,rm -f $(file); ))

PID := $(shell echo $$$$)

# Package all files in the manifest into a tarball
dist:
	mkdir -p disttemp$(PID)/${NAME}
	cp $(filter-out $(NAME).tar.gz,$(FILES)) disttemp$(PID)/$(NAME)/
	tar czf ${NAME}.tar.gz -C disttemp$(PID) $(NAME)
	rm -rf disttemp$(PID)

# Remove all files related to the project
nuke: $(if $(wildcard *.o),clean)
ifeq ($(wildcard .nuke),)
	@echo "Warning: This will remove all source files"
	@echo "If you really mean to do this, please run 'make nuke' again"
	@touch .nuke
else
	rm -f ${FILES} ${NAME} ${NAME}.tar.gz .nuke
	@echo "Files removed"
endif

# Check the value of certain variables
info:	
	@echo "CFLAGS = '$(CFLAGS)'"
	@echo "CPPFLAGS = '$(CPPFLAGS)'"
	@echo "LDFLAGS = '$(LDFLAGS)'"
	@echo "FILES = '$(FILES)'"
	@echo "C_FILES = '$(C_FILES)'"
	@echo "H_FILES = '$(H_FILES)'"

# ---- DEPENDENCY CREATION ----
# Automatically build dependency files
%.d: %.c
	@set -e; rm -f $@; \
	$(CC) -MM $(CPPFLAGS) $(CFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

# Include all of the needed dependency files
include $(C_FILES:.c=.d)


# ---- NEEDED LIBRARIES ----
LDFLAGS += -lreadline -lcurses -lpcre -lxxl -lgc

ifeq ($(shell hostname),alfred.kantrn.stu.rpi.edu)
LDFLAGS += -lpthread
endif