# Makefile installer for IceWarp server
# Copyright (c) 2008, 2009 IceWarp Ltd.

.SILENT:

#
# Set default variables
#
_INSTALL_DATE=$(shell date)
_SERVICE_NAME=icewarp

_INSTALL_VERSION=13.0.3.13 RHEL8 x64
_INSTALL_PACKAGE=IceWarpServer-image-13.0.3.13_RHEL8_x64.tar

ifneq ($(strip $(ICEWARP_INSTALL_PREFIX)),)
_INSTALL_PREFIX=$(ICEWARP_INSTALL_PREFIX)
else
_INSTALL_PREFIX="/opt/$(_SERVICE_NAME)"
endif

ifneq ($(strip $(ICEWARP_INSTALL_USER)),)
_INSTALL_USER=$(ICEWARP_INSTALL_USER)
else
_INSTALL_USER=$(shell id -un)
endif

ifneq ($(strip $(ICEWARP_INSTALL_GROUP)),)
_INSTALL_GROUP=$(ICEWARP_INSTALL_GROUP)
else
_INSTALL_GROUP=$(shell id -gn)
endif

ifneq ($(strip $(ICEWARP_INSTALL_SERVICE)),)
_INSTALL_SERVICE=yes
else
_INSTALL_SERVICE=no
endif

ifneq ($(strip $(ICEWARP_INSTALL_BOOT_START)),)
_INSTALL_BOOT_START=yes
else
_INSTALL_BOOT_START=no
endif

_INSTALL_UUID=$(shell id -u)
_INSTALL_UUID_DST=$(shell id -u $(_INSTALL_USER))

_CONFIG_FILE="$(_INSTALL_PREFIX)/config/merak.conf"

all: _help

.PHONY: install

install: _pre_install _install _post_install _thankyou

#
#  Root rules
#
_help:
	echo "IceWarp server installer"
	echo "This is GNU makefile (gmake) script"
	echo "Copyright (c) 2008 IceWarp Ltd."
	echo ""
	echo "Usage:"
	echo "       make install"
	echo ""

_pre_install: _collect_informations _check_informations

_install: _install_package _configure_installed_package _update_configuration_files

_post_install: _install_service _create_uninstall_info _finish_installation

_thankyou:
	echo "Thank you for installing IceWarp server."

#
# Pre-install rules
#
_collect_informations:
	echo "** Collecting informations"
	echo "Install prefix: $(_INSTALL_PREFIX)"
	echo "Install user: $(_INSTALL_USER)"
	echo "Install group: $(_INSTALL_GROUP)"
	echo "Install package: $(_INSTALL_PACKAGE)"
	echo "Install service: $(_INSTALL_SERVICE)"
	echo "Autostart service at boot: $(_INSTALL_BOOT_START)"

_check_informations:
	echo "** Checking informations"
# test if entered user and group exists
	grep -q "^$(_INSTALL_USER):" /etc/passwd; if [ $$? -ne 0 ]; then echo "ERROR: User '$(_INSTALL_USER)' does not exists"; exit 1; fi;
	grep -q "^$(_INSTALL_GROUP):" /etc/group; if [ $$? -ne 0 ]; then echo "ERROR: Group '$(_INSTALL_GROUP)' does not exists"; exit 1; fi;
# test if have permissions to install
	if [ "$(_INSTALL_UUID)" != "0" ] && [ "$(_INSTALL_UUID)" != "$(_INSTALL_UUID_DST)" ]; then echo "ERROR: Permission denied to install with user '$(_INSTALL_USER)'"; exit 1; fi;

#
# Install rules
#

_install_package:
	echo "** Installing package"
# create destination diractory
	mkdir -p "$(_INSTALL_PREFIX)"
# remove files which can be changed/new in instalation package
# installer is default in "upgrade mode" so all functions which can return error but will not stop install process must be succesffull
	rm -rf "$(_INSTALL_PREFIX)/scripts"; exit 0;
	rm -rf "$(_INSTALL_PREFIX)/startd"; exit 0;
	rm -rf "$(_INSTALL_PREFIX)/stopd"; exit 0;
	rm -rf "$(_INSTALL_PREFIX)/restartd"; exit 0;
# extract image
	tar -xf "$(_INSTALL_PACKAGE)" -C "$(_INSTALL_PREFIX)"
# create or add new configuration files
	#                 \
	# config/cert.pem \
	#                 \
	FNAME_SRC="config/default/cert.pem"; \
	FNAME_DST="config/cert.pem"; \
	if [ ! -f "$(_INSTALL_PREFIX)/$${FNAME_DST}" ]; then \
	echo "Copying $${FNAME_DST}"; \
	cp -f "$(_INSTALL_PREFIX)/$${FNAME_SRC}" "$(_INSTALL_PREFIX)/$${FNAME_DST}"; \
	fi; \
	#                      \
	# config/webserver.dat \
	#                      \
	FNAME_SRC="config/default/webserver.dat"; \
	FNAME_DST="config/webserver.dat"; \
	if [ ! -f "$(_INSTALL_PREFIX)/$${FNAME_DST}" ]; then \
	echo "Copying $${FNAME_DST}"; \
	cp -f "$(_INSTALL_PREFIX)/$${FNAME_SRC}" "$(_INSTALL_PREFIX)/$${FNAME_DST}"; \
	fi; \
	#                       \
	# calendar/calendar.dat \
	#                       \
	FNAME_SRC="calendar/default/calendar.dat"; \
	FNAME_DST="calendar/calendar.dat"; \
	if [ ! -f "$(_INSTALL_PREFIX)/$${FNAME_DST}" ]; then \
	echo "Copying $${FNAME_DST}"; \
	cp -f "$(_INSTALL_PREFIX)/$${FNAME_SRC}" "$(_INSTALL_PREFIX)/$${FNAME_DST}"; \
	fi; \
	#                       \
	# calendar/groupware.db \
	#                       \
	FNAME_SRC="calendar/default/db/groupware.db"; \
	FNAME_DST="calendar/groupware.db"; \
	if [ ! -f "$(_INSTALL_PREFIX)/$${FNAME_DST}" ]; then \
	echo "Copying $${FNAME_DST}"; \
	cp -f "$(_INSTALL_PREFIX)/$${FNAME_SRC}" "$(_INSTALL_PREFIX)/$${FNAME_DST}"; \
	fi; \
	#                  \
	# spam/antispam.db \
	#                  \
	FNAME_SRC="spam/default/db/antispam.db"; \
	FNAME_DST="spam/antispam.db"; \
	if [ ! -f "$(_INSTALL_PREFIX)/$${FNAME_DST}" ]; then \
	echo "Copying $${FNAME_DST}"; \
	cp -f "$(_INSTALL_PREFIX)/$${FNAME_SRC}" "$(_INSTALL_PREFIX)/$${FNAME_DST}"; \
	fi; \
	#                     \
	# spam/rules/local.cf \
	#                     \
	FNAME_SRC="spam/default/rules/local.cf"; \
	FNAME_DST="spam/rules/local.cf"; \
	if [ ! -f "$(_INSTALL_PREFIX)/$${FNAME_DST}" ]; then \
	echo "Copying $${FNAME_DST}"; \
	cp -f "$(_INSTALL_PREFIX)/$${FNAME_SRC}" "$(_INSTALL_PREFIX)/$${FNAME_DST}"; \
	fi; \

_configure_installed_package:
	echo "** Configuring package"
# create new configuration file
	if [ -f "$(_CONFIG_FILE)" ]; then mv "$(_CONFIG_FILE)" "$(_CONFIG_FILE).bak"; fi; \
	touch "$(_CONFIG_FILE)"; \
	echo "# IceWarp server configuration file" >> "$(_CONFIG_FILE)"; \
	echo "# this file was generated by IceWarp installer on $(_INSTALL_DATE)" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)"; \
	echo "InstallDir=\"$(_INSTALL_PREFIX)\"" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)"; \
	echo "Version=\"$(_INSTALL_VERSION)\"" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)"; \
	echo "# User to run service as" >> "$(_CONFIG_FILE)"; \
	echo "User=\"$(_INSTALL_USER)\"" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)"; \
	echo "# Group to run service as" >> "$(_CONFIG_FILE)"; \
	echo "Group=\"$(_INSTALL_GROUP)\"" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)"; \
	echo "AVENGINE=\"$(_INSTALL_AVENGINE)\"" >> "$(_CONFIG_FILE)"; \
	echo "" >> "$(_CONFIG_FILE)";
# configure PHP
	touch "$(_INSTALL_PREFIX)/php/php.ini"
	echo "" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "[Zend]" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "zend_extension_manager.optimizer = \"$(_INSTALL_PREFIX)/php/ext/Optimizer-3.3.3\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "zend_extension_manager.optimizer_ts = \"$(_INSTALL_PREFIX)/php/ext/Optimizer_TS-3.3.3\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "zend_optimizer.version = 3.3.3" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "zend_extension = \"$(_INSTALL_PREFIX)/php/ext/ZendExtensionManager.so\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "zend_extension_ts = \"$(_INSTALL_PREFIX)/php/ext/ZendExtensionManager_TS.so\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "[IceWarp]" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "icewarp_sharedlib_path = \"$(_INSTALL_PREFIX)/html/_shared/\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "extension_dir = \"$(_INSTALL_PREFIX)/php/ext/\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "error_log = \"$(_INSTALL_PREFIX)/logs/php/error.log\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "open_basedir_ignore = \"$(_INSTALL_PREFIX)\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "upload_tmp_dir = \"$(_INSTALL_PREFIX)/php/tmp/\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "session.save_path = \"$(_INSTALL_PREFIX)/php/tmp/\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
	echo "sendmail_path = \"$(_INSTALL_PREFIX)/sendmail\"" >> "$(_INSTALL_PREFIX)/php/php.ini"
# copy configuration file to /etc
	rm -f "/etc/icewarp/icewarp.conf"
	mkdir -p "/etc/icewarp" &> /dev/null
	cp -f "$(_CONFIG_FILE)" "/etc/icewarp/icewarp.conf"

_update_configuration_files:
	echo "** Updating configuration files"
# update webserver.dat
	_UPDATE_FILE="$(_INSTALL_PREFIX)/config/webserver.dat"; \
	echo "Updating $${_UPDATE_FILE}"; \
	sed "s|phpd<|phpd.sh<|" $${_UPDATE_FILE} > "$${_UPDATE_FILE}_"; \
	rm -f $${_UPDATE_FILE}; \
	mv "$${_UPDATE_FILE}_" "$${_UPDATE_FILE}";

#
# Post-install rules
#
_install_service:
ifeq ($(_INSTALL_SERVICE),yes)
	echo "** Installing service"
ifeq ($(_INSTALL_UUID),0)
	rm -f "/etc/init.d/$(_SERVICE_NAME)"
	sed "s|%%INSTALL_DIR_VALUE%%|$(_INSTALL_PREFIX)|" "$(_INSTALL_PREFIX)/scripts/setup/$(_SERVICE_NAME).init" > "/etc/init.d/$(_SERVICE_NAME)"
	chmod a+x "/etc/init.d/$(_SERVICE_NAME)" > /dev/null
ifeq ($(_INSTALL_BOOT_START),yes)
	echo "** Installing service to start at boot"
	chkconfig --del $(_SERVICE_NAME)
	chkconfig --add $(_SERVICE_NAME)
endif
else
	echo "WARNING: You do not have permission to install service."
endif
endif

_create_uninstall_info:
	echo "** Creating uninstall info"
# create list of all files in tar
	tar -tf "$(_INSTALL_PACKAGE)" > "$(_INSTALL_PREFIX)/scripts/setup/content.txt" 2> /dev/null
# split list to derictories and files
	INSTALLED_DIRS=$$(cat "$(_INSTALL_PREFIX)/scripts/setup/content.txt" | grep ".*\/$$"); \
	INSTALLED_FILES=$$(cat "$(_INSTALL_PREFIX)/scripts/setup/content.txt" | grep ".*[^\/]$$"); \
	# create file with installed directories \
	rm -f "$(_INSTALL_PREFIX)/scripts/setup/installed_dirs.txt"; \
	touch "$(_INSTALL_PREFIX)/scripts/setup/installed_dirs.txt"; \
	for LINE in $$INSTALLED_DIRS; do \
	echo $$LINE >> "$(_INSTALL_PREFIX)/scripts/setup/installed_dirs.txt"; \
	done; \
	# create file with installed files \
	# do not count configuration files which comes with installation \
	rm -f "$(_INSTALL_PREFIX)/scripts/setup/installed_files.txt"; \
	touch "$(_INSTALL_PREFIX)/scripts/setup/installed_files.txt"; \
	for LINE in $$INSTALLED_FILES; do \
		if [ "$$LINE" != "config/cert.pem" ] && \
			[ "$$LINE" != "config/webserver.dat" ] && \
			[ "$$LINE" != "spam/antispam.dat" ] && \
			[ "$$LINE" != "spam/rules/local.cf" ] && \
			[ "$$LINE" != "calendar/groupware.db" ] && \
			[ "$$LINE" != "webmail/config/settings.dat" ]; then \
			echo $$LINE >> "$(_INSTALL_PREFIX)/scripts/setup/installed_files.txt"; \
		fi; \
	done; \
	# add installed_dirs.txt file \
	echo "scripts/setup/installed_dirs.txt" >> "$(_INSTALL_PREFIX)/scripts/setup/installed_files.txt"; \
	# add installed_files.txt file \
	echo "scripts/setup/installed_files.txt" >> "$(_INSTALL_PREFIX)/scripts/setup/installed_files.txt"; \
	# remove content file \
	rm -f "$(_INSTALL_PREFIX)/scripts/setup/content.txt";

_finish_installation:
# change file permissions
	echo "Changing permissions"
	chown -R $(_INSTALL_USER):$(_INSTALL_GROUP) "$(_INSTALL_PREFIX)"
