SVNVER = $(shell ( svnversion -c . 2>/dev/null | sed 's/Unversioned directory/exported/' || echo 0:0 ) | cut -d : -f 2)

VERSION_MAJOR = 2
VERSION_MINOR = 17

ifeq ($(VERSION_TWEAK),)
ifeq ($(SVNVER),exported)
# mw.rev is created after an export so we can rely on it to provide the svn rev here
VERSION_TWEAK = $(shell cat $(SRCROOT)/mw.rev)
else
ifeq ($(SVNVER),0)
$(warning failed to run svnversion, is it installed? VERSION_TWEAK will be set to 0)
endif
VERSION_TWEAK = $(SVNVER)
endif
endif

VERSION = $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_TWEAK)

MWVERSION = mw3-$(VERSION)

prefix ?= /usr
libdir ?= $(prefix)/lib
bindir ?= $(prefix)/bin
datadir ?= $(prefix)/share
localstatedir ?= /var
initddir ?= /etc/init.d

# The non source files that should get installed
INSTALLFILES = colour help login.banner scripthelp talkhelp wizhelp COPYING INSTALL LICENSE README

LOGDIR := $(localstatedir)/log/mw
MSGDIR := $(localstatedir)/run/mw
STATEDIR := $(localstatedir)/lib/mw
HOMEPATH := $(libdir)/mw

GCCMAJOR := $(echo __GNUC__ | $compiler -E -xc - | tail -n 1)
GCCMINOR := $(echo __GNUC_MINOR__ | $compiler -E -xc - | tail -n 1)
GCCVER := $(shell printf "%02d%02d" $(GCCMAJOR) $(GCCMINOR))


ifeq ($(JSFLAGS),)
	JSDIR=$(DEPTH)mozjs
	JSFLAGS= -include $(JSDIR)/usr/include/js-17.0/js/RequiredDefines.h -I/usr/include/nspr -I$(JSDIR)/usr/include/js-17.0
	LDFLAGS += -L$(JSDIR)/$(libdir)/
	JSLIBS= -lmozjs-17.0
	JSSCRIPTTYPE=JSScript
endif
ifeq ($(JSFLAGS),)
	#Fedora
	JSFLAGS=$(shell pkg-config --cflags libjs 2>/dev/null)
	JSLIBS=$(shell pkg-config --libs-only-l libjs 2>/dev/null)
	JSSCRIPTTYPE=JSObject # Some idiot made the script APIs different without changing the version
endif
ifeq ($(JSFLAGS),)
	#Debian
	JSFLAGS=$(shell pkg-config --cflags mozilla-js)
	JSLIBS=$(shell pkg-config --libs-only-l mozilla-js)
	JSSCRIPTTYPE=JSScript
endif

# cflags for standard 'cc' compiler
CFLAGS+= -Wall -Wshadow -Wmissing-prototypes -Wpointer-arith -Wwrite-strings -Wcast-align -Wbad-function-cast -Wmissing-format-attribute -Wformat=2 -Wformat-security -Wformat-nonliteral -Wno-long-long -Wno-strict-aliasing -pedantic -std=gnu99 -D_GNU_SOURCE $(JSFLAGS) -DJSSCRIPTTYPE=$(JSSCRIPTTYPE)

# until gcc catches up (4.7.x is good)
CFLAGS += $(shell if [ $(GCCVER) -lt 0406 ] ; then echo "-Wno-multichar"; fi )

ifdef WITHPIE
CFLAGS += -fpie
LDFLAGS += -pie
endif

# info strings, do not edit.
DEFS:= -DBUILD_DATE=\"$(shell date +%Y%m%d)\"
DEFS+= -DBUILD_USER=\"$(shell whoami | awk -f $(DEPTH)capitalise.awk)\"
DEFS+= -DVER_MAJ=\"$(VERSION_MAJOR)\"
DEFS+= -DVER_MIN=\"$(VERSION_MINOR)\"
DEFS+= -DVER_TWK=\"$(VERSION_TWEAK)\"
DEFS+= -DHOMEPATH=\"$(HOMEPATH)\"
DEFS+= -DLOGDIR=\"$(LOGDIR)\"
DEFS+= -DSTATEDIR=\"$(STATEDIR)\"
DEFS+= -DMSGDIR=\"$(MSGDIR)\"

### uncomment for gdb debugging
LDFLAGS+= -ggdb -g
CFLAGS+= -ggdb -g -D__NO_STRING_INLINE -fstack-protector-all

### Optimisation - uncomment for release & extra testing
CFLAGS+=-O0 -Werror

CFLAGS += $(DEFS)

### The magic which lets us autogenerate dependencies
CFLAGS += -MMD

CODE=$(wildcard *.c)
HDRS=$(wildcard *.h)

all: build

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

-include $(CODE:.c=.d)

.PHONY: build install clean test all testclean