#!/bin/bash

. /usr/share/preupgrade/common.sh

#END GENERATED SECTION

check_pkg() {
  is_pkg_installed "$1" || return 1
  is_dist_native "$1" || return 1
  return 0
}

FILES=""
VERIFY=""
for pkg in tuned tuned-profiles-{sap,sap-hana,oracle}; do
    check_pkg "$pkg" || continue
    FILES="$FILES $(rpm -ql "$pkg" 2> /dev/null)"
    VERIFY="$VERIFY $(rpm -qV "$pkg" 2> /dev/null)"
done

TUNED_DIR="/etc/tune-profiles/"
if [[ -d "$TUNED_DIR" ]]; then
    TUNED_BACKUP_DIR="$VALUE_TMP_PREUPGRADE/backup/tuned"
    mkdir -p "$TUNED_BACKUP_DIR"

    RES=$RESULT_FAIL
    if [ -f /etc/tune-profiles/active-profile ]; then
        ACTIVE_PROFILE=$(cat /etc/tune-profiles/active-profile)
        case "$ACTIVE_PROFILE" in
        "default")
            ACTIVE_PROFILE="balanced"
            log_medium_risk "Your Tuned profile 'default' is no longer supported, switched to the replacement profile 'balanced'."
            ;;
        "throughput-performance" | "latency-performance" | "virtual-guest" | "virtual-host" | \
        "sap-netweaver" | "sap-hana" | "sap-hana-vmware" | "oracle")
            RES=$RESULT_PASS
            ;;
        "laptop-ac-powersave" | "laptop-battery-powersave" | "server-powersave")
            log_medium_risk "Your Tuned profile '$ACTIVE_PROFILE' is no longer supported, switched to the replacement profile 'powersave'."
            ACTIVE_PROFILE="powersave"
            ;;
        "enterprise-storage" | "spindown-disk")
            log_medium_risk "Your Tuned profile '$ACTIVE_PROFILE' is no longer supported, switched to the default Tuned profile."
            ACTIVE_PROFILE=""
            ;;
        *)
            log_medium_risk "You need to migrate your Tuned profile '$ACTIVE_PROFILE' by hand, switched to the default Tuned profile."
            ACTIVE_PROFILE=""
            ;;
        esac
        TUNED_CONF_DIR=$VALUE_TMP_PREUPGRADE/cleanconf/etc/tuned
        mkdir -p $TUNED_CONF_DIR
        echo $ACTIVE_PROFILE > $TUNED_CONF_DIR/active_profile
        if [ "$ACTIVE_PROFILE" ]; then
            echo "manual" > $TUNED_CONF_DIR/profile_mode
        else
            echo "auto" > $TUNED_CONF_DIR/profile_mode
        fi
    else
        log_medium_risk "Unable to detect your active Tuned profile, not migrating it."
    fi

    BACKUP_LIST=""
    for f in $(ls -d /etc/tune-profiles/*/); do
        if ! echo "$FILES" | grep -q "$f" || echo "$VERIFY" | grep -q "$f"; then
            BACKUP_LIST="$BACKUP_LIST ${f%/}"
        fi
    done

    if [ "$RES" != "$RESULT_PASS" ]; then
        # copy the post-upgrade script (in case of $RESULT_PASS, in this phase
        # of the check, there is no reason for the postupgrade seatbelt here,
        # in such case do not do anything)
        cp -a "zz_tuned_post.sh" "$POSTUPGRADE_DIR"
        chmod +x "$POSTUPGRADE_DIR/zz_tuned_post.sh"
    fi

    sed -i "s|TUNED_BACKUP_DIR|$TUNED_BACKUP_DIR|g" solution.txt
    if [ "$BACKUP_LIST" ]; then
        cp -a -t $TUNED_BACKUP_DIR $BACKUP_LIST
        log_medium_risk "Customized Tuned profiles were detected in the $TUNED_DIR directory. See the tuned-adm and tuned-profiles man pages for more information."
        exit $RESULT_FAIL
    fi
    exit $RES
fi

log_debug "The $TUNED_DIR dir doesn't exist. Nothing to do."
exit $RESULT_NOT_APPLICABLE
