SHELL=/bin/sh

PACKAGE_TARNAME=@PACKAGE_TARNAME@
PACKAGE_NAME=@PACKAGE_NAME@
PACKAGE_VERSION=@PACKAGE_VERSION@

VERSION_MAJOR=@VERSION_MAJOR@
VERSION_MINOR=@VERSION_MINOR@
VERSION_PATCH=@VERSION_PATCH@
VERSION_EXTEN=@VERSION_EXTEN@

RELEASE_DATE=@RELEASE_DATE@
TAR_DIR_NAME=$(shell basename `pwd`)

prefix=@prefix@
exec_prefix=@exec_prefix@
LOGDIR=@web_log_dir@
CFGDIR=@sysconfdir@
BINDIR=@bindir@
CGIDIR=@sbindir@
HTMLDIR=@datarootdir@

INSTALL=@INSTALL@
INSTALL_OPTS=@INSTALL_OPTS@
INSTALL_OPTS_WEB=@INSTALL_OPTS_WEB@
INSTALL_OPTS_CACHE=@INSTALL_OPTS_CACHE@

BIN_USER=@bin_user@
BIN_GROUP=@bin_group@
WEB_USER=@web_user@
WEB_GROUP=@web_group@
WEB_PATH=@web_path@
WEB_APACHE_PATH=@web_apache_path@

SED=@SED@
PHP=@PHP@
PHING=@PHING@
PHING_CALL=cd @srcdir@/etc/ && ../bin/phing -f build.xml
PHPUNIT=@PHPUNIT@
PHPCS=@PHPCS@

API_DIR=@icinga_api@

LOC_PO=app/data/i18n/po
LOC_MO=app/data/i18n/mo
LOC_TEMPLATE=$(LOC_PO)/templates/default.pot

SRC_DIR=@srcdir@
SRC_LIB=@srcdir@/lib
SRC_PUB=@srcdir@/pub
SRC_ETC=@srcdir@/etc
SRC_BIN=@srcdir@/bin
SRC_DOC=@srcdir@/doc
SRC_APP=@srcdir@/app
SRC_SITE=@srcdir@/etc/conf.d

none:
#	@printf "\t%-22s - %s\n" "" ""
	@printf "Please supply a command line argument (i.e. 'make install').\n\n"
	@printf "Generic targets: install, clean\n\n"
	@printf "For updates please use 'upgrade' to avoid overwriting your siteconfig!\n\n"
	@printf "Other usefull targets:\n"
	@printf "\t%-22s - %s\n" "testdeps" "test php dependencies"
	@printf "\t%-22s - %s\n" "create-tarball" "creates a tarball for packaging"
	@printf "\t%-22s - %s\n" "create-tarball-nightly" "creates a tarball from HEAD for nightly builds"
	@printf "\t%-22s - %s\n" "cacheclean" "purges the agavi xml cache"
	
	@printf "\nDatabase (web) targets:\n"
	@printf "\t%-22s - %s\n" "db-initialize" "create the db"
	@printf "\t%-22s - %s\n" "db-deinitialize" "DEPRECATED: Same as db-drop"
	@printf "\t%-22s - %s\n" "db-drop" "drop the entire db"
	@printf "\t%-22s - %s\n" "db-purge-userprefs" "purge existing user preferences"
	@printf "\t%-22s - %s\n" "db-doc2sql" "export doctrine db schema"

	@printf "\nIcinga web targets:\n"
	@printf "\t%-22s - %s\n" "icinga-reset-password" "Reset password for any account on icinga-web"
	@printf "\t%-22s - %s\n" "install-apache-config" "Install the apache config to configured path"

	@printf "\nI18n targets:\n"
	@printf "\t%-22s - %s\n" "loc-update-catalog" "Create a initial locale from source (default.po)"
	@printf "\t%-22s - %s\n" "loc-compile" "Compile language files into mo and json"

	@printf "\nScheduler targets:\n"
	@printf "\t%-22s - %s\n" "scheduler-install" "Install the icingaScheduler cronjob"
	@printf "\t%-22s - %s\n" "scheduler-remove" "remove the icingaScheduler cronjob"
	
	@printf "\nDevelopment targets:\n"
	@printf "\t%-22s - %s\n" "devel-inplace-config" "Prepare src tree for running inplace"
	@printf "\t%-22s - %s\n" "devel-reconfigure" "Clean and reconfigure source"
	@printf "\t%-22s - %s\n" "devel-update-version" "Upgrades the version file"
	@printf "\t%-22s - %s\n" "test" "Running tests"
	@printf "\t%-22s - %s\n" "test-cs" "Running codestyle tests"
	@printf "\n\n"
	
logclean:
	rm -rf $(LOGDIR)/*.log

locclean:
	rm -f \
		$(LOC_MO)/*.mo \
		$(LOC_MO)/*.json

cacheclean:
	$(PHING_CALL) agavi-clear-cache	

clean: distclean

distclean:
	rm -rf \
	autom4te.cache/ \
	.build/ \
	
	rm -f \
	Makefile \
	lib/Makefile \
	lib/icingaScheduler/config.php \
	pub/Makefile \
	etc/Makefile \
	bin/Makefile \
	doc/Makefile \
	app/Makefile \
	app/config.php \
	app/config/databases.xml \
	app/config/icinga.xml \
	app/config/logging.xml \
	app/config/settings.xml \
	app/config/translation.xml \
	app/config/factories.xml \
	app/modules/Api/config/module.xml \
	app/modules/Api/config/access.xml \
	app/modules/AppKit/config/module.xml \
	app/modules/AppKit/config/auth.xml \
	app/modules/Config/config/module.xml \
	app/modules/Reporting/config/module.xml \
	app/modules/Web/config/module.xml \
	lib/*/Makefile \
	lib/Makefile \
	etc/build.properties \
	tests/phpunit/test.properties \
	etc/apache2/icinga-web.conf \
	etc/scheduler/icingaCron \
	bin/clearcache.sh \
	etc/conf.d/access.xml \
	etc/conf.d/databases.xml \
	etc/conf.d/Makefile \
	etc/conf.d/sla.xml
	
remove-temp-files:
	bin/rmtmp-files.sh

create-tarball:
	@./bin/make-tarball --prefix $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)/

create-tarball-nightly:
	#${MAKE} submodule-update
	./bin/make-tarball --prefix $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-`date +%Y%m%d`-`git rev-parse --short HEAD`/

testdeps:
	 $(PHP) bin/testdeps.php

test-precede:
	if test ! -d "$(SRC_DIR)/.build"; then mkdir .build; fi
	

test: test-precede
	rm -f $(SRC_DIR)/.build/test-results.xml
	$(PHPUNIT) \
        --configuration=$(SRC_DIR)/tests/phpunit/suites.xml

test-cs: test-precede
	rm -f $(SRC_DIR)/.build/checkstyle.xml
	touch $(SRC_DIR)/.build/checkstyle.xml
	$(PHPCS) \
		--standard=$(SRC_DIR)/tests/phpcs/YACS \
		--report=checkstyle \
		--report-file=$(SRC_DIR)/.build/checkstyle.xml \
		--ignore="cache,\.js$$" \
		$(SRC_DIR)/app

devel-inplace-config:
	mkdir -p \
	$(SRC_DIR)/app/cache/config \
	$(LOGDIR) \
	$(SRC_DIR)/app/modules/Reporting/data/tmp
	chown $(WEB_USER).$(WEB_GROUP) \
		$(SRC_DIR)/app/cache \
		$(SRC_DIR)/app/cache/config \
		$(LOGDIR) \
		$(SRC_DIR)/app/modules/Reporting/data/tmp
	@printf "Done.\n"

devel-reconfigure:
	@EXEC=`head config.log  | grep "$$ configure" | sed -e 's/\s\+\$$\s\+//g'`; \
	make distclean; \
	sh $$EXEC

devel-update-version:
	@VFILE="doc/VERSION"; \
        VERSION=`printf "%d.%d.%d" "$(VERSION_MAJOR)" "$(VERSION_MINOR)" "$(VERSION_PATCH)"`; \
        test ! "X$(VERSION_EXTEN)" = "X" && VERSION=`printf "%s-%s" "$$VERSION" "$(VERSION_EXTEN)"`; \
        echo "$$VERSION" > $$VFILE; \
        echo "Upgraded $$VFILE to version $$VERSION"

install-javascript:
	$(INSTALL) -m 755 $(INSTALL_OPTS) -d $(DESTDIR)$(prefix)/pub/js
	ln -sf $(DESTDIR)$(prefix)/lib/ext3 $(DESTDIR)$(prefix)/pub/js/ext3

install-apache-config:
	$(INSTALL) -m 755 -d $(INSTALL_OPTS) $(DESTDIR)$(WEB_APACHE_PATH)
	${INSTALL} -b -m 664 $(INSTALL_OPTS) etc/apache2/icinga-web.conf $(DESTDIR)$(WEB_APACHE_PATH)/icinga-web.conf
	@printf "\nInstalled configuration in '%s'.\n" "$(WEB_APACHE_PATH)"
	@printf "Please make sure that the directory is included or \n"
	@printf "include %s/icinga-web.conf manually.\n" "$(WEB_APACHE_PATH)"

	@if [ "$(WEB_APACHE_PATH)" = "/etc/apache2/conf-available" ]; then \
		printf "Enable the configuration using 'a2enconf icinga-web'.\n"; \
	fi

	@printf "\nPlease restart your apache in order to complete this step.\n\n"

install-lib:
	cd $(SRC_LIB) && $(MAKE)

install-pub:
	cd $(SRC_PUB) && $(MAKE)

install-etc:
	cd $(SRC_ETC) && $(MAKE)

install-bin:
	cd $(SRC_BIN) && $(MAKE)

install-doc:
	cd $(SRC_DOC) && $(MAKE)

install-app:
	cd $(SRC_APP) && $(MAKE)

install-siteconfig:
	cd $(SRC_SITE) && $(MAKE)

install-log:
	$(INSTALL) -m 755 $(INSTALL_OPTS_WEB) -d $(DESTDIR)$(LOGDIR)

install-all: install-lib install-pub install-etc install-bin install-doc install-app install-log install-siteconfig

install: install-all install-done

install-done:
	@printf "\n\n"
	@printf "Installation if icinga-web succeeded.\n"
	@printf "\n"
	@printf	"Please check the new Apache2 configuration ($(DESTDIR)$(WEB_APACHE_PATH)/icinga-web.conf).\n"
	@printf "\n"
	@printf "You can install it simply by invoking 'make install-apache-config'.\n"
	@printf "\n"
	@printf "If you don't want this you can restore its old behavior by\n"
	@printf "typing 'make install-javascript'. This will install the old symlinks.\n"
	@printf "\n"
	@printf "If you want to setup your database manually, you can find the scripts \n"
	@printf "at etc/schema, otherwise use make db-initialize.\n"	
	@printf "\n"
	@printf "Have fun!\n\n"

upgrade: install-all upgrade-done

upgrade-done:
	@printf "\n\n"
	@printf "Upgrade of icinga-web succeeded.\n"
	@printf "\n"
	@printf "Please check your site config files if something have to be changed.\n"
	@printf "\n"
	@printf "For database upgrades look into etc/schema/updates and import the\n"
	@printf "appropriate changes into your database\n"
	@printf "\n"
	@printf "Have fun!\n\n"

db-initialize:
	$(PHING_CALL) db-initialize

# -----------------------------------------------------------------------------
# DEPRECATED TARGET, REMOVE ON 1.10
# -----------------------------------------------------------------------------
db-deinitialize:
	$(PHING_CALL) db-deinitialize

db-doc2sql:
	${PHING_CALL} doc2sql

db-drop:
	$(PHING_CALL) db-drop

db-purge-userprefs:
	$(PHING_CALL) db-purge-userprefs

make-create-files:
	$(SED) -i '/^# INSTALL_FILES_BEGIN/,/^# INSTALL_FILES_END/{D}' $(INC_MAKE_FILES) > $(INC_MAKE_FILES).tmp
	mv $(INC_MAKE_FILES).tmp $(INC_MAKE_FILES)
	./bin/create-makefile.sh ./ >> $(INC_MAKE_FILES)

loc-update-catalog:
	./bin/loc-create-catalog.pl --base=./app/ --out=$(LOC_PO)/templates/default.pot
	./bin/loc-merge-template.sh $(LOC_PO)/templates/default.pot $(LOC_PO)

loc-compile: loc-create-mo loc-create-json

loc-create-mo:
	./bin/loc-create-mo.sh $(LOC_PO) $(LOC_MO)

loc-create-json:
	./bin/loc-create-json.sh $(LOC_PO) $(LOC_MO)

submodule-init:
	git submodule init app/data/i18n/po

submodule-update:
	cd app/data/i18n/po
	git checkout master
	git pull origin master
	cd -
	git submodule update

scheduler-install:
	$(PHING_CALL) scheduler-install -DPATH_Icinga=$(prefix)

scheduler-remove:
	$(PHING_CALL) scheduler-remove -DPATH_Icinga=$(prefix)

icinga-reset-password:
	$(PHING_CALL) icinga-reset-password
