#!/bin/bash

# shellcheck source=install/usr/bin/xivocc-installer-lib/xivo-configuration-helper.sh
source "/usr/bin/xivocc-installer-lib/xivo-configuration-helper.sh"

init_log

source_custom_env_file

# Regex IPv4 stricte (0.0.0.0 à 255.255.255.255)
regex_ip='^(([0-9]{1,2}|1[0-9]{2}|2([0-4][0-9]|5[0-5]))\.){3}([0-9]{1,2}|1[0-9]{2}|2([0-4][0-9]|5[0-5]))$'

if ! [[ "$XUC_HOST" =~ $regex_ip ]]; then
	XUC_HOST=$(whiptail --inputbox "Enter the XIVOCC IP address:" 8 65 3>&1 1>&2 2>&3)
	if ! [[ "$XUC_HOST" =~ $regex_ip ]]; then
		echo -e "\e[1;31mERROR: Variable XUC_HOST in custom.env must be a valid IP address before configuring the XiVO PBX. Exiting...\e[0m"
		exit 1
	fi
fi

xivocc_configures_xivo_post_wizard

echo "Installation complete. Check logs at $LOG_FILE if needed."
