#!/bin/bash

set -e

# TODO: update iwiki url
# Print error message and exit
msg_err() {
    echo "Error: $1" >&2
    echo "Please refer to https://iwiki.woa.com/p/4015781670" >&2
    exit 1
}

check_tagent() {
    # check tagent
    if [ ! -f /var/lib/tagent/tagent.lock ]; then
        return 1
    fi
    tagent_pid=$(cat /var/lib/tagent/tagent.lock)
    if ! grep -q "tagent" /proc/${tagent_pid}/cmdline; then
        return 1
    fi

    # check tmanager service
    if [ ! -f /usr/local/tmanager/tmanager-service.pid ]; then
        return 1
    fi
    tmanager_pid=$(tr -d '\0' </usr/local/tmanager/tmanager-service.pid)
    if ! grep -q "tmanager-service" /proc/${tmanager_pid}/cmdline; then
        return 1
    fi
}

if ! check_tagent; then
    echo "Warning: tagent not supported. Modify default pusher disabled and record latency without pushing."
    if [ -f /etc/nbs_agent.conf ]; then
        sed -i '/^pusher:/,/^[^ ]/ s/^  push: true$/  push: false/' /etc/nbs_agent.conf
        echo "/etc/nbs_agent.conf default pusher disabled."
    fi
    if [ -f /opt/nbs_agent/misc/nbs_agent.conf ]; then
        sed -i '/^pusher:/,/^[^ ]/ s/^  push: true$/  push: false/' /opt/nbs_agent/misc/nbs_agent.conf
        echo "/opt/nbs_agent/misc/nbs_agent.conf default pusher disabled."
    fi
else
    echo "Info: tagent supported. Keep default pusher as before."
fi

# Path to the nbs_uninstall binary
NBS_UNINSTALL_BIN="/usr/bin/nbs_uninstall"
# Path to the nbs_update binary
NBS_UPDATE_BIN="/usr/bin/nbs_update"
# Path to the nbsctl binary
NBSCTL_BIN="/usr/bin/nbsctl"

# Check for root privileges
if [[ $EUID -ne 0 ]]; then
    msg_err "Please run this script as root."
fi

if [[ -x "$NBS_UNINSTALL_BIN" ]]; then
    # Make sure to uninstall the old version before installing the new one
    "$NBS_UNINSTALL_BIN"
else
    msg_err "$NBS_UNINSTALL_BIN not found or not executable."
fi

if [[ ! -x "$NBSCTL_BIN" ]]; then
    msg_err "$NBSCTL_BIN not found or not executable."
fi

if [[ -x "$NBS_UPDATE_BIN" ]]; then
    $NBS_UPDATE_BIN
else
    msg_err "$NBS_UPDATE_BIN not found or not executable."
fi

# check_tagent_with_exit() {
#     # check tagent
#     if [ ! -f /var/lib/tagent/tagent.lock ]; then
#         msg_err "Please check if tagent has been installed."
#     fi
#     tagent_pid=$(cat /var/lib/tagent/tagent.lock)
#     if ! grep -q "tagentV" /proc/${tagent_pid}/cmdline; then
#         msg_err "Please check if tagent is running."
#     fi

#     # check tmanager service
#     if [ ! -f /usr/local/tmanager/tmanager-service.pid ]; then
#         msg_err "Please check tagent version, lacking tmanager service."
#     fi
#     tmanager_pid=$(tr -d '\0' </usr/local/tmanager/tmanager-service.pid)
#     if ! grep -q "tmanager-service" /proc/${tmanager_pid}/cmdline; then
#         msg_err "Please check if tmanager service is running."
#     fi

#     # check tdsp taskchannel
#     if [ ! -f /usr/local/tmanager/plugins/tdsp-taskchannel.pid ]; then
#         msg_err "Please check tagent version, lacking tdsp taskchannel."
#     fi
#     tdsp_pid=$(tr -d '\0' </usr/local/tmanager/plugins/tdsp-taskchannel.pid)
#     if ! grep -q "tdsp-taskchannel" /proc/${tdsp_pid}/cmdline; then
#         msg_err "Please check if tdsp taskchannel is running."
#     fi
# }
