#!/bin/sh
#
# metadata_begin
# recipe: ISPmanager 5 Lite
# tags: centos,debian,lic_ISPmanager_5_Lite
# revision: 9
# description_ru: Рецепт установки ISPmanager
# description_en: ISPmanager installing recipe
# metadata_end
#

RNAME=install_mgr

set -x

LOG_PIPE=/tmp/log.pipe.$$                                                                                                                                                                                                                    
mkfifo ${LOG_PIPE}
LOG_FILE=/root/${RNAME}.log
touch ${LOG_FILE}
chmod 600 ${LOG_FILE}

tee < ${LOG_PIPE} ${LOG_FILE} &

exec > ${LOG_PIPE}
exec 2> ${LOG_PIPE}

killjobs() {
	jops="$(jobs -p)"
	test -n "${jops}" && kill ${jops} || :
}
trap killjobs INT TERM EXIT

echo
echo "=== Recipe ${RNAME} started at $(date) ==="
echo

HOME=/root
export HOME

if [ -f /etc/redhat-release ]; then
	OSNAME=centos
else
	OSNAME=debian
fi

set -x

if [ "#${OSNAME}" = "#centos" ]; then

	if [ ! "($HTTPPROXYv4)" = "()" ]; then
		# Стрипаем пробелы, если они есть
		PR=($HTTPPROXYv4)
		PR=$(echo ${PR} | sed "s/''//g" | sed 's/""//g')
		if [ -n "${PR}" ]; then
			echo "proxy=${PR}" >> /etc/yum.conf
#			export http_proxy="${PR}"
#			export HTTP_PROXY="${PR}"
		fi
	fi

#	sed -i"ispbak" -r "s/^(mirrorlist=)/#\1/g; s/^#(baseurl=)/\1/g" /etc/yum.repos.d/*.repo

	echo "Installing ispmgr5"
	cd /root
	selinuxenabled && echo "selinux enabled"

	curl -o install.5.sh "http://download.ispsystem.com/install.5.sh" || (sleep 1; curl -o install.5.sh "http://download.ispsystem.com/install.5.sh") || (sleep 1; curl -o install.5.sh "http://download.ispsystem.com/install.5.sh")
	sh install.5.sh --silent --ignore-hostname --release stable ispmanager-lite

	if [ "#$(rpm -qf /etc/centos-release --qf "%{version}")" = "#7" ]; then
		# centos-7
		rpm -e --nodeps ispmanager-pkg-httpd
		yum -y install ispmanager-pkg-httpd-itk
	fi
	yum clean all

	sed -i -r "s|^short_open_tag = Off|short_open_tag = On|" /etc/php.ini
	if [ -n "($TIMEZONE)" ] && [ ! "($TIMEZONE)" = "()" ]; then
		if ! grep -q "^date.timezone =" /etc/php.ini ; then
			sed -i -r "s|;date.timezone =|date.timezone = ($TIMEZONE)|" /etc/php.ini
		else
			sed -i -r "s|^date.timezone = .+|date.timezone = ($TIMEZONE)|" /etc/php.ini
		fi
	fi
	/usr/local/mgr5/sbin/mgrctl -m ispmgr -R feature.update sok=ok

#	for file in /etc/yum.repos.d/*.repoispbak; do mv -f $file $(echo $file|sed 's/ispbak//'); done
	sed -r -i "/proxy=/d" /etc/yum.conf
	echo "Installation finished at $(date)"
else
	wget "http://download.ispsystem.com/install.5.sh" || (sleep 1; wget "http://download.ispsystem.com/install.5.sh") || (sleep 1; wget "http://download.ispsystem.com/install.5.sh")
	sh install.5.sh --silent --ignore-hostname --release stable ispmanager-lite
fi

echo "utf-8" >> /usr/local/mgr5/etc/charset
echo "windows-1251" >> /usr/local/mgr5/etc/charset

/usr/local/mgr5/sbin/mgrctl -m ispmgr db.server.edit elid=MySQL sok=ok || exit 1
