##############################################################################
# (c) SZABO Gergely, 2009
# Free software, distributed under the GNU GPL v3 license
# There is absolutely no warranty.
##############################################################################

####### Macros #######
SHELL := /bin/bash

# Target and sources
BIN    := bin
WIN    := win

# Windows/Unix targets compiled on Windows/Unix
WARGET := ogc.exe
ifdef SYSTEMROOT
TARGET := ogc.exe
else
TARGET := ogc
endif

# Defines
ifneq "$(VARS)" "no"
CDEFS  := -DVARS
endif

# Unix targets compiled on both Windows/Unix
CC     := gcc
CFLAGS := -lm -Wno-format -Wmissing-prototypes $(CDEFS) -o $(BIN)/$(TARGET)
WFLAGS := -lm -Wno-format -Wmissing-prototypes $(CDEFS) -o $(WIN)/$(WARGET)

# Windows targets compiled on Windows/Unix
ifdef SYSTEMROOT
WC     := gcc
WLIBS  := -mno-cygwin
else
WC     := i586-mingw32msvc-gcc
WLIBS  := -L/usr/i586-mingw32msvc/lib -I/usr/i586-mingw32msvc/include
endif

# Sources
CSRC   := ogc.c grammar.c output.c tNum.c patterns.c arg1/arg1.c
HSRC   := ogc.h output.h tNum.h arg1/arg1.h

ifneq "$(VARS)" "no"
CSRC   += vars.c
HSRC   += vars.h
endif

TMPF   := patterns.c grammar.c grammar.h tNumTest arg1/usage.h arg1/version.h *~

MANPAGE:= ogc.1.gz
MAN    := /usr/share/man/man1

TARBALL:= ogc.tar.gz

####### Rules ########
.PHONY: target arg1 tnum install uninstall clean commit tarball release

target: arg1 $(BIN)/$(TARGET) $(WIN)/$(WARGET)

# Compile target
$(BIN)/$(TARGET) $(WIN)/$(WARGET): $(CSRC) $(HSRC) makefile usage.txt version.txt
	@if [ ! -d $(BIN) ]; then mkdir $(BIN); fi
	$(CC) $(CFLAGS) $(CSRC)
	@if [ ! -d $(WIN) ]; then mkdir $(WIN); fi
	$(WC) $(WFLAGS) $(WLIBS) $(CSRC);

# Recursive make for the arg1 subdir
arg1:
	$(MAKE) -C arg1

# tNum test-suite
tnum:
	@gcc -Wno-format -o tNumTest tNum.c tNumTest.c

# c/h from y (yacc)
%.c: %.y
	bison -y -d -o $@ $<

# c from l (lex)
%.c: %.l
	flex -l -o $@ $<

# Install to c/cygdrive/WINDOWS /usr/bin
install:
	@cp -f $(BIN)/$(TARGET) /usr/bin;
	@cp -f $(MANPAGE) $(MAN);
	@if [ -n "$(SYSTEMROOT)" ];                        \
	  then cp -f $(WIN)/$(TARGET) /cygdrive/c/WINDOWS; \
	fi

# Uninstall from c/cygdrive/WINDOWS /usr/bin
uninstall:
	@rm -f /usr/bin/$(TARGET);
	@rm -f $(MAN)/$(MANPAGE);
	@if [ -n "$(SYSTEMROOT)" ];                        \
	  then rm -f /cygdrive/c/WINDOWS/$(TARGET);        \
	fi

# Remove all generated files
clean:
	@rm -fr $(BIN) $(WIN) $(TMPF) $(TARBALL)

# Commit to git repository
commit: clean
	@if [ "`git diff --no-ext-diff HEAD`" ]; \
	  then git commit -a;     \
	fi

# Create tarball for distribution
tarball: $(TARBALL)
$(TARBALL):
	7z a $(TARBALL:.tar.gz=.tar) *
	$(MAKE) target
	7z a $(TARBALL:.tar.gz=.tar) win bin
	7z a $(TARBALL) $(TARBALL:.tar.gz=.tar)
	rm -f $(TARBALL:.tar.gz=.tar)

# Tag HEAD and Create compressed tarball
release: commit
	@echo 'Chose old tag to follow: '; \
	select OLD in `git tag`; do break; done; \
	export TAG; \
	read -p 'Please Enter new tag name: ' TAG; \
	sed -r -e "s/version\s..*$$/version $$TAG/" \
	       -e 's/^(\(c\).+, [0-9]{4}).*$$/\1-'"`date +%Y`/" \
	       -i version.txt || exit 1; \
	git commit -a -m "version $$TAG"; \
	echo Adding git tag $$TAG; \
	git log --pretty=format:"%h %an %s" $$OLD.. > Release.txt; \
	$$EDITOR Release.txt; \
	git tag -a -F Release.txt $$TAG HEAD; \
	rm release.txt; \
	$(MAKE) tarball

