PDFLATEX  = /usr/bin/pdflatex
BIBTEX    = /usr/bin/bibtex
RM        = /bin/rm -f

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
#check if pdflatex is installed
ifeq (, $(shell which pdflatex))
	$(error "No pdflatex in $(PATH)")
endif
	PDFLATEX = pdflatex
endif

INFILE  = documentation

INPUT   = $(INFILE).tex
OUTPUT  = $(INFILE).pdf
PLUGINS = $(wildcard ../*/doc/*.tex)
TEMP    = $(wildcard *.aux *.bbl *.blg *.log *.out *.svn *.toc *~)

all: plugins_list.tex
ifeq ($(wildcard *.toc),)
	$(PDFLATEX) $(INPUT)
endif
	$(PDFLATEX) $(INPUT)
	$(PDFLATEX) $(INPUT)

plugins_list.tex: clean_plugins_list_tex
	for p in $$(grep "\\documentclass\[documentation\]{subfiles}" ../*/doc/*.tex | cut -d: -f1 | grep -v tranalyzer2 | grep -v t2PSkel | grep -v scripts | xargs); do \
		echo "\_ransubfile{$$p}" | sed 's/_/t/' >> plugins_list.tex; \
	done

clean_plugins_list_tex:
	@if [ -f "plugins_list.tex" ]; then \
		$(RM) "plugins_list.tex"; \
	fi

clean: clean_plugins_list_tex
ifneq ($(TEMP),)
	$(RM) $(TEMP)
endif

clean_all: clean
	$(RM) $(OUTPUT)
