MAJOR=4
MINOR=61
DEVNAME=tty61
  1   ˆl–äY>”Šn-jq@Šãa¸”Å£Æ„m >÷_ŒuÐb&=LyYÿÃÅ ?÷     #!/usr/bin/bash


# Short Description :Deploy imunify360/ImunifyAV
# Description       :Installs imunify360/ImunifyAV repository
# Copyright         :Cloud Linux Zug GmbH
# License           :Cloud Linux Commercial License

# Do not edit/move/reformat this line except for actual version bump
# it is used by old versions of deploy scripts to check for update
version="2.155"

readonly package="imunify-antivirus"
readonly imunify360="imunify360-firewall"
readonly imunify_av="imunify-antivirus"


if [[ "$package" != "$imunify360" ]] && [[ "$package" != "$imunify_av" ]]; then
  exit 1
fi



if [[ "$package" = "$imunify_av" ]]; then
    PRODUCT="ImunifyAV"
    UI_PACKAGE="imunify-ui-antivirus"
    COMMAND="imunify-antivirus"
    STAND_ALONE_URL="https://docs.imunifyav.com/stand_alone_mode"
    LOG_FILE="/var/log/imav-deploy.log"
    LOCK="/var/lock/imav-deploy.lck"
    PREMIUM_REVISIUM_EXTENSION="/var/imunify360/premium_revisium_license.flag"
    # packages mentioned in the update command in the daily cron job
    readonly additional_packages_to_remove="ai-bolit\
    alt-php-hyperscan\
    imunify-release\
    imunify-common\
    imunify-notifier\
    imunify-core\
    imunify-realtime-av\
    imunify-realtime-av-imrt2\
    rustbolit\
    imunify-ui\
    imunify-wp-security\
    imunify360-venv\
    imunify-patchman"
    readonly additional_packages_to_remove_cl="alt-php-internal\
    app-version-detector"
    readonly additional_packages_to_remove_centos="minidaemon"
    readonly additional_packages_to_remove_debian=""
fi

readonly YUM_DISABLED_PHP_REPOS_OPTION="--disablerepo=imunify360-ea-php-hardened*"
OS_RELEASE_INFO=/etc/os-release
set -o pipefail
# fail if any error: will not
#set -e
# error for unbound variable: not for now
#set -eu

# $1 = Message prompt
# Returns ans=0 for yes, ans=1 for no
yesno() {
    local YES=0
    local NO=1
    local PENDING=2

    if [ $dry_run -eq 1 ]; then
        echo "Would be asked here if you wanted to"
        echo "$1 (y/n - y is assumed)"
        local ans=$YES
    elif [ "$assumeyes" = "true" ]; then
        local ans=$YES
    else
        local ans=$PENDING
    fi

    while [ $ans -eq $PENDING ]; do
        echo "Do you want to $1 (y/n) ?" ; read -r reply
        case "$reply" in
            Y*|y*) ans=$YES ;;
            N*|n*) ans=$NO ;;
            *) echo "Please answer y or n" ;;
        esac
    done

    return "$ans"
}

prepend_timestamp() {
    # Prepend current time to each line
    #
    # Usage: source-program | prepend_timestamp
    #
    # Note: it may fail if the input contains \0 bytes
    while IFS= read -r line
    do
        printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$line"
    done
}

log()
{
    # Run given command and append its duplicated stdout/stderr to
    # $LOG_FILE.
    #
    # Usage: log <command> [<args>...]
    #
    "$@" |& prepend_timestamp | tee -a "$LOG_FILE"
    return "${PIPESTATUS[0]}"
}

exit_with_error()
{
    log echo -e "$@"
    rm -rf "$lock"
    exit 1
}

print_debug_info()
{
    if [ "$DEBUG" == "true" ]; then
        echo "$@"
    fi
}

# Only for imunify360-firewall
set_low_resource_usage_mode_if_necessary()
{
  local _package=$1
  local _package_version=$2
  imunify360_low_mem_limit=2147483648
  imunify360_python38_datadir=$venv_path/share/imunify360
  # total usable memory in bytes
  mem_total=$(</proc/meminfo awk '$1 == "MemTotal:" { printf "%.0f", $2 * 1024 }')
  if (( mem_total < imunify360_low_mem_limit )); then
    # enable "Low Resource Usage" mode
    local low_mem_config="$imunify360_python38_datadir/20-low-mem-settings.config"
    if [ -f "$low_mem_config" ]; then
        log echo "Installing $low_mem_config from $_package $_package_version"
        ln -s "$low_mem_config" /etc/sysconfig/imunify360/imunify360.config.d/ && log echo "Done."
    fi
  fi
}

populate_os_release_vars()
{
    # shellcheck source=/etc/os-release
    [ -f "$OS_RELEASE_INFO" ] && source "$OS_RELEASE_INFO"
}


is_debian_like()
{
    populate_os_re