# fgfs-builder Version 2.0
# Copyright (c) 2006-2007, Ralf Gerlich <ralf.gerlich@custom-scenery.org>
TOPDIR   ?= $(shell pwd)
SRCDIR   ?= $(TOPDIR)/src
BUILDDIR ?= $(TOPDIR)/build
PREFIX   ?= $(TOPDIR)/install

LOCALCFGDIR = $(BUILDDIR)/config
CONFIGDIR   = $(SRCDIR)/config
MAKELIB     = $(TOPDIR)/make
PRODUCTDIR  = $(TOPDIR)/products
DOWNLOADDIR = $(TOPDIR)/downloads

INSTALL=/usr/bin/install

PACKAGE=fgfs-builder
VERSION=$(shell date +%Y%m%d)
GNUPG=/usr/bin/gpg

-include $(LOCALCFGDIR)/config.local

export TOPDIR SRCDIR BUILDDIR PREFIX
export LOCALCFGDIR CONFIGDIR MAKELIB PRODUCTDIR DOWNLOADDIR

export INSTALL

ALL_PRODUCTS=$(notdir $(wildcard $(PRODUCTDIR)/*))
PRODUCTS=$(foreach product,$(ALL_PRODUCTS),\
              $(if $(wildcard $(LOCALCFGDIR)/$(product)/enabled),$(product)))

ENABLED_PRODUCTS=$(PRODUCTS)
export ENABLED_PRODUCTS ALL_PRODUCTS

ifeq ($(strip $(PRODUCTS)),)
all: help
else
all: prepare install
endif

help:
	@(cat $(TOPDIR)/doc/help.txt; \
	echo; \
	echo "Enabled products:"; \
	for p in $(PRODUCTS); do echo "    $$p"; done; \
	echo "Disabled products:"; \
	for p in $(ALL_PRODUCTS); do \
		[ -f $(LOCALCFGDIR)/$$p/enabled ] || echo "    $$p"; \
	done)

prepare download patch install unpatch clean distclean:
	$(MAKE) -C $(TOPDIR) $(addsuffix -$@,$(PRODUCTS))

configure:
	$(MAKE) -C $(TOPDIR) $(addsuffix -$@,$(ALL_PRODUCTS))

$(PRODUCTS):
	$(MAKE) -C $(TOPDIR) $@-prepare $@-install

disableall: $(addprefix disable-,$(PRODUCTS))

enable-%:
	@if [ ! -f $(LOCALCFGDIR)/$*/enabled ]; then \
		echo "Enabling previously disabled product $*"; \
		mkdir -p $(LOCALCFGDIR)/$*; \
		touch $(LOCALCFGDIR)/$*/enabled; \
	fi

disable-%:
	@if [ -f $(LOCALCFGDIR)/$*/enabled ]; then \
		echo "Disabling previously enabled product $*"; \
		rm -f $(LOCALCFGDIR)/$*/enabled; \
	fi

conflict-disable-%:
	@if [ -f $(PRODUCTDIR)/$*/enabled ]; then \
		echo "Disabling previously enabled product $* (CONFLICT)"; \
		rm -f $(LOCALCFGDIR)/$*/enabled; \
	fi

realclean:
	rm -Rf $(BUILDDIR)
	$(MAKE) -C $(TOPDIR) $(addsuffix -realclean,$(PRODUCTS))

wrapup: wrapup_clean wrapup_base wrapup_products wrapup_pack

wrapup-source: wrapup_clean wrapup_base wrapup_src wrapup_products wrapup_pack

wrapup_clean:
	rm -Rf $(PACKAGE)-$(VERSION) $(PACKAGE)-$(VERSION).tar.gz

wrapup_base:
	mkdir $(PACKAGE)-$(VERSION)
	tar -cf - -C $(TOPDIR) \
		Makefile config doc make products scripts | \
		tar xf - -C $(PACKAGE)-$(VERSION)
	env -i $(MAKE) -C $(PACKAGE)-$(VERSION) configure
	find $(PACKAGE)-$(VERSION) -name "*~" | xargs -r rm
	find $(PACKAGE)-$(VERSION) -type d -name ".svn" | xargs -r rm -Rf

wrapup_src:
	tar -cf - -C $(TOPDIR) downloads src | \
		tar xf - -C $(PACKAGE)-$(VERSION)
ifneq ('',$(PRODUCTS))
	env -i $(MAKE) -C $(PACKAGE)-$(VERSION) $(addsuffix -unpatch,$(PRODUCTS))
endif

wrapup_products:
	env -i $(MAKE) -s -C $(PACKAGE)-$(VERSION) OLDDIR=$(TOPDIR) $(addsuffix -wrapup,$(ALL_PRODUCTS))

wrapup_pack:
	find $(PACKAGE)-$(VERSION) -name "*~" -o -name ".#*#" | xargs rm -f
	tar czf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)
	rm -Rf $(PACKAGE)-$(VERSION)

wrapup_sign:
	$(GNUPG) -b $(PACKAGE)-$(VERSION).tar.gz

# include dependencies for all products
-include $(TOPDIR)/products/*/dependencies

.PHONY: all configure prepare download patch install unpatch
.PHONY: clean distclean realclean wrapup
.PHONY: help
.PHONY: $(PRODUCTS)
.PHONY: enable-% disable-% conflict-disable-% disableall
