# Copyright (C) 2007 Manu Garg.
# Author: Manu Garg <manugarg@gmail.com>
#
# Makefile for pacparser. Please read README file included with this package
# for more information about pacparser.
#
# pacparser is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.

# pacparser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

# This file is not part of the source code repository. It's generated by the
# packaging script.
-include version.mk

VERSION ?= $(shell git describe --always --tags --candidate=100)

PREFIX ?= /usr
OS_ARCH := $(subst /,_,$(shell uname -s | sed /\ /s//_/))

LIBRARY_NAME = libpacparser
LIB_VER = 1

# This Makefile should at least work on Linux and Mac OS X. It should work on
# most other types of Unix systems too, but I have not put any conscious effort
# for that.
#
# Set variables according to Linux.
SO_SUFFIX = so
LIBRARY = $(LIBRARY_NAME).$(SO_SUFFIX).$(LIB_VER)
MKSHLIB = $(CC) -shared
LIB_OPTS = -Wl,-soname=$(LIBRARY) -Wl,-exclude-libs=libjs.a
SHFLAGS = -fPIC
SMCFLAGS = -DHAVE_VA_COPY -DVA_COPY=__va_copy

ifeq ($(OS_ARCH),Darwin)
  MAC_MINOR_VERSION := $(shell sw_vers -productVersion | cut -d. -f2)
  MAC_GT_5 := $(shell [ $(MAC_MINOR_VERSION) -le 5 ] && echo false)
  SO_SUFFIX = dylib
  LIBRARY = $(LIBRARY_NAME).$(LIB_VER).$(SO_SUFFIX)
  MKSHLIB = $(CC) -dynamiclib -framework System
  LIB_OPTS = -install_name $(PREFIX)/lib/$(notdir $@)
  SHFLAGS =
  ifeq ($(MAC_GT_5),false)
    SMCFLAGS =
  endif
endif

CFLAGS = -g -DXP_UNIX -Wall -DVERSION=$(VERSION)

ifndef PYTHON
  PYTHON = python
endif

# Spidermonkey library.
CFLAGS += -Ispidermonkey/js/src

LIBRARY_LINK = $(LIBRARY_NAME).$(SO_SUFFIX)
PREFIX := $(DESTDIR)$(PREFIX)
LIB_PREFIX = $(PREFIX)/lib
INC_PREFIX = $(PREFIX)/include
BIN_PREFIX = $(PREFIX)/bin
MAN_PREFIX = $(PREFIX)/share/man

.PHONY: clean pymod install-pymod
all: testpactester

spidermonkey/js/src: spidermonkey/js-?.?.?.tar.gz
	tar xzvf spidermonkey/js-?.?.?.tar.gz -C spidermonkey
	sed -i -e 's:\(shell uname -s | sed /\\ /s//_/\):\1 | sed s,GNU.*,Linux,:g' spidermonkey/js/src/config.mk

jsapi_buildstamp: spidermonkey/js/src
	cd spidermonkey && SMCFLAGS="$(SHFLAGS) $(SMCFLAGS)" $(MAKE) jsapi
	touch jsapi_buildstamp

libjs.a: spidermonkey/js/src
	cd spidermonkey && SMCFLAGS="$(SHFLAGS) $(SMCFLAGS)" $(MAKE) jslib

pacparser.o: pacparser.c pac_utils.h pacparser.h jsapi_buildstamp
	$(CC) $(CFLAGS) $(SHFLAGS) -c pacparser.c -o pacparser.o
	touch pymod/pacparser_o_buildstamp

$(LIBRARY): pacparser.o libjs.a
	$(MKSHLIB) $(CFLAGS) $(LDFLAGS) $(LIB_OPTS) -o $(LIBRARY) pacparser.o libjs.a -lm

$(LIBRARY_LINK): $(LIBRARY)
	ln -sf $(LIBRARY) $(LIBRARY_LINK)

pactester: pactester.c pacparser.h $(LIBRARY_LINK)
	$(CC) $(CFLAGS) $(LDFLAGS) pactester.c -o pactester -lpacparser -L. -I.

testpactester: pactester
	echo "Running tests for pactester."
	NO_INTERNET=$(NO_INTERNET) ../tests/runtests.sh

docs:
	../tools/generatedocs.sh

install: all
	install -d $(LIB_PREFIX) $(INC_PREFIX) $(BIN_PREFIX)
	install -m 644 $(LIBRARY) $(LIB_PREFIX)/$(LIBRARY)
	ln -sf $(LIBRARY) $(LIB_PREFIX)/$(LIBRARY_LINK)
	install -m 755 pactester $(BIN_PREFIX)/pactester
	install -m 644 pacparser.h $(INC_PREFIX)/pacparser.h
	# install pactester manpages
	install -d $(MAN_PREFIX)/man1/
	(test -d ../docs && install -m 644 ../docs/man/man1/*.1 $(MAN_PREFIX)/man1/) || true
	# install pacparser manpages
	install -d $(MAN_PREFIX)/man3/
	(test -d ../docs && install -m 644 ../docs/man/man3/*.3 $(MAN_PREFIX)/man3/) || true
	# install html docs
	install -d $(PREFIX)/share/doc/pacparser/html/
	(test -d ../docs/html && install -m 644 ../docs/html/* $(PREFIX)/share/doc/pacparser/html/) || true
	# install examples
	install -d $(PREFIX)/share/doc/pacparser/examples/
	(test -d ../examples && install -m 644 ../examples/* $(PREFIX)/share/doc//pacparser/examples/) || true

# Targets to build python module
pymod: pacparser.o pacparser.h libjs.a
	cd pymod && ARCHFLAGS="" $(PYTHON) setup.py build
	$(PYTHON) ../tests/runtests.py

install-pymod: pymod
	cd pymod && ARCHFLAGS="" $(PYTHON) setup.py install --root="$(DESTDIR)/" $(EXTRA_ARGS)

clean:
	rm -f $(LIBRARY_LINK) $(LIBRARY) libjs.a pacparser.o pactester pymod/pacparser_o_buildstamp jsapi_buildstamp
	cd pymod && python setup.py clean --all
	cd spidermonkey && $(MAKE) clean
