#
# Makefile for the cryptobox web-interface
#
# Copyright (c) 02006 senselab
#

CRYPTOBOX_ROOT = .

PREFIX = /usr/local
SHARE_DIR = $(PREFIX)/share/cryptobox
LIB_DIR = $(PREFIX)/lib/cryptobox
DOC_DIR = $(PREFIX)/share/doc/cryptobox
BUILD_DIR = build_dir
INSTALL = install

OS_TYPE=$(shell uname -o | tr [:upper:] [:lower:] | sed s/[^a-z0-9\._\-]/_/g)

.PHONY: install clean build

build:
	$(INSTALL) -d -m 755 $(BUILD_DIR)/etc
	$(INSTALL) -c -m 644 conf-examples/cryptobox.conf $(BUILD_DIR)/etc/
	@sed -i 's#^HTML_TEMPLATE_DIR=.*$$#HTML_TEMPLATE_DIR=$(SHARE_DIR)/templates#' $(BUILD_DIR)/etc/cryptobox.conf
	@sed -i 's#^LANGUAGE_DIR=.*$$#LANGUAGE_DIR=$(SHARE_DIR)/lang#' $(BUILD_DIR)/etc/cryptobox.conf
	@sed -i 's#^DOC_DIR=.*$$#DOC_DIR=$(DOC_DIR)/html#' $(BUILD_DIR)/etc/cryptobox.conf
	@sed -i 's#^CONFIG_DEFAULTS_DIR=.*$$#CONFIG_DEFAULTS_DIR=$(SHARE_DIR)/defaults#' $(BUILD_DIR)/etc/cryptobox.conf

	# choose the appropriate program_locations.conf
	if test -e conf-examples/distributions/$(OS_TYPE) ; \
	  then	cat conf-examples/distributions/$(OS_TYPE) ;\
	  else	cat conf-examples/distributions/default ;\
	 fi >$(BUILD_DIR)/etc/distribution.conf

	# compile the suid wrapper
	$(MAKE) -C bin LIB_DIR=$(LIB_DIR)
	@touch $(BUILD_DIR)-stamp


install: $(BUILD_DIR)-stamp
	$(INSTALL) -d -m 755 $(LIB_DIR)
	$(INSTALL) -c -m 755 bin/cbox-manage.sh $(LIB_DIR)
	$(INSTALL) -c -m 755 bin/cbox-root-actions.sh $(LIB_DIR)
	$(INSTALL) -c -m 755 bin/cryptobox.pl $(LIB_DIR)
	$(INSTALL) -c -m 755 bin/cryptobox_cgi_wrapper $(LIB_DIR)
	$(INSTALL) -c -m 4755 bin/cryptobox_root_wrapper $(LIB_DIR)
	$(INSTALL) -d -m 755 $(SHARE_DIR)/lang
	$(INSTALL) -c -m 644 lang/* $(SHARE_DIR)/lang/
	$(INSTALL) -d -m 755 $(SHARE_DIR)/templates
	$(INSTALL) -c -m 644 templates/*.cs $(SHARE_DIR)/templates
	$(INSTALL) -d -m 755 $(SHARE_DIR)/defaults
	$(INSTALL) -c -m 644 conf-examples/default-settings/* $(SHARE_DIR)/defaults/
	$(INSTALL) -d -m 755 $(SHARE_DIR)/html
	$(INSTALL) -c -m 644 www-data/*.css $(SHARE_DIR)/html/
	$(INSTALL) -c -m 644 www-data/*.png $(SHARE_DIR)/html/
	$(INSTALL) -c -m 644 www-data/*.gif $(SHARE_DIR)/html/
	#$(INSTALL) -d -m 755 $(SHARE_DIR)/html/screenshots
	#$(INSTALL) -c -m 644 www-data/screenshots/*.png $(SHARE_DIR)/html/screenshots/
	$(INSTALL) -d -m 755 $(DOC_DIR)/html/en
	$(INSTALL) -d -m 755 $(DOC_DIR)/html/de
	$(INSTALL) -c -m 644 doc/html/en/* $(DOC_DIR)/html/en/
	$(INSTALL) -c -m 644 doc/html/de/* $(DOC_DIR)/html/de/
	$(INSTALL) -d -m 755 $(SHARE_DIR)/distributions
	$(INSTALL) -c -m 644 conf-examples/distributions/* $(SHARE_DIR)/distributions/


clean:
	$(MAKE) -C bin clean
	-rm -rf $(BUILD_DIR)
	-rm -f $(BUILD_DIR)-stamp