#!/bin/bash
# © Copyright EnterpriseDB UK Limited 2015-2023 - All rights reserved.
#
# This is an ansible module that takes no arguments and returns only
# some distribution-specific facts.

source "$1"

declare -A families pkg_mgrs
families=([Debian]=Debian [RedHat]=RedHat [Ubuntu]=Debian [SLES]=SUSE)
pkg_mgrs=([Debian]=apt [RedHat]=dnf [SUSE]=zypper)

. /etc/os-release

version=$VERSION_ID
major_version=${VERSION_ID%.*}

case "${ID}:${VERSION_ID%.*}" in
    rhel:7|centos:7|ol:7)
        distribution=RedHat
        release=$VERSION_ID
        python=python2
        pkg_mgr=yum
        ;;
    debian:8)
        distribution=Debian
        release=jessie
        python=python2.7
        python_pkg_prefix=python
        ;;
    debian:9|debian:10|debian:11)
        distribution=Debian
        release=${VERSION_CODENAME:-stretch}
        python=${preferred_python_version:-python3}
        if [[ $python == "python2" ]]; then
            python_pkg_prefix=python
        fi
        ;;
    ubuntu:16|ubuntu:18)
        distribution=Ubuntu
        release=$VERSION_CODENAME
        python=${preferred_python_version:-python3}
        if [[ $python == "python2" ]]; then
            python_pkg_prefix=python
            python=python2.7
        fi
        ;;
    ubuntu:20|ubuntu:22)
        distribution=Ubuntu
        release=$VERSION_CODENAME
        python=python3
        ;;
    rhel:8|centos:8|ol:8|rocky:8|almalinux:8)
        distribution=RedHat
        release=$VERSION_ID
        python=python3
        ;;
    rhel:9|centos:9|ol:9|rocky:9|almalinux:9)
        distribution=RedHat
        release=$VERSION_ID
        python=python3
        ;;
    sles:15)
        distribution=SLES
        release=$VERSION_ID
        python=python3
        ;;
    *)
        cat <<RESULT
        {"failed": true, "msg": "Unsupported distribution: ${ID}:${VERSION_ID}"}
RESULT
        exit 0
        ;;
esac

os_family=${os_family:-${families[$distribution]}}
python_pkg_prefix=${python_pkg_prefix:-$python}
pkg_mgr=${pkg_mgr:-${pkg_mgrs[$os_family]}}
service_mgr=${service_mgr:-systemd}

memtotal=$(awk '/^MemTotal: / {print int($2/1024)}' /proc/meminfo)

if command -v locale &>/dev/null; then
    eval "$(locale)"
    system_locale=${LC_ALL:-$LANG}
fi

etc_tpa_exists=false
if [[ -d /etc/tpa ]]; then etc_tpa_exists=true; fi

# Detects and returns the type of container we're executing in, or an
# empty string if we are not in a container or can't tell.

container_type=$(systemd-detect-virt -c 2>/dev/null)
if [[ $container_type == "none" ]]; then
    container_type=""
fi

grep -Ev '": ""' <<RESULT
{
    "ansible_facts": {
        "ansible_os_family": "$os_family",
        "ansible_distribution": "$distribution",
        "ansible_distribution_id": "$ID",
        "ansible_distribution_release": "$release",
        "ansible_distribution_version": "$version",
        "ansible_distribution_major_version": "$major_version",
        "ansible_service_mgr": "$service_mgr",
        "ansible_pkg_mgr": "$pkg_mgr",
        "python_pkg_prefix": "$python_pkg_prefix",
        "python": "$python",
        "system_locale": "${system_locale:-}",
        "etc_tpa_exists": $etc_tpa_exists,
        "target_container_type": "$container_type",
        "ansible_memtotal_mb": $memtotal
    }
}
RESULT
