#!/bin/bash
# info: change hostname
# options: HOSTNAME
#
# example: v-change-sys-hostname mydomain.tld
#
# This function for changing system hostname.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
domain=$1

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source $TULIO/func/main.sh
# shellcheck source=/usr/local/tulio/func/hosts.sh
source $TULIO/func/hosts.sh
# shellcheck source=/usr/local/tulio/func/webmail.sh
source $TULIO/func/webmail.sh
# load config file
source_conf "$TULIO/conf/tulio.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '1' "$#" 'HOSTNAME'
is_format_valid 'domain'

# Perform verification if read-only mode is enabled
check_tulio_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

current_hostname="$(hostname)"
if [[ "$current_hostname" == "$domain" ]]; then
	echo "Current hostname \"$current_hostname\" is the same as the new one you want to use"
	echo "I'm not going to change it"
	exit
fi

# Update /etc/hosts first: it is the one step that can refuse to proceed, and
# leaving the system with a new hostname it cannot resolve is worse than not
# changing the hostname at all.
if ! update_etc_hosts "$current_hostname" "$domain"; then
	echo "Error: /etc/hosts could not be updated safely, it was left unchanged"
	echo "The hostname has not been changed"
	log_event "$E_UPDATE" "$ARGUMENTS"
	exit "$E_UPDATE"
fi

hostname "$domain"

# Debian/Ubuntu
hostnamectl set-hostname "$domain"
echo "$domain" > /etc/hostname

# Update webmail's password plugin configuration
rc_password_config='/etc/roundcube/plugins/password/config.inc.php'
if [ -f "$rc_password_config" ]; then
	if ! set_roundcube_option "$rc_password_config" 'password_tulio_host' "$domain"; then
		echo "Error: unable to update $rc_password_config"
		log_event "$E_UPDATE" "$ARGUMENTS"
		exit "$E_UPDATE"
	fi
fi
if [ -d /etc/rainloop/ ]; then
	sed -i "/tulio_host/c\tulio_host = \"$domain\"" /etc/rainloop/data/_data_/_default_/configs/plugin-tulio-change-password.ini
fi
if [ -d /var/lib/snappymail/ ]; then
	sed -i "/\"tulio_host\":/c\\        \"tulio_host\": \"$domain\"," /var/lib/snappymail/data/_data_/_default_/configs/plugin-change-password.json
fi

#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

# Logging
$BIN/v-log-action "system" "Warning" "System" "System hostname changed (Host: $domain)."
log_event "$OK" "$ARGUMENTS"

exit
