#!/bin/sh

REPO_NAME="CloudModule"

REPO_FILE="/etc/yum.repos.d/cloud-module.repo"

usage() {
    cat <<EOU >&2

Usage:
	$0 --help 	Print this help

	$0 [options]
	--noinstall                 Not install the package. Just add or update repository.
	--auto                      Install the package. Install the latest version for the currently installed core manager.

	Example:
	$0 --auto                           Install latest available version cloudmodule for current installed Billmanager.
	$0 --auto --noinstall               Configure only the repository, without installing the cloudmodule package.
EOU
}

#####

set -e

if [ "$#" -eq 0 ]; then
    usage
    exit 0
fi

get_available_coremanager_version() {
    version=$(dnf --showduplicates list coremanager | awk '/coremanager/ {print $2}' | cut -d'-' -f1 | sort -V | tail -n1)
    echo "$version"
}

while [ "$#" -gt 0 ]; do
    case "${1}" in
    -h | --help)
        usage
        exit 0
        ;;
    --noinstall)
        noinstall="true"
        shift 1
        ;;
    --auto)
        COREVER=$(get_available_coremanager_version)
        shift 1
        ;;
    *)
        echo "Error: unknown options '$1'"
        usage
        exit 1
        ;;
    esac
done

check_os_version() {
    printf "# Check version OS...\n"
    if grep -q "AlmaLinux" /etc/os-release && grep -q "VERSION_ID=\"9" /etc/os-release; then
        echo "- A supporting OS is used"
    else
        echo ""
        echo "! This OS is not supported for installing the module" && exit 1
    fi
}

check_core_manager_version() {
    echo "# Check version BillManager and CoreManager..."

    list="5.440 5.441 5.442 5.443 5.444 5.445 5.446 5.447 "
    found=0
    for item in $list; do
        case "$COREVER" in
        "$item"*)
            found=1
            break
            ;;
        esac
    done

    if [ $found -eq 0 ]; then
        echo "! The version $COREVER of core manager that bill manager works with is not supported for installation"
        echo "! Available versions - $list"
        exit 1
    fi
    echo "- CoreManager version in use $COREVER"
}

check_install_latest_available_version_core_manager() {
    latest_version=$(repoquery --show-duplicates --qf '%{version}' coremanager | sort -V | tail -n 1)

    installed_version=$(rpm -q --qf '%{VERSION}\n' coremanager 2>/dev/null)

    if [ -z "$installed_version" ]; then
        echo "!Coremanager is not install."
        exit 1
    elif [ "$installed_version" = "$latest_version" ]; then
        echo "- Installed latest version coremanager ($installed_version)."
        return
    else
        echo "!Installed version coremanager: $installed_version. Available latest version coremanager: $latest_version. Please update coremanager "
        exit 1
    fi
}

check_root_permission() {
    if [ "$(id -u)" -ne 0 ]; then
        echo " ! This script must be run with administrator rights (root)." && exit 1
    fi
}

check_run_install() {
    if [ "$noinstall" = "true" ]; then
        return
    fi
    echo "! Billmanager will be restarted during installation"
    printf "? Do you agree to continue the operation? (Y/N): "
    read -r response

    if [ "$response" = "Y" ] || [ "$response" = "y" ]; then
        echo ""
    else
        echo "# The operation was canceled by the user. Exit..."
        exit 0
    fi

}

update_yum_repo() {
    echo "# Updating the yum repository..."
    echo "- Update file $REPO_FILE..."
    cat <<EOL >$REPO_FILE
[$REPO_NAME]
name="AlmaLinux \$releasever - \$basearch - CloudModule"
baseurl=https://repo.zomro.com/cloudmodule/rpm/almalinux/\$releasever/\$basearch/$COREVER
enabled=1
gpgcheck=0
metadata_expire=0
EOL

    echo "# Updating the yum repository successfully!"
}
install_cloud_module() {
    if [ "$noinstall" = "true" ]; then
        return
    fi
    echo "# Installing CloudModule..."
    yum makecache
    yum install -y cloudmodule || yum update cloudmodule
    echo "# Installation completed successfully!"
}

echo "- Check all parameters..."
echo
check_os_version
#check_install_latest_available_version_core_manager
check_core_manager_version
check_root_permission
check_run_install
update_yum_repo
install_cloud_module
