#!/bin/bash
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
#

#
# Plesk script
#


### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.


true apache_status_linux_debian

apache_status_linux_debian()
{
	get_pid "/usr/sbin/apache2" false
	local pid=$common_var
	if test "$pid" -ne 1; then
# running
		return 0
	fi
	return 1
}

true drweb_status
drweb_status()
{
	local pidfile="/var/drweb/run/drwebd.pid"
	if [ ! -r "$pidfile" ]; then
		p_echo "drweb is stopped (no pidfile found)"
		return 1
	fi

	local pid=$(head -1 "$pidfile" 2>/dev/null)
	if  [ -z "$pid" ]; then
		p_echo "drweb is stopped (wrong pidfile)"
		return 1
	fi

	if kill -0 "$pid" 2>/dev/null || ps -p "$pid" >/dev/null 2>&1 ; then
		p_echo "drwebd (pid $pid) is running..."
		return 0
	fi
	p_echo "drwebd is stopped"
	return 1
}

### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
# vim:ft=sh
# Usage:  pleskrc <service> <action>
pleskrc()
{
	[ 2 -le $# ] || die "Not enough arguments"

	local service_name=$1
	local action=$2
	local ret=0
	local inten
	shift
	shift

	# Now check redefined functions
	if test "$machine" = "linux" && is_function "${service_name}_${action}_${machine}_${linux_distr}"; then
		"${service_name}_${action}_${machine}_${linux_distr}" "$@"
		return $?
	elif is_function "${service_name}_${action}_${machine}"; then
		"${service_name}_${action}_${machine}" "$@"
		return $?
	elif is_function "${service_name}_${action}"; then
		"${service_name}_${action}" "$@"
		return $?
	fi

	# Not redefined - call default action
	eval "service=\$${service_name}_service"
	[ -n "$service" ] || die "$action $service_name service (Empty service name for '$service_name')"

	inten="$action service $service"
	[ "$action" = "status" -o "$action" = "exists" ] || echo_try "$inten"

	service_ctl "$action" "$service" "$service_name"

	ret="$?"
	if [ "$action" != "status" -a "${action}" != "exists" ]; then
		if [ "$ret" -eq 0 ]; then
			suc
		else
			if [ -x "/bin/systemctl" ]; then
				/bin/systemctl -l status "${service}.service" | awk 'BEGIN {s=0} s==1 {s=2} /^$/ {s=1} s==2 {print}' | tee /tmp/service_control.log >> "$product_log"
			fi
			warn "$inten"
		fi
	fi

	return $ret
}

# NOTE:
#	Function service_ctl is just helper for pleskrc().
#	Do not call it directly, use pleskrc()!!!
service_ctl()
{
	local action=$1
	local service=$2
	local service_name=$3

	if [ "$action" != "exists" ]; then
		_service_exec $service exists;
		if [ "$?" != "0" ]; then
			warn "attempt to ${inten} - control script doesn't exist or isn't executable"
			return 1
		fi
	fi

	case "$action" in
		start)
			pleskrc "$service_name" status || _service_exec "$service" "$action"
			;;
		stop)
			! pleskrc "$service_name" status || _service_exec "$service" "$action"
			;;
		restart)
			if pleskrc "$service_name" status; then
				_service_exec "$service" "$action"
			else
				_service_exec "$service" start
			fi
			;;
		reload)
			! pleskrc "$service_name" status || _service_exec "$service" "$action"
			;;
		status)
			_service_exec "$service" status
			;;
		try-restart)
			if [ -x "/bin/systemctl" ]; then
				_service_exec "$service" "$action"
			else
				! pleskrc "$service_name" status || _service_exec "$service" "restart"
			fi
			;;
		try-reload)
			! pleskrc "$service_name" status || _service_exec "$service" "reload"
			;;
		reload-or-restart)
			if [ -x "/bin/systemctl" ]; then
				_service_exec "$service" "$action"
			elif pleskrc "$service_name" status; then
				_service_exec "$service" "reload"
			else
				_service_exec "$service" "start"
			fi
			;;
		*)
			_service_exec "$service" "$action"
			;;
	esac >> "$product_log"
}

_service_exec()
{
	local service=$1
	local action=$2

	local action_cmd
	local sysvinit_service="/etc/init.d/$service"

	if [ -x "/bin/systemctl" ]; then
		case "${action}" in
			exists)
				if /bin/systemctl list-unit-files | awk 'BEGIN { rc = 1 } $1 == "'$service'.service" { rc = 0;} END { exit rc }'; then
					return 0 # systemd unit
				elif [ -x "$sysvinit_service" ]; then
					return 0 # sysvinit compat
				fi
				return 1 # not found
				;;
			status)
				action="is-active"
				;;
			reload|graceful)
				action='reload-or-try-restart'
				;;
		esac
		/bin/systemctl "$action" "${service}.service"
	elif  [ -x "/sbin/initctl" -a -e "/etc/init/$service.conf" ]; then  # upstart (ubuntu)
		if [ "$action" = "status" ]; then
			/sbin/initctl status "$service" | grep -qE ' ([0-9]+)$' && return 0 || return 1
		elif [ "$action" = "exists" ]; then
			return 0
		else
			/sbin/initctl "$action" "$service"
		fi
	else
		if [ -x "/usr/sbin/invoke-rc.d" ]; then
			action_cmd="/usr/sbin/invoke-rc.d $service"
		elif [ -x "/sbin/service" ]; then
			action_cmd="/sbin/service $service"
		elif [ -x "/usr/sbin/service" ]; then
			action_cmd="/usr/sbin/service $service"
		else
			action_cmd="$sysvinit_service"
		fi

		if [ "$action" = "exists" ]; then
			[ -x "$sysvinit_service" ] && return 0 || return 1
		else
			$action_cmd $action 2>/dev/null
		fi
	fi
}

is_function()
{
	local type_output=$(type -t "$1")
	test "X${type_output}" = "Xfunction"
}

# echo message to product log, unless debug
p_echo()
{
    if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" -o -z "$product_log" ] ; then
        echo "$@" >&2
    else
        echo "$@" >> "$product_log" 2>&1
    fi
}

# echo message to product log without new line, unless debug
pnnl_echo()
{
    if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" -o -z "$product_log" ] ; then
        echo -n "$*" >&2
    else
        echo -n "$*" >> "$product_log" 2>&1
    fi
}

die()
{
	PACKAGE_SCRIPT_FAILED="$*"

	report_problem \
		"ERROR while trying to $*" \
		"Check the error reason(see log file: ${product_log}), fix and try again"

	selinux_close

	exit 1
}

warn()
{
	local inten
	inten="$1"
	p_echo
	p_echo "WARNING!"
	pnnl_echo "Some problems are found during $inten"
	p_echo "(see log file: ${product_log})"
	p_echo
	p_echo "Continue..."
	p_echo

	product_log_tail | send_error_report_with_input "Warning: $inten"

	[ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ] || \
	product_log_tail
}

# Use this function to report failed actions.
# Typical report should contain
# - reason or problem description (example: file copying failed)
# - how to resolve or investigate problem (example: check file permissions, free disk space)
# - how to re-run action (example: perform specific command, restart bootstrapper script, run installation again)
report_problem()
{
	[ -n "$product_problems_log" ] || product_problems_log="/dev/stderr"

	p_echo
	if [ "0$problems_occured" -eq 0 ]; then
		echo "***** $process problem report *****" >> "$product_problems_log" 2>&1
	fi
	for problem_message in "$@"; do
		p_echo "$problem_message"
		echo "$problem_message" >> "$product_problems_log" 2>&1
	done
	p_echo

	product_log_tail | send_error_report_with_input "Problem: $@"

	[ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ] || \
		product_log_tail

	problems_occured=1
}

echo_try()
{
	msg="$*"
	pnnl_echo " Trying to $msg... "
}

suc()
{
	p_echo "done"
}

# do not call it w/o input! Use send_error_report in these cases.
send_error_report_with_input()
{
	get_product_versions
	{
		echo "$@"
		echo ""
		if [ -n "$error_report_context" ]; then
			echo "Context: $error_report_context"
			echo ""
		fi
		if [ -n "$RP_LOADED_PATCHES" ]; then
			echo "Loaded runtime patches: $RP_LOADED_PATCHES"
			echo ""
		fi
		cat -
	} | $PRODUCT_ROOT_D/admin/bin/send-error-report --version "$product_this_version" install >/dev/null 2>&1
}
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
reexec_with_clean_env()
{
	# Usage: call this function as 'reexec_with_clean_env "$@"' at the start of a script.
	#        Don't use with scripts that require sensitive environment variables.
	#        Don't put the call under any input/output redirection.
	# Purpose: make sure the script is executed with a sane environment.

	export LANG=C LC_MESSAGES=C LC_ALL=C
	export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
	umask 022

	[ -z "$PLESK_INSTALLER_ENV_CLEANED" ] || { unset PLESK_INSTALLER_ENV_CLEANED; return 0; }
	[ -n "$BASH" ] || exec /bin/bash "$0" "$@"

	# N.B.: the following code requires Bash. On Dash it would cause syntax error upon parse w/o eval.
	eval '
	local extra_vars=()                     # list of variables to preserve
	for var in "${!PLESK_@}"; do            # enumerate all PLESK_* variables
		extra_vars+=("$var=${!var}")
	done
	extra_vars+=("PLESK_INSTALLER_ENV_CLEANED=1")

	# Exec self with clean env except for extra_vars, shell opts, and arguments.
	exec /usr/bin/env -i "${extra_vars[@]}" /bin/bash ${-:+-$-} "$0" "$@" || {
		echo "Failed to reexec self ($0) with clean environment" >&2
		exit 91		# Just some relatively unique error code
	}
	'
}

detect_vz()
{
	[ -z "$PLESK_VZ_RESULT" ] || return $PLESK_VZ_RESULT

	PLESK_VZ_RESULT=1
	PLESK_VZ=0
	PLESK_VE_HW_NODE=0
	PLESK_VZ_TYPE=

	local issue_file="/etc/issue"
	local vzcheck_file="/proc/self/status"
	[ -f "$vzcheck_file" ] || return 1

	local env_id=`sed -ne 's|^envID\:[[:space:]]*\([[:digit:]]\+\)$|\1|p' "$vzcheck_file"`
	[ -n "$env_id" ] || return 1
	if [ "$env_id" = "0" ]; then
		# Either VZ/OpenVZ HW node or unjailed CloudLinux
		PLESK_VE_HW_NODE=1
		return 1
	fi

	if grep -q "CloudLinux" "$issue_file" >/dev/null 2>&1 ; then
		return 1
	fi

	if [ -f "/proc/vz/veredir" ]; then
		PLESK_VZ_TYPE="vz"
	elif [ -d "/proc/vz" ]; then
		PLESK_VZ_TYPE="openvz"
	fi

	PLESK_VZ=1
	PLESK_VZ_RESULT=0
	return 0
}

call_optional_function()
{
	export LANG=C LC_MESSAGES=C LC_ALL=C
	local type_output="`type \"$1\" 2>/dev/null | head -n 1`"
	case "$type_output" in
		*function)
			"$@"
			;;
		*)
			return 0
			;;
	esac
}

product_log_tail()
{
	[ -f "$product_log" ] || return 0
	{
		tac "$product_log" | awk '/^START/ { exit } { print }' | tac
	} 2>/dev/null
}
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.

get_pid()
{
	local i

	local ex_f="$1"
	local opt="$2"
	local owner="$3"

	local min_num="1"

	# Use pidof by default, bug 121868, except for FreeBSD - 140182
	if type pidof >/dev/null 2>&1 && [ "$os" != "BSD" ]; then
		for pid in `pidof -o $$ -o $PPID -o %PPID -x $ex_f`; do
			# Check for owner
			[ "$opt" = "true" -a "$owner" != "`ps -p $pid -o ruser=`" ] && continue
			min_num=$pid
			break
		done
		common_var=$min_num
		return $min_num
	fi

	case "$opt" in
		false)
			for i in `$ps_long | grep $ex_f | grep -v grep | grep -v httpsdctl | grep -v apachectl | awk '{print $2}' -`; do
				min_num=$i
				break
			done
			;;
		true)
			for i in `$ps_long | grep $ex_f | grep -v grep | grep -v httpsdctl | grep -v apachectl | grep "$owner" | awk '{print $2}' -`; do
				min_num=$i
				break
			done
			;;
		*)
			p_echo "get_pid: wrong parameter"
			die "get_pid $ex_f $opt $owner"
			;;
	esac

	common_var=$min_num
	return $min_num
}
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.

#
# Support for runtime patching of shell scripts (including utilities and package scripts).
#

# --- Service functions ---

# Load and apply a patch in a relatively safe way
rp_safe_load_patch()
{
	local patch_file="$1"
	echo_try "load shell patch '$patch_file'"
	/bin/sh -n "$RP_BASEDIR/$patch_file" && 
	{
		. "$RP_BASEDIR/$patch_file"
		RP_LOADED_PATCHES="$RP_LOADED_PATCHES $patch_file"
	} &&
	suc
}

# Apply patches specific to the current context (e.g., depending on utility basename or package name)
# This is currently not implemented. This may be overriden by "spark".
rp_patch_runtime_context_specific()
{
	:
}

# --- Main entry points ---

rp_patch_runtime()
{
	# List of loaded patch files
	RP_LOADED_PATCHES=

	local RP_BASEDIR="$PRODUCT_BOOTSTRAPPER_DIR/rp"
	[ -d "$RP_BASEDIR" ] || return 0

	if [ -r "$RP_BASEDIR/spark" ]; then
		rp_safe_load_patch "spark"
	fi

	call_optional_function rp_patch_runtime_context_specific "$@"
}
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.

#-*- vim:ft=sh

register_service() {

	[ -n "$1" ] || die "register_service: service name not specified"
	local inten="register service $1"
	echo_try "$inten"

	{


		# chkconfig for RedHat based
		/sbin/chkconfig --add "$1"
		/sbin/chkconfig "$1" on


		local rs_db="$PRODUCT_ROOT_D/admin/sbin/register_service_db"
		[ ! -x "$rs_db" ] || "$rs_db" -a "$@"
	}

	suc
}

selinux_close()
{
	if [ -z "$SELINUX_ENFORCE" -o "$SELINUX_ENFORCE" = "Disabled" ]; then
		return
	fi

	setenforce "$SELINUX_ENFORCE"
}
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
# vim:ft=sh:

#set_params

set_common_params()
{
	common_var=0

	PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
	LANG=C
	export PATH LANG
	unset GREP_OPTIONS
	umask 022
	ulimit -n 65535 2>/dev/null

	K_HUP="/bin/kill -HUP"
	K_KILL="/bin/kill -KILL"
	K_TERM="/bin/kill -TERM"
	K_USR2="/bin/kill -USR2"
	K_TEST="/bin/kill -0"

	users_created=""
	groups_created=""

	certificate_file="$PRODUCT_ETC_D/httpsd.pem"
	services="/etc/services"
	mtab="/etc/mtab"
	get_hostname="hostname"
	get_domainname="domainname"

	#default parameters
	tar="tar"
	crontab="/usr/bin/crontab"

	cp_preserve="cp -p"
	SYSTEM_RC_D=/etc/init.d
	PLESK_LIBEXEC_DIR="/usr/lib64/plesk-9.0"
	PLESK_DB_DIR="/var/lib/plesk"
	POSTFIX_LIBEXEC_DIR="/usr/libexec/postfix"
	PRODUCT_BOOTSTRAPPER_DIR="/usr/local/psa/bootstrapper/pp17.5.3-bootstrapper"
	AUTOGENERATED_CONFIGS="#ATTENTION!\n#\n#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,\n#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.\n"
	AUTOGENERATED_CONFIGS_UPGRADE="#ATTENTION!\n#\n#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,\n#SO ALL YOUR CHANGES WILL BE LOST AFTER YOU UPGRADE PARALLELS PLESK PANEL.\n"
	PRODUCT_LOGS_D="/var/log/plesk"

	set_common_params_linux 

	rp_patch_runtime
}

set_common_params_linux()
{
	get_hostname="hostname -f"
	fstab="/etc/fstab"
	cp_preserve="cp --preserve=all --remove-destination"
	machine="linux"
	sendmail="/usr/sbin/sendmail"
	ps="ps axw"
	ps_long="ps axuw"
	false_shell="/bin/false"
	dummy_home="/"
	compress="gzip -9 -c"
	uncompress="gunzip -c"
	uudecode="uudecode -o /dev/stdout"
	ifconfig="/sbin/ifconfig -a"
	inet_str="inet addr"

	useradd_options="-r"
	if [ -f /etc/SuSE-release ]; then
		linux_distr="suse"
	elif [ -f /etc/debian_version ]; then
		linux_distr="debian"
		get_domainname="dnsdomainname"
	else
		linux_distr="redhat"
	fi

	sndml_ini="/etc/init.d/sendmail"
	mail_local="/usr/libexec/mail.local"
	if [ -x /sbin/nologin ]; then
		dummy_shell="/sbin/nologin"
	else
		dummy_shell="/bin/false"
	fi
	bash_shell="/bin/bash"
	rbash_shell="/bin/rbash"
	uudecode_full="/usr/bin/uudecode"
	# FIXME: remove or keep? can't find use of this variable
	# named_osrelease=`perl -F"/[.-]/" -n -a  -e 'printf "%02u%02u%02u\n", $F[0],$F[1],$F[2]' /proc/sys/kernel/osrelease`

	return 0
}
## @@constructor set_syslog_params

true syslog_status_linux_debian
syslog_status_linux_debian()
{
	get_pid "$syslog_binary" false
	local pid=$common_var
	if test "$pid" -ne 1; then
		# running
		return 0
	fi
	return 1
}

true syslog_reload_linux_suse
true syslog_reload

syslog_reload_linux_suse()
{
	if [ "$syslog_service" = "rsyslog" -a "$syslog_binary" = "/sbin/rsyslogd" ]; then
		# Suse 13.1 man 8 rsyslogd:
		# So it is advised to use HUP only for closing files, and a "real restart" (e.g. /etc/rc.d/rsyslogd restart) to activate configuration changes.
		service_ctl restart $syslog_service syslog
	else
		service_ctl reload $syslog_service syslog
	fi
}

syslog_reload()
{
	# set_syslog_params must be called in outed function

	if [ "X$syslog_service" = "Xrsyslog" ]; then
		# Bug 142129
		# rsyslog service registration is necessary
		# it is workaround on default rsyslog service registration behaviour
		register_service rsyslog
		# then we restart it
	fi

	detect_vz
	if [ "$syslog_service" = "syslog" -o "$syslog_service" = "rsyslog" ] && [ "$PLESK_VZ" = "1" ]; then
		# 146355 - rsyslog/syslog service returns false on VZ
		service_ctl restart $syslog_service syslog
	elif [ "X$syslog_service" = "Xrsyslog" ]; then
		service_ctl restart $syslog_service syslog
	else
		service_ctl reload $syslog_service syslog
	fi
}

get_product_versions()
{
	local prod_root_d="/usr/local/psa"
	
	product_name="psa"
	product_this_version="17.5.3"
	product_this_version_tag="testing"
	if [ -z "$product_prev_version" ]; then
		if [ -r "$prod_root_d/version.upg" ]; then
			product_prev_version=`awk '{ print $1 }' "$prod_root_d/version.upg"`
		elif [ -r "$prod_root_d/version" ]; then
			product_prev_version=`awk '{ print $1 }' "$prod_root_d/version"`
		else
			product_prev_version="$product_this_version"
		fi
	fi
}

#Invoke mysql
mysql()
{
	mysql_anydb -D$mysql_db_name "$@"
}

mysql_anydb()
{
	(
		export MYSQL_PWD="$mysql_passwd"
		$mysql_client $mysql_host $mysql_user $mysql_args "$@" 2>>"$product_log"
		local status=$?

		if [ $status -gt 0 ]; then
			$mysql_client $mysql_host $mysql_user $mysql_args -D$mysql_db_name $mysql_args_raw -e "SHOW ENGINE innodb status" >>"$product_log" 2>&1
		fi
		unset MYSQL_PWD
		return $status
	)
}
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.
# -*- vim:ft=sh

# MySQL service action handlers

true mysql_start_linux_suse
mysql_start_linux_suse()
{
	local rc
	
	inten="start service mysql"
	echo_try "$inten"

	service_ctl start $mysql_service mysql
	rc="$?"

	# bug 52690. MySQL init script reports failure if protected mysqld is running (true for SuSE >= 11.3)
	if [ "$rc" -ne 0 ]; then
		local mysqld_bin="/usr/sbin/mysqld"
		killall -TERM mysqld >> $product_log 2>&1
		if [ -x "$mysqld_bin" ]; then
			for i in 2 4 8 16 32; do
				get_pid "$mysqld_bin" false
				local pid="$common_var"
				if test "$pid" -eq 1; then
					break
				fi
				killall -TERM mysqld >> $product_log 2>&1
				sleep $i
			done
		fi
		service_ctl start $mysql_service mysql
		rc="$?"
	fi

	[ "$rc" -eq 0 ] && suc || warn "$inten"
	return $rc
}

###	FIXME: probably need var service_restart warn
true mysql_stop
mysql_stop()
{
	local op_result i

	inten="stop MySQL server"
	echo_try $inten

	service_ctl stop $mysql_service mysql
	op_result=$?

	if [ "X$linux_distr" = "Xdebian" ]; then
		# Debian has well designed mysql stopping code
		[ "$op_result" -eq 0 ] || die $inten
		suc
		return 0
	fi

	for i in 2 4 6 8 16; do
		if ! mysql_status ; then
			suc
			return 0
		fi

		# I just want to be sure that mysql really stopped
		killall -TERM mysqld mysql safe_mysqld mysqld_safe >> $product_log 2>&1

		sleep $i
	done

	die "$inten"
}

true mysql_status
mysql_status()
{
	local file

    #Check with native script first
	#debian script always return 0. bug #111825
	[ "X$linux_distr" = "Xdebian" ] && msqld_status_supported="no"
	
	if [ -z "$msqld_status_supported" ]; then
		# MySQL AB packages doesn't know about status command
		if LC_MESSAGES=C $PRODUCT_RC_D/$mysql_service 2>&1 | grep -q "status"; then
			msqld_status_supported="yes"
		else
			msqld_status_supported="no"
		fi
	fi

	if [ "$msqld_status_supported" = "yes" ]; then
		service_ctl status $mysql_service mysql && return 0
	fi

	if [  "$msqld_status_supported" = "no" ]; then
		# MySQL AB packages
		file="/usr/sbin/mysqld"
	fi

    if [ -x "$file" ]; then
		#standard build and debian
		get_pid "$file" false
		pid=$common_var
		if test "$pid" -ne 1; then
			echo "$file (pid $pid) is running..." >>$product_log 2>&1
			return 0
		else
			echo "$file is stopped" >>$product_log 2>&1
			return 1
		fi
	fi

	return 1
}


true named_status_linux_debian
named_status_linux_debian()
{
    get_pid "/usr/sbin/named" false
    local pid=$common_var
    if test "$pid" -ne 1; then
# running
		return 0
    fi
    return 1
}

true exim_status_linux_debian
exim_status_linux_debian()
{
	get_pid /usr/lib/exim/exim3 false
	local pid=$common_var

	if test "$pid" -ne 1; then
		#running
		return 0;
	fi
	return 1
}

true postfix_status
postfix_status()
{
	# here be dragons.
	# the practical experience shows that simple checking of status of
	# Postfix "master" process is not enough. So we read Postfix master
	# process pid file if any, then try to look for a process with
	# name ``qmgr'' and parent pid being equal to
	# the pid read from the pidfile. If pgrep finds such a process
	# it returns 0, if not its exit status is non-zero.
	# pgrep is portable enough to prefer it to "hand-made" alternatives
	# such as famous ``ps | grep $name | grep -v grep...'' pipes
	# bug 147822. do not interrupt installation for FreeBSD

	[ -f "/var/spool/postfix/pid/master.pid" ] || return 1

	local ppid

	read ppid </var/spool/postfix/pid/master.pid 2>/dev/null
	if [ $? -ne 0 -o -z "$ppid" ]; then
		# not found or other error
		return 1;
	fi
	pgrep -P $ppid qmgr >/dev/null 2>/dev/null
}

# vim:ft=sh:
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.

reexec_with_clean_env "$@"

# constants
PN=`basename $0`

usage()
{
	cat << EOT
Usage: $PN [--nginx|--apache] --set ULIMIT [--no-restart]

Increase limit on number of open files for web-servers ("ulimit nofile").
Note that if existing ulimits are greater than new ones then they are left unchanged.

Commands:
    -s, --set ULIMIT        set number of open file ulimits (ulimit -n)

Options:
    -n, --nginx             manage nginx ulimits
    -a, --apache            manage apache ulimits
    -R, --no-restart        do not reconfigure/restart webservers
EOT
}

get_nginx_ulimit_sysvinit()
{
	(
		. "$1"
		eval set -- "$NGINX_ULIMIT"
		if [ -z "$1" ]; then
			echo 0
			return
		fi

		if [ "$1" = "-n" ]; then
			shift
		fi

		if ! echo "$1" | egrep -q '^[0-9]+$'; then
			echo 0
			return
		fi

		echo "$1"
	)
}

set_nginx_default_ulimit_sysvinit()
{
	local ulimits="$1"
	local nginx_default_conf="/etc/sysconfig/nginx"
	local option="NGINX_ULIMIT"

	# format: NGINX_ULIMIT="-n 4096"
	# What's happenning:
	# 1. Search for NGINX_ULIMIT option. 2. Check if old ulimit is less than new one. 3. Then replace old with new one
	# 4. If NGINX_ULIMIT=... is not found then write it into the end of file
	local got_ulimit="`get_nginx_ulimit_sysvinit $nginx_default_conf`"
	if [ "$got_ulimit" -lt "$ulimits" ]; then
		perl -nle '/^\s*'$option'\s*=/ and $found=1 and s/^\s*('$option')\s*=.*$/$1="-n '$ulimits'"/g; print }
			{ print "'$option'=\"-n '$ulimits'\"" unless $found' \
			"$nginx_default_conf" > "$nginx_default_conf.new" && \
			mv -f "$nginx_default_conf.new" "$nginx_default_conf"
	fi
}

set_service_ulimit_systemd()
{
	local nginx_ulimits_conf="$1"
	local ulimits="$2"
	local section="Service"
	local option="LimitNOFILE"

	mkdir -p `dirname "$nginx_ulimits_conf"`

	# format:
	# [Service]
	# LimitNOFILE=4096

	# What's happenning:
	# 1. Check if we are inside section [Service]
	# 2. If inside, search for LimitNOFILE=... 3. if new limit is bigger 4. then replace old with new one
	# 5. print line
	# 6. If LimitNOFILE was not found then: print section header if we are not inside the section; print LimitNOFILE=new_value
	perl -nle '$inside = m/^\s*\['$section'\]/ ... m/^\s*\[.+\]/;
				if ($inside) { m/^\s*'$option'\s*=\s*(\d+)/ and $found=1 and $1 < '$ulimits' and s/^\s*('$option')\s*=\s*\d+.*$/$1='$ulimits'/ }
				print }
				{ unless ($found) { $inside or print "['$section']"; print "'$option'='$ulimits'" }' \
		"$nginx_ulimits_conf" > "$nginx_ulimits_conf.new" 2>/dev/null && \
	mv -f "$nginx_ulimits_conf.new" "$nginx_ulimits_conf"

	systemctl --system daemon-reload
}

set_nginx_default_ulimit()
{
	if [ -n "" ]; then
		set_service_ulimit_systemd "/nginx.service.d/limit_nofile.conf" "$@"
	else
		set_nginx_default_ulimit_sysvinit "$@"
	fi
}

set_nginx_worker_rlimit_nofile()
{
	local ulimits="$1"
	local nginx_global_params_conf="/etc/nginx/ulimit.global_params"
	local option="worker_rlimit_nofile"

	# if file 'global_params' does not exist then it will be created.
	# format: "worker_rlimit_nofile 4096;"
	perl -nle '/^\s*'$option'\s+(\d+)\s*;/ and $found=1 and $1 < '$ulimits' and s/^\s*('$option')\s+\d+;.*$/$1 '$ulimits';/g; print }
					{ print "'$option' '$ulimits';" unless $found' \
		"$nginx_global_params_conf" > "$nginx_global_params_conf.new" 2>/dev/null && \
	mv -f "$nginx_global_params_conf.new" "$nginx_global_params_conf"
}

set_apache_ulimit_debian()
{
	local ulimits="$1"
	local apache_envvars_conf="/etc/apache2/envvars"
	local option="APACHE_ULIMIT_MAX_FILES"

	# format: "APACHE_ULIMIT_MAX_FILES='ulimit -n 4096'"
	perl -nle '/^\s*'$option'\s*=\s*'\''\s*ulimit\s+\-n\s+(\d+)'\''/ and $found=1 and $1 < '$ulimits' and s/^\s*('$option')\s*=.*$/$1='\''ulimit -n '$ulimits\''/g; print }
					{ print "'$option'='\''ulimit -n '$ulimits\''" unless $found' \
		"$apache_envvars_conf" > "$apache_envvars_conf.new" && \
	mv -f "$apache_envvars_conf.new" "$apache_envvars_conf"
}

set_apache_ulimit_redhat()
{
	local ulimits="$1"
	local apache_conf="/etc/sysconfig/httpd"

	# It's a bit tricky to specify command in '/etc/sysconfig/httpd' but it should work since it is directly included into init-script.

	# format: "ulimit -n 4096"
	perl -nle '/^\s*ulimit\s+\-n\s+(\d+)/ and $found=1 and $1 < '$ulimits' and s/^\s*(ulimit\s+\-n)\s+\d+/$1 '$ulimits'/; print }
					{ print "ulimit -n '$ulimits'" unless $found' \
		"$apache_conf" > "$apache_conf.new" && \
	mv -f "$apache_conf.new" "$apache_conf"
}

set_apache_ulimit()
{
	if [ -n "" ]; then
		set_service_ulimit_systemd "/httpd.service.d/limit_nofile.conf" "$@"
	elif [ "$linux_distr" = "debian" ]; then
		# includes debian and ubuntu
		set_apache_ulimit_debian "$@"
	elif [ "$linux_distr" = "redhat" ]; then
		#includes redhat and centos <= 6.x
		set_apache_ulimit_redhat "$@"
	else
		echo "Unknown OS: setting apache ulimit -n is not implemented for this OS. Stop." >&2
		return 1
	fi
}

#parse options
TEMP=`getopt -o nas:gRh --long nginx,apache,set:,no-restart,help -n "$PN" -- "$@"`

if [ $? != 0 ] ; then echo "Inrernal error: failed to parse command line options." >&2 ; exit 1 ; fi
eval set -- "$TEMP"

opt_nginx=0
opt_apache=0
opt_set=
opt_no_restart=0

while true; do
	case $1 in
		-n|--nginx) opt_nginx=1; shift;;
		-a|--apache) opt_apache=1; shift;;
		-s|--set) opt_set="$2"; shift 2;;
		-R|--no-restart) opt_no_restart=1; shift;;
		-h|--help) usage; exit 0;;
		--) shift; break;;
		*) echo "Unrecognized option: $1" >&2; usage; exit 1;;
	esac
done

set_common_params

if [ "$opt_nginx" -eq 0 -a "$opt_apache" -eq 0 ]; then
	opt_nginx=1
	opt_apache=1
fi

if [ -z "$opt_set" ]; then
	echo "Option --set is not specified." >&2
	exit 1
fi

if [ "$opt_set" -gt 0 ]; then
	if ! echo "$opt_set" | egrep -q '^[0-9]+$'; then
		echo "Incorrect parameter --set, expected integer, found: '$opt_set'" >&2
		exit 1
	fi

	if [ "$opt_apache" -gt 0 ]; then
		set_apache_ulimit "$opt_set" || exit 1
	fi

	if [ "$opt_nginx" -gt 0 ]; then
		set_nginx_default_ulimit "$opt_set" || exit 1
		set_nginx_worker_rlimit_nofile "$opt_set" || exit 1
	fi

	[ "$opt_no_restart" -eq 1 ] || "/usr/local/psa/admin/sbin/httpdmng" --reconfigure-all
fi

# vim: ft=sh
