# uncomment and/or set to match processor 
# ** ONLY IF ** automatic determination of these compile flags is
# not working properly. uncommenting these variables will override 
# automatic determination of them
#ARCH = native
#TUNE = native

# uncomment the following line if you *DO NOT* want MySQL compiled in
#NOMYSQL = -DnoMySQL

# Normally no futher modifications necessary past here, an exception 
# may be if cross compiling 

CC = gcc
CFLAGS = -O2 -Wall -I/usr/include -I./include -fmessage-length=0 -D_FORTIFY_SOURCE=2 -g -DPIC -fPIC -DUNDEF_HAVE_INITGROUPS -fno-strict-aliasing
#LDFLAGS = -shared
OFILES = main.o dhandler.o
HMAIN = include/main.h include/dhandler.h include/names.h
HDH = include/dhandler.h include/names.h
HMYH = include/dhandler.h include/names.h include/mySQLhandler.h include/mySQLnames.h

BIT := $(shell getconf LONG_BIT)
BIT64 = -Dbit$(BIT)
CFLAGS += $(BIT64)

ifdef NOMYSQL
CFLAGS += $(NOMYSQL)
LDFLAGS += -lm
else
CFLAGS += $(shell mysql_config --cflags )
OFILES += mySQLhandler.o
ifeq ($(BIT),64)
LDFLAGS += $(shell mysql_config --libs | awk '{for (i =1; i <= NF; i++) print $$i}' | sed -e 's/^-L\/usr\/lib$$//g' | awk '{for (i =1; i <= NF; i++) printf $$i" "}' )
else
LDFLAGS += $(shell mysql_config --libs )
endif
HMAIN += include/mySQLhandler.h
HDH += include/mySQLhandler.h
endif

INSTALLSITEMAN1DIR = ${mandir}/man1
SHARESITEMAN1DIR = ${prefix}/share
DISTMAN1DIR = docs/man
SHAREDIR = docs/share
bindir = ${exec_prefix}/bin
datarootdir = ${prefix}/share
exec_prefix = ${prefix}
mandir = ${datarootdir}/man
prefix = /usr/local

GCCVER = $(shell expr `gcc -dumpversion` \>= 4.2)

GFLAGS1 = $(shell echo 'int main(){return 0;}' > /tmp/test-arch-tune.c && gcc -v -Q -march=native -O2 /tmp/test-arch-tune.c -o /tmp/test-arch-tune 2>&1 | sed -e 's/[\(|\)]//g' | grep march && rm /tmp/test-arch-tune.c)
GNONATIVE = $(shell echo ${GFLAGS1} | grep -e "error" -e "bad value native" | awk '{for (i =1; i <= NF; i++) print $$i}' | grep -e "bad" -e "error")
ifeq "$(strip $(GNONATIVE))" ""
GFLAGS = $(shell echo ${GFLAGS1} | awk '{for (i =1; i <= NF; i++) print $$i}' | grep -e "^-march=" -e "^-mtune=" | sort -u && rm /tmp/test-arch-tune)
endif

ifdef GFLAGS
ifndef ARCH
GARCH1 = $(shell echo ${GFLAGS} | awk '{for (i =1; i <= NF; i++) print $$i}' | grep -e "^-march=" | sed -e 's/-march=//')
GARCH = $(shell echo ${GARCH1} | sed -e 's/ native//' -e 's/native //')
endif
ifndef TUNE
GTUNE1 = $(shell echo ${GFLAGS} | awk '{for (i =1; i <= NF; i++) print $$i}' | grep -e "^-mtune=" | sed -e 's/-mtune=//')
GTUNE = $(shell echo ${GTUNE1} | sed -e 's/ native//' -e 's/native //')
endif
endif

ifndef ARCH
ifdef GARCH
ARCH = ${GARCH}
else
ifeq ($(GCCVER),1)
ifeq "$(strip $(GNONATIVE))" ""
ARCH = native
endif
endif
endif
endif

ifndef TUNE
ifdef GTUNE
TUNE = ${GTUNE}
else
ifeq ($(GCCVER),1)
ifneq "$(strip $(GNONATIVE))" ""
TUNE = native
endif
endif
endif
endif

ifneq "$(strip $(GNONATIVE))" ""
ifeq ($(ARCH),native)
ARCH =
endif
ifeq ($(TUNE),native)
TUNE =
endif
endif

ifneq "$(strip $(ARCH))" ""
ARCH2 = $(shell echo ${ARCH} | awk '{for (i =1; i <= NF; i++) print "-march="$$i}')
CFLAGS += $(ARCH2)
endif
ifneq "$(strip $(TUNE))" ""
TUNE2 = $(shell echo ${TUNE} | awk '{for (i =1; i <= NF; i++) print "-mtune="$$i}')
CFLAGS += $(TUNE2)
endif

TARGET = vproweather

.PHONY: install

all:    $(TARGET)

$(TARGET): check $(OFILES)
	$(CC) $(LDFLAGS) $(OFILES) -o $(TARGET)

check:
	@echo -n "Compiling for "
ifeq "$(strip $(NOMYSQL))" ""
	@echo -n "MySQL "
endif
ifeq ($(BIT),64)
	@echo -n "64bit "
else
	@echo -n "32bit "
endif
ifneq "$(strip $(GNONATIVE))" ""
	@echo -n "NoNative "
endif
ifdef ARCH
	@echo -n "$(ARCH) "
endif
ifdef TUNE
ifneq ($(ARCH),$(TUNE))
	@echo -n "$(TUNE) "
endif
endif
	@echo

OFILESD = $(OFILES) debug.o
debug: CFLAGS += -D__debug__
debug: HMAIN += include/debug.h
debug: Makefile check $(OFILESD)
	$(CC) $(LDFLAGS) $(OFILESD) -o $(TARGET)

main.o: Makefile main.c $(HMAIN)
	$(CC) $(CFLAGS) -c main.c

dhandler.o: Makefile dhandler.c $(HDH)
	$(CC) $(CFLAGS) -c dhandler.c

mySQLhandler.o: Makefile mySQLhandler.c $(HMYH)
	$(CC) $(CFLAGS) -c mySQLhandler.c

debug.o: Makefile debug.c include/debug.h
	$(CC) $(CFLAGS) -c debug.c

clean:
	rm -f $(TARGET) *.o

install: 
	install -m 0700 $(TARGET) $(bindir)/$(TARGET)
	install -D -p -m 0644 ${DISTMAN1DIR}/$(TARGET).1 ${INSTALLSITEMAN1DIR}/$(TARGET).1
	mkdir -p ${SHARESITEMAN1DIR}/$(TARGET)/
	cp -rp ${SHAREDIR}/$(TARGET)/* ${SHARESITEMAN1DIR}/$(TARGET)/
	chmod -R 0644 ${SHARESITEMAN1DIR}/$(TARGET)/

uninstall:
	rm -f $(bindir)/$(TARGET) ${INSTALLSITEMAN1DIR}/$(TARGET).1 ${SHARESITEMAN1DIR}/$(TARGET)/*
	rm -fr ${SHARESITEMAN1DIR}/$(TARGET)/

