#!/bin/bash
# vim: ts=4 noexpandtab

MGRDIR="/usr/local/mgr5"
MGRCTL="${MGRDIR}/sbin/mgrctl"

OSType() {
	if [ -f /etc/redhat-release  ]; then
		echo REDHAT
		return
	elif [ -f /etc/debian_version ]; then
		echo DEBIAN
		return
	elif [ "$(uname)" = "FreeBSD" ]; then
		echo FREEBSD
		return
	fi
}

OSTYPE=$(OSType)

if [ "x$(OSType)" = "xDEBIAN" ]; then
	MYSQL_SERVICE=mysql
elif [ "x$(OSType)" = "xREDHAT" ]; then
	OSVERSION=$(rpm -q --qf "%{version}" -f /etc/redhat-release | cut -d. -f1)
	export OSVERSION
	if [ "$(rpm -q --qf "%{version}" -f /etc/redhat-release)" = "7" ]; then
		# systemd
		if [ -f /usr/lib/systemd/system/mariadb.service ]; then
			MYSQL_SERVICE=mariadb
		fi
	else
		MYSQL_SERVICE=mysqld
	fi
elif [ "x$(OSType)" = "xFREEBSD" ]; then
	MYSQL_SERVICE=mysql-server
fi

Errorn() {
	printf "\033[1;31m$@\033[0m"
}

Error() {
	Errorn "$@\n"
}

Infon() {
	printf "\033[1;32m$@\033[0m"
}

Info() {
	Infon "$@\n"
}

ReloadService() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	
	service ${1} restart >/dev/null 2>&1 || :
}

RcconfSet() {
	if [ -z $2 ] || [ -z $1 ]; then
		# Empty argument
		return 1
	fi
	if [ "#${1}" = "#_enable" ]; then
		return 1
	fi
	test -f /etc/rc.conf || exit 1
	if grep -q "${1}=" /etc/rc.conf ; then
		sed -i ".bak" -r "s|^${1}=.*|${1}=\"${2}\"|" /etc/rc.conf
	else
		echo "${1}=\"${2}\"" >> /etc/rc.conf
	fi
}


EnableService() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	if [ -n "${2}" ]; then
		RCNAME=${2}
	else
		RCNAME=${1}
	fi

	service ${1} stop >/dev/null 2>&1 || :
	# If delay is set sleeping
	if [ -n "${RESTART_DELAY}" ]; then
		sleep ${RESTART_DELAY} || :
	fi
	if [ "$(OSType)" = "REDHAT" ]; then
		chkconfig ${1} on >/dev/null 2>&1 || :
	elif [ "$(OSType)" = "DEBIAN" ]; then
		update-rc.d ${1} enable >/dev/null 2>&1 || :
	elif [ "${OSTYPE}" = "FREEBSD" ]; then
		RcconfSet ${RCNAME}_enable YES
	fi	
	service ${1} start >/dev/null 2>&1
}

DisableService() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	if [ -n "${2}" ]; then
		RCNAME=${2}
	else
		RCNAME=${1}
	fi

	service ${1} stop >/dev/null 2>&1 || echo -n ""
	if [ "$(OSType)" = "REDHAT" ]; then 
		chkconfig ${1} off >/dev/null 2>&1 || echo -n ""
	elif [ "$(OSType)" = "DEBIAN" ]; then
		update-rc.d ${1} disable >/dev/null 2>&1 || echo -n ""
	elif [ "${OSTYPE}" = "FREEBSD" ]; then
		RcconfSet ${RCNAME}_enable NO
	fi	
}

Service() {
# $1 - name
# $2 - command
	if [ -n "$(which systemctl 2>/dev/null)" ] || [ -x /usr/bin/systemctl ] ; then
		systemctl ${2} ${1}.service
	else
		if [ "${2}" = "enable" ] || [ "${2}" = "disable" ] ; then
			if [ "${OSTYPE}" = "DEBIAN" ]; then
				update-rc.d ${1} ${2}
			else
				if [ "${2}" = "enable" ]; then
					chkconfig ${1} on
				else
					chkconfig ${1} off
				fi
			fi
		else
			service ${1} ${2}
		fi
	fi
}

ReloadMgr() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi

	if [ "${1}" = "all" ]; then
		${MGRCTL} -l >/dev/null 2>&1
		${MGRCTL} -u >/dev/null 2>&1
	elif [ -f ${MGRDIR}/var/${1}.pid ]; then
		# Pid file exists
		local pid
		pid=$(cat ${MGRDIR}/var/${1}.pid 2>/dev/null)
		if [ -n "${pid}" ] && [ -f /proc/${pid}/status ]; then
			# Proc by PID exists. Call this to exit
			${MGRCTL} -m ${1} exit >/dev/null 2>&1
		fi
	fi
}

AddMgr() {
	[ "#${1}" = "#" ] && return 0
	grep -qE "^manager\s+${1}\$" ${MGRDIR}/etc/mgrlist.conf || echo "manager ${1}" >> ${MGRDIR}/etc/mgrlist.conf
}

RemoveMgr() {
	[ "#${1}" = "#" ] && return 0
	if [ "#${OSTYPE}" = "#FREEBSD" ]; then
		sed -i "" -r "/^manager\s+${1}$/d" ${MGRDIR}/etc/mgrlist.conf
	else
		sed -i -r "/^manager\s+${1}$/d" ${MGRDIR}/etc/mgrlist.conf
	fi
}

LockMgr() {
	if [ "#${1}" = "#" ]; then
		${MGRCTL} -l
	else
		${MGRCTL} -m ${1} -l
	fi
}

UnlockMgr() {
	if [ "#${1}" = "#" ]; then
		${MGRCTL} -u
	else
		${MGRCTL} -m ${1} -u
	fi
}

AfterinstallMgr() {
	if [ "#${1}" = "#" ]; then
		MGR=core
	else
		MGR=${1}
	fi
	${MGRCTL} -m ${MGR} afterinstall xset.up=on >/dev/null 2>&1 &
}

AfterinstallModule() {
	if [ -z ${1} ]; then
		echo "Empty argument MGR"
		return 1
	fi
	if [ -z ${2} ]; then
		echo "Empty argument MODULE"
		return 1
	fi
	MGR=${1}
	MODULE=${2}
	${MGRCTL} -R -m ${MGR} afterinstall elid=${MODULE} xset.up=on >/dev/null 2>&1 &
}

AfterupdateMgr() {
	if [ "#${1}" = "#" ]; then
		MGR=core
	else
		MGR=${1}
		shift
	fi
	if [ -n "${@}" ]; then
		echo "${@}" > ${MGRDIR}/var/${MGR}.startup
	else
		touch ${MGRDIR}/var/${MGR}.startup
	fi
}


TMPCNF=/usr/local/mgr5/tmp/.$$.cnf

check_my_cnf() {
	test -f /root/.my.cnf 2>/dev/null
	return $?
}

mysql_version() {
	# Вернуть полную версию mysql(mariadb)
	# mysqladmin version не слишком удобен для парсинга
	# Вызывать только если есть файл my.cnf у коры
	local ver
	if [ -n "${MGRDIR}" ] && [ -f ${MGRDIR}/etc/my.cnf ]; then
		local MYSQLOPTS="--defaults-file=${MGRDIR}/etc/my.cnf"
	fi
	ver=$(echo "SELECT VERSION()" | mysql ${MYSQLOPTS} -N)
	if [ -z "${ver}" ] && [ "${OSTYPE}" = "REDHAT" ]; then
		ver=$(rpm -qf --qf '%{version}' $(which /usr/sbin/mysqld))
	fi
	echo "${ver}"
}

mysql_short_version() {
	# Вернуть короткую(2 знака) версию mysql(mariadb)
	# Вызывать только если есть файл my.cnf у коры
	mysql_version | awk -F. '{print $1 "." $2}'
}

mysql_update_password() {
	# Сбросить пароль пользователю(но не root)
	# $1 - username
	# $2 - password
	# $@ - mysql arguments
	# Если не нужно звать FLUSH PRIVILEGES, то выставить ENV NOFLUSH
	local USERNAME=${1}
	local PW=${2}
	shift 2
	local MYSQLOPTS="$@"
	if echo ${PW} | grep -q '^\*'; then
		local PW_S="'${PW}'"
		local restores_pw=yes
	else
		local PW_S="PASSWORD('${PW}')"
	fi
	if [ "$(mysql_short_version)" = "5.7" -o "$(mysql_short_version)" = "10.3" ]; then
		# В mysql 5.7 изменили формат таблиц
		echo "UPDATE user SET authentication_string=${PW_S}, password_expired = 'N' WHERE user='${USERNAME}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1 || return 1
	elif [ "$(mysql_short_version)" = "8.0" ]; then
		if [ -n "${restores_pw}" ]; then
			echo "UPDATE user SET authentication_string=${PW_S}, password_expired = 'N' WHERE user='${USERNAME}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1 || return 1
		else
			echo "UPDATE user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('${PW}'))))), password_expired = 'N' WHERE user='${USERNAME}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1 || return 1
		fi
	else
		echo "UPDATE user SET Password=${PW_S} WHERE user='${USERNAME}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1 || return 1
	fi
	if [ -z "${NOFLUSH}" ]; then
		echo "FLUSH PRIVILEGES" | mysql ${MYSQLOPTS} >/dev/null 2>&1 || return 1
	fi
}

check_core_cnf() {
	if [ -f /usr/local/mgr5/etc/my.cnf ]; then
		DLIST=$(check_connect /usr/local/mgr5/etc/my.cnf)
		ret=$?
		echo ${DLIST}
		return $ret
	else
		return 1
	fi
}

check_connect() {
	# Return dblist
	# Если есть аргумент, то это --defaults-file
	if [ -n "$1" ]; then
		MYSQLCOMMAND="mysql --defaults-file=$1 -N"
	else
		MYSQLCOMMAND="mysql -N"
	fi
	DLIST=$(echo "show databases" | ${MYSQLCOMMAND} 2>/dev/null)
	RET=$?
	if [ ${RET} -ne 0 ]; then
		DLIST=""
	fi
	echo ${DLIST}
	return ${RET}
}

delete_empty_passwords() {
    echo "delete from user where password = ''" | mysql -N mysql > /dev/null 2>&1
}

create_root_passwd() {
	PW=$(pwgen -s 10 1)
	export PW
	if [ "$(mysql_short_version)" = "5.7" -o "$(mysql_short_version)" = "10.3" -o "$(mysql_short_version)" = "8.0" ]; then
		# меняем auth plugin на ubuntu-16
		echo "UPDATE mysql.user set plugin='mysql_native_password' WHERE user='root'; FLUSH PRIVILEGES;" | mysql -N
	fi
	sh -c 'mysqladmin -u root password "${PW}"'
	echo "[client]" > /root/.my.cnf 
	chmod 400 /root/.my.cnf
	echo "user = root" >> /root/.my.cnf
	echo "password = ${PW}" >> /root/.my.cnf
	delete_empty_passwords
}

create_coremgr_passwd() {
	if [ -f ${TMPCNF} ]; then
		MYSQLOPTS="--defaults-file=${TMPCNF}"
	else
		MYSQLOPTS=""
	fi
	PW=$(pwgen -s 10 1)
	export PW
	USERNAME=coremgr
	echo "DROP USER '${USERNAME}'@'localhost'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	echo "CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PW}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_update_password ${USERNAME} ${PW} ${MYSQLOPTS}
	fi
	echo "GRANT ALL PRIVILEGES ON *.* TO '${USERNAME}'@'localhost' WITH GRANT OPTION" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	echo "[client]" > /usr/local/mgr5/etc/my.cnf
	chmod 400 /usr/local/mgr5/etc/my.cnf
	echo "user = ${USERNAME}" >> /usr/local/mgr5/etc/my.cnf
	echo "password = ${PW}" >> /usr/local/mgr5/etc/my.cnf
}

skip_grant_to_mycnf() {
	if [ -f /etc/mysql/mysql.conf.d/mysqld.cnf ] && grep -q '\[mysqld\]' /etc/mysql/mysql.conf.d/mysqld.cnf; then
		MYCNF=/etc/mysql/mysql.conf.d/mysqld.cnf
	elif [ -f /etc/mysql/my.cnf ]; then
		MYCNF=/etc/mysql/my.cnf
	elif [ -f /etc/my.cnf.d/mysql-server.cnf ]; then
		MYCNF=/etc/my.cnf.d/mysql-server.cnf
	else
		MYCNF=/etc/my.cnf
	fi
	if grep -q '[mysqld]' ${MYCNF} ; then
		if ! cat ${MYCNF} | sed -n -r '/\[mysqld\]/,/\[/p' | grep -q skip-grant-tables ; then
			sed -i -r '/^\[mysqld\]/a skip-grant-tables' ${MYCNF}
		fi
		if ! cat ${MYCNF} | sed -n -r '/\[mysqld\]/,/\[/p' | grep -q skip-networking ; then
			sed -i -r '/^\[mysqld\]/a skip-networking' ${MYCNF}
		fi
	else
		printf "[mysqld]\nskip-grant-tables\nskip-networking\n" >> ${MYCNF}
	fi
}

skip_grant_from_mycnf() {
	if [ -f /etc/mysql/mysql.conf.d/mysqld.cnf ] && grep -q '\[mysqld\]' /etc/mysql/mysql.conf.d/mysqld.cnf; then
		MYCNF=/etc/mysql/mysql.conf.d/mysqld.cnf
	elif [ -f /etc/mysql/my.cnf ]; then
		MYCNF=/etc/mysql/my.cnf
	elif [ -f /etc/my.cnf.d/mysql-server.cnf ]; then
		MYCNF=/etc/my.cnf.d/mysql-server.cnf
	else
		MYCNF=/etc/my.cnf
	fi
	sed -i -r '/\[mysqld\]/,/\[/ {/^(skip-networking|skip-grant-tables)/d}' ${MYCNF}
}

reset_root_passwd() {
	# Save passwd to ${TMPCNF}
	service ${MYSQL_SERVICE} stop >/dev/null 2>&1
	sleep 2
	if [ -f /bin/systemctl ]; then 
		# Systemd. Can`t stop service by pid
		skip_grant_to_mycnf
		service ${MYSQL_SERVICE} start
		skip_grant_from_mycnf
	else
		mysqld_safe --skip-grant-tables --skip-networking >/dev/null 2>&1 &
	fi
	sleep 2
	MYSQLSOCK=$(mysql --help | grep ^socket | awk '{print $2}')
	if ! echo "${MYSQLSOCK}" | grep -q sock ; then
		MYSQLSOCK=$(/usr/bin/my_print_defaults mysqld socket |sed -n "s/^--socket=//p" | tail -n 1)
	fi
	SLEEPCOUNT=1
	while ! test -S ${MYSQLSOCK}; do
		sleep 2
		SLEEPCOUNT=$(expr ${SLEEPCOUNT} + 1)
		if [ ${SLEEPCOUNT} -gt 5 ]; then # Если мы уже 5 раз ждали мускуля, то больше не ждём и выходим с ошибкой
			break
		fi
	done
	local mysql_short_version_var pwfield
	mysql_short_version_var="$(mysql_short_version)"
	if [ "${mysql_short_version_var}" = "5.7" -o "${mysql_short_version_var}" = "10.3" -o "${mysql_short_version_var}" = "8.0" ]; then
		pwfield=authentication_string
	else
		pwfield=password
	fi
    if [ "${mysql_short_version_var}" = "8.0" ]; then
        OLDPW=$(echo "SELECT ${pwfield} FROM user WHERE user='root' AND ${pwfield}!=''" | mysql -N mysql | head -1)
    else
        OLDPW=$(echo "SELECT ${pwfield} FROM user WHERE user='root' AND password!=''" | mysql -N mysql | head -1)
    fi
	PW=$(pwgen -s 10 1)
	export PW
	mysql_update_password root ${PW}
	echo "[client]" > ${TMPCNF}
	chmod 400 ${TMPCNF}
	echo "password = ${PW}" >> ${TMPCNF}
	service ${MYSQL_SERVICE} restart >/dev/null 2>&1
	delete_empty_passwords
	echo ${OLDPW}
}

restore_root_passwd() {
	if [ ! -f ${TMPCNF} ]; then
		return 0
	fi
	OLDROOTPW=${1}
	MYSQLOPTS="--defaults-file=${TMPCNF}"
	NOFLUSH=yes
	mysql_update_password root ${OLDROOTPW} ${MYSQLOPTS} || return 1
	service ${MYSQL_SERVICE} restart >/dev/null 2>&1
}

create_coremgr_user() {
	create_coremgr_passwd #>/dev/null 2>&1
}

create_db() {
	CORECNF=/usr/local/mgr5/etc/my.cnf
	if [ -z "${1}" ]; then
		return 1
	fi
	local MYSQLOPTS="--defaults-file=${CORECNF}"
	mysqladmin ${MYSQLOPTS} create ${1} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "mysqladmin create database ${1} failed" >&2
	fi
	local USERNAME=${1}
	local PW
	if [ -n "${CREATEDB_PW}" ]; then
		PW=${CREATEDB_PW}
	else
		PW=$(pwgen -s 10 1)
	fi
	echo "CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PW}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_update_password ${USERNAME} ${PW} ${MYSQLOPTS}
		if [ $? -ne 0 ]; then
			echo "create or update mysql user ${USERNAME} failed" >&2
		fi
	fi
	if [ "#${2}" = "#1" ]; then
		echo "GRANT ALL PRIVILEGES ON *.* TO '${USERNAME}'@'localhost'"	| mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	else
		echo "GRANT ALL PRIVILEGES ON ${1}.* TO '${USERNAME}'@'localhost'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	fi
	if [ $? -ne 0 ]; then
		echo "grant privileges to ${USERNAME} failed" >&2
		return 1
	fi
	echo ${PW}
}

create_admin_user() {
	# $1 - username
	local USERNAME=${1}
	CORECNF=/usr/local/mgr5/etc/my.cnf
	local MYSQLOPTS="--defaults-file=${CORECNF}"
	if [ -z "${1}" ]; then
		return 1
	fi
	local PW
	if [ -n "${CREATEDB_PW}" ]; then
		PW=${CREATEDB_PW}
	else
		PW=$(pwgen -s 10 1)
	fi
	echo "CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PW}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_update_password ${USERNAME} ${PW} ${MYSQLOPTS}
		if [ $? -ne 0 ]; then
			echo "create or update mysql user ${USERNAME} failed" >&2
		fi
	fi
	echo "GRANT ALL PRIVILEGES ON *.* TO '${USERNAME}'@'localhost' WITH GRANT OPTION" | mysql ${MYSQLOPTS}  mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "grant privileges to ${USERNAME} failed" >&2
		return 1
	fi
	echo ${PW}

}

MgrInstalled() {
	if [ -z ${1} ]; then echo "Empty arg 1" ; return 1; fi
	if [ -z ${2} ]; then echo "Empty arg 2" ; return 1; fi

	IPADDR=$(echo $SSH_CONNECTION | awk '{print $3}')
	if [ -z ${IPADDR} ]; then
		IPADDR=$(ip addr show | awk '$1 ~ /inet/ && $2 !~ /127.0.0|::1|fe80:/ {print $2}' |cut -d/ -f1 | head -1)
	fi

	Info "================================================="
	Info "$2 is installed"
	Info "Go to the \"https://${IPADDR}:1500/${1}\" to login"
	Info "Login: root"
	Info "Password: <root password>"
	Info "================================================="
}

IsMgrExist() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	grep -qE "^manager\s+${1}\$" ${MGRDIR}/etc/mgrlist.conf 2>/dev/null
	return $?
}

LicError() {
	Error "Trial license for this IP has expired"
	Error "You have no complete license on this manager or it can no be activated automatically"
	Error "If You have that license set environment variable ACTIVATION_KEY"
	exit 1
}

LicCheck() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	MGR=${1}
	FIRSTSTART=yes ${MGRDIR}/sbin/licctl fetch ${MGR} >/dev/null 2>&1 || LicError
}

PathConvert() {
	# $1 - new conf
	test  "#${ISPCONVERT}" = "#yes" || return 1
	test -n "${PATHLIST}" || return 1
	for VAL in ${PATHLIST} ; do
		VALPATH=""
		VALPATH=$(grep -E "^path\s+${VAL}\s+" ${MGRDIR}/etc/*.conf | head -1 | awk '{for(i=3;i<=NF;i++){printf "%s ", $i}; printf "\n"}')
		if [ -n "${VALPATH}" ]; then
			sed --follow-symlinks -i -r "/^path\s+${VAL}\s+/d" ${MGRDIR}/etc/*.conf
			sed --follow-symlinks -i -r "/^path\s+${VAL}\s+/d" $1
			echo "path ${VAL} ${VALPATH}" >> $1
		fi
	done
}

ParamConvert() {
	# $1 - new conf
	test  "#${ISPCONVERT}" = "#yes" || return 1
	test -n "${PARAMLIST}" || return 1
	for VAL in ${PARAMLIST} ; do
		VALPARAM=""
		VALPARAM=$(grep -E "^${VAL}\s+" ${MGRDIR}/etc/*.conf | head -1 | awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}')
		if [ -n "${VALPARAM}" ]; then
			sed --follow-symlinks -i -r "/^${VAL}\s+/d" ${MGRDIR}/etc/*.conf
			sed --follow-symlinks -i -r "/^${VAL}\s+/d" $1
			echo "${VAL} ${VALPARAM}" >> $1
		fi
	done
}

MgrParamConvert() {
	# $1 - mgr
	# $2 - new conf
	test  "#${ISPCONVERT}" = "#yes" || return 1
	test -n "${PARAMLIST}" || return 1
	for VAL in ${PARAMLIST} ; do
		VALPARAM=""
		VALPARAM=$(grep -E "^${VAL}\s+" ${MGRDIR}/etc/${1}.conf | head -1 | awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}')
		if [ -n "${VALPARAM}" ]; then
			sed --follow-symlinks -i -r "/^${VAL}\s+/d" ${MGRDIR}/etc/${1}.conf
			if [ -f ${2} ]; then
				sed --follow-symlinks -i -r "/^${VAL}\s+/d" $2
			fi
			echo "${VAL} ${VALPARAM}" >> $2
			chmod 600 ${2} 2>/dev/null || :
		fi
	done
}


InsertOrReplace() {
	# If param exist, replace it. If it commented put after comment. Else append to end of file
	# $1 - param
	# $2 - value
	# $3 - file
	#
	if grep -qE "^(#\s*){0,1}${1}\s+" ${3} ; then
		if grep -qE "^\s*${1}\s+" ${3} ; then
			sed -i -r "s;(^\s*${1})(\s+).*(#.*)*;\1\2${2}\3;" ${3}
		else
			sed -i -r "0,/^(#\s*${1}\s+.*)/s;;\1\n${1} ${2}\n;" ${3}
		fi
	else
		echo "${1} ${2}" >> ${3}
	fi
}

CronLink() {
	# $1 - mgr
	test -z "${1}" && return 1
	if [ ! -e ${MGRDIR}/sbin/cron-${1} ]; then
		ln -s cron ${MGRDIR}/sbin/cron-${1}
	fi
}

CheckDF() {
	# Check free disk space for centos
	# $1 - partition
	# $2 - min size
	test -n "${1}" || return 1
	test -n "${2}" || return 1

	# skip if silent install
	# shellcheck disable=SC2039
	local cursize=$(df -P -m "${1}" 2>/dev/null | tail -1 | awk '{print $4}')
	test -z "${cursize}" && return 0
	if [ "${cursize}" -lt "${2}" ]; then
		return 1
	fi
}
