#!/bin/bash
### Copyright 1999-2026. WebPros International GmbH. All rights reserved.
#

#
# Plesk script
#



#default values

### Copyright 1999-2026. WebPros International 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.

	local lc="`get_default_locale`"
	export LANG="$lc" LC_MESSAGES="$lc" LC_ALL="$lc"
	export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
	umask 022

	PLESK_SCRIPT_COMMAND_LINE="$0 $*"
	[ -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
	}
	'
}

get_default_locale()
{
	# Note that CentOS 7 typically doesn't have C.UTF-8
	for lc in "C.UTF-8" "en_US.UTF-8" "C"; do
		if [ -z "`LC_ALL=$lc locale 2>&1 >/dev/null`" ]; then
			echo "$lc"
			return 0
		fi
	done
	echo "C"
}

### Copyright 1999-2026. WebPros International GmbH. All rights reserved.
# vim:ft=sh:

reexec_with_clean_env "$@"

HOSTNAME=$1

OLD_HOSTNAME="`cat /var/qmail/control/me`"
echo "$HOSTNAME" > /var/qmail/control/me

#
# Exclude last hostname from qmail/control/locals which is not a predefined
# record.
#
sed -i '
    /^localhost\(\.localdomain\)\?$/ {

        # Flush the content of the hold buffer and pass the current record.

        # Exchange content of the hold buffer and current record
        x

        # If pattern taken from hold buffer is not empty - print it.
        /./ p
        
        # Erase the pattern in such a way to not start next cycle.
        s/.*//g
        
        # Put the original content back from hold buffer to current pattern.
        x

        # Pass predefined localhost records to stdout.
        b
    }

    /^$/ {
        # Prune empty lines.
        d
        b
    }

    # Remember the current not empty line, assuming it is a hostname.
    # Previously remembered line now becomes current.
    x

    /./ {
        # Print the current line if it is not empty.
        p
    }

    # Do not output anything what was not explicitly printed before.
    d
' /var/qmail/control/locals

#
# Put the new hostname into locals, if it not coincides with domains having
# their own mail systems.
#
/opt/psa/admin/sbin/hostname-helper --check-name-unused "$HOSTNAME"

if [ $? -eq 0 ]; then
    echo "$HOSTNAME" >> /var/qmail/control/locals
fi

#
# Replace old hostname with new one in rcpthosts
#
if [ -n "$OLD_HOSTNAME" ]; then
    escaped_old_hostname=`echo $OLD_HOSTNAME | sed 's/[][\.*^$/]/\\\&/g'`
    sed -i 's/^'$escaped_old_hostname'$/'$HOSTNAME'/g' /var/qmail/control/rcpthosts
fi
