#! /bin/bash
# vim: shiftwidth=4 tabstop=4 expandtab

AUTODETECT_HYPS_CONF_FILE="$CONFIG_DIR/000-autodetected-hypervisors.conf"

if in_array "${COMMAND_ARGS[0]}" --help -h help; then
    echo "$COMMAND [--force]"
    if [ "${COMMAND_ARGS[1]}" == "details" ]; then
        echo "  Auto-detect hypervisors list from /etc/hosts file and update the"
        echo "  $AUTODETECT_HYPS_CONF_FILE configuration file."
        echo "    -F|--force                     Force updating file even if no change was detected"
        echo
        echo "  Note: The auto-detection use the following configured pattern:"
        echo "    '$AUTODETECT_HYPS_PATTERN'"
    fi
    return
fi

# Note: the -F/--force parameter is managed by common stuff.

echo -n "Auto-detecting hypervisors from /etc/hosts... "
mapfile -t AUTODETECTED_HYPS < <(
    grep -E "$AUTODETECT_HYPS_PATTERN" /etc/hosts | awk '{print $2}'
)
echo "done."

if [[ ${#AUTODETECTED_HYPS[@]} -eq 0 ]]; then
    echo "No hypervisor found in /etc/hosts using the following pattern:"
    echo "  '$AUTODETECT_HYPS_PATTERN'"
    exit 1
fi

echo "${#AUTODETECTED_HYPS[@]} auto-detected hypervisors: ${AUTODETECTED_HYPS[*]}"

# Make sure to reload your configuration file to compare detected list with its one.
# shellcheck disable=SC1090
[[ -e "$AUTODETECT_HYPS_CONF_FILE" ]] && source "$AUTODETECT_HYPS_CONF_FILE"
mapfile -t HYPS < <( tr ' ' '\n' <<< "$HYPS" | grep -v '^$' )

if [[ "${AUTODETECTED_HYPS[*]}" == "${HYPS[*]}" ]]; then
    echo "No change detected in previously auto-detected hypervisors list."
    [[ $FORCE -eq 1 ]] && echo "Force mode enabled, continue" || exit 0
fi

[[ $JUST_TRY -eq 1 ]] && log DEBUG "Do not really update $AUTODETECT_HYPS_CONF_FILE file" && exit 0

echo -n "Updating $AUTODETECT_HYPS_CONF_FILE file... "
cat << EOF > "$AUTODETECT_HYPS_CONF_FILE"
###############################################################################
#                                                                             #
#                       /!\ Auto-generated file /!\                           #
#                                                                             #
#  This file is maintained by the postinst script of the eehyp-tools debian   #
#  packages. To override hypervisors list, please create a file in the same   #
#  directory. Make sure it will be loaded after this one.                     #
#                                                                             #
#  You could redetect hypervisors list by running the following command:      #
#                                                                             #
#                         vm autodetect-hypervisors                           #
#                                                                             #
###############################################################################

# Last update: $( date "+%Y-%m-%d %H:%M:%S" )
# Auto-detection pattern used: '$AUTODETECT_HYPS_PATTERN'
# Note: You could adjust it in configuration if need.
HYPS="$( implode ' ' "${AUTODETECTED_HYPS[@]}" )"
EOF

echo "done."
