# -*- makefile -*-

VFILE = lib/guardian/_version.py

VERSION := $(shell test -e $(VFILE) && python -c "execfile('$(VFILE)'); print __version__")

SVNR := $(shell svn info 2>/dev/null | sed -n 's/^Revision: //p')
ifndef SVNR
 SVNR := $(shell git svn info 2>/dev/null | sed -n 's/^Revision: //p')
endif
ifndef SVNR
 SVNR := $(shell git log | grep git-svn-id | head -1 | sed 's|.*@\([0-9]*\) .*|\1|')
endif

##########

.PHONY: all
all: build

.PHONY: test
test:
	./test/guardian-test $(GUARD_TEST_OPTS)
test-verbose:
	./test/guardian-test --verbose $(GUARD_TEST_OPTS)

##########

.PHONY: version
version:
ifeq ($(VERSION), $(SVNR))
	@echo V = $(VERSION)
else
	echo "__version__ = '$(SVNR)'" >$(VFILE)
endif

.PHONY: dist
dist: version
	python ./setup.py sdist

.PHONY: build
build: version
	python ./setup.py build

.PHONY: install
ifndef PREFIX
install:
	python ./setup.py install
else
install:
	python ./setup.py install --prefix=$(PREFIX)
endif
ligo-install: APPSROOT := $(or $(APPSROOT), /ligo/apps/linux-x86_64)
ligo-install: DISTDIR := $(APPSROOT)/guardian-$(VERSION)
ligo-install: ENVSCRIPT := $(DISTDIR)/etc/guardian-user-env.sh
ligo-install:
	python ./setup.py install --prefix=$(DISTDIR)
	install -d $(DISTDIR)/etc
	install etc/guardian-user-env.sh $(DISTDIR)/etc
	sed -i "s|___GUARDIAN_LOCATION___|$(DISTDIR)|" $(DISTDIR)/etc/guardian-user-env.sh

##########

# Debian/Ubuntu test package
.PHONY: deb-snapshot
# FIXME: do this without creating a dist tarball
deb-snapshot: DIST=guardian-$(VERSION)
deb-snapshot: dist
	mkdir -p build
	cd build; tar zxf ../dist/$(DIST).tar.gz
	cp -a debian build/$(DIST)
	cd build/$(DIST); dch -b -v $(VERSION) -D UNRELEASED 'test build, not for upload'
	cd build/$(DIST); echo '3.0 (native)' > debian/source/format
	cd build/$(DIST); debuild -us -uc

##########

.PHONY: clean
clean:
	rm -rf dist
	rm -rf build
	rm -rf test/tmp.*
