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

get_ipv4_address_list()
{
	if test -x /sbin/ip ; then
		/sbin/ip addr list | grep -v 'inet6' | grep -v '127\.[0-9]*\.[0-9]*\.[0-9]*' | sed -n -e 's/^ *inet \([0-9\.]*\).*$/\1/ p'
	else
		local inet_str="inet addr"
		/sbin/ifconfig -a | grep "$inet_str" | grep -v "inet6" | sed -e "s/$inet_str.\([0-9.]*\).*/\1/g" | awk '{print $1}' - | grep -v '^127\.[0-9]*\.[0-9]*\.[0-9]*$'
	fi
}

get_public_ipv4_address()
{
	local addrs="`get_ipv4_address_list`"
	local public_addrs="`echo "$addrs" | egrep -v '^(127|10|192\.168|172\.(1[6-9]|2[0-9]|3[0-1]))\.'`"
	if [ -n "$public_addrs" ]; then
		echo "$public_addrs" | head -1
	else
		echo "$addrs" | head -1
	fi
}

get_fqdn()
{
	hostname -f
}

get_relative_url()
{
    plesk login -relative-url
}

out()
{
	echo -e "\t$*" >&2
}

print_urls()
{
	local hostname="`get_fqdn`"
	local ipv4_addr="`get_public_ipv4_address`"
	local relative_url="`get_relative_url`"

	[ -z "$ipv4_addr" ] || out "  * https://$ipv4_addr:8443$relative_url"
	[ -z "$hostname"  ] || out "  * https://$hostname:8443$relative_url"
}

print_congratulations()
{
	local mode="$1"		# 'install' or 'upgrade'
	local process=
	[ "$mode" = "install" ] && process="installation" || process="upgrade"

	out
	out "                           Congratulations!"
	out
	out "The $process has been finished. Plesk is now running on your server."
	out
	out "To complete the configuration process, browse either of URLs:"
	print_urls
	out
	out "Use the username 'admin' to log in. To log in as 'admin', use the 'plesk login' command."
	out "You can also log in as 'root' using your 'root' password."
	out
	out "Use the 'plesk' command to manage the server. Run 'plesk help' for more info."
	out
	out "Use the following commands to start and stop the Plesk web interface:"
	out "'service psa start' and 'service psa stop' respectively."
	out
	if [ "$mode" = "install" ]; then
		out "If you would like to migrate your subscriptions from other hosting panel"
		out "or older Plesk version to this server, please check out our assistance"
		out "options: https://www.plesk.com/professional-services/"
		out
	fi
}

unset GREP_OPTIONS

print_congratulations "$1"
# Force showing text when used as AI post-examiner
exit 1
