#!/bin/bash
# A script to configure the module's debug log level.
# 
# Note: Please do not modify this script directly, 
# as modifying this script will invalidate this script.
set -e

pkg_name="network-manager"
debug="no"

# 获取当前用户的UID
current_uid=$(id -u)

# 判断是否是Root权限
if [ "$current_uid" -ne 0 ]; then
  echo "You need to have root privileges to run this script."
  exit 1
fi

networkmanager_conf_file="/etc/NetworkManager/NetworkManager.conf"
networkmanager_conf_file_backup_nodebug="/etc/NetworkManager/NetworkManager.conf.bak-off-debug"

if [ ! -f  "$networkmanager_conf_file" ]; then
    echo "ERROR: Cannot find ${networkmanager_conf_file}"
    exit 1
fi

for x in "$@"; do
        case $x in
        debug=*)
                debug=${x#debug=}
                ;;
        esac
done


# 备份networkmanager_conf_file
backup_no_debug_networkmanager_conf_file() {
    if [[ ! -f "$networkmanager_conf_file_backup_nodebug" ]]; then
        cp "$networkmanager_conf_file" "$networkmanager_conf_file_backup_nodebug"
    fi
    sed -i "/level=[^ ]*/d" "$networkmanager_conf_file_backup_nodebug"
}

case "${debug}" in
  "on" | "debug")
    if [[ $(grep -cE 'level=TRACE' "$networkmanager_conf_file") -gt 0 ]]; then
        echo "Debug logs are already enabled for ${pkg_name}."
    else
        backup_no_debug_networkmanager_conf_file
	#如果旧文件已经有level=字段，则删除它
	sed -i "/level=[^ ]*/d" "$networkmanager_conf_file"
        if [[ $(grep -cE '\[logging\]' "$networkmanager_conf_file") -eq 0 ]]; then
            # echo "" >> "$networkmanager_conf_file"
            echo "[logging]" >> "$networkmanager_conf_file"
	    echo "level=TRACE" >> "$networkmanager_conf_file"
    	else
	    sed -i '/^\[logging\]/a\level=TRACE' "$networkmanager_conf_file"
        fi
        echo "Debug logs are enabled for ${pkg_name}."
    fi
    ;;
  "off" | "warning")
    if [[ $(grep -cE 'level=[^ ]*' "$networkmanager_conf_file") -eq 0 ]]; then
        echo "Debug logs are already disabled for ${pkg_name}."
    else
        # if [ -f "$backup_no_debug_networkmanager_conf_file" ]; then
        #     cp "$backup_no_debug_networkmanager_conf_file" "$networkmanager_conf_file"
        # fi
        sed -i "/\[logging\]/d" "$networkmanager_conf_file"
        sed -i "/level=[^ ]*/d" "$networkmanager_conf_file"
        echo "Debug logs are disabled for ${pkg_name}."
    fi
    ;;
  "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERR" | "OFF")
    if [[ $(grep -cE 'level='"$1"'' "$networkmanager_conf_file") -gt 0 ]]; then
        echo "Debug logs are already set to '$1' for ${pkg_name}."
    else
        backup_no_debug_networkmanager_conf_file
	#如果旧文件已经有level=字段，则删除它
        sed -i "/level=[^ ]*/d" "$networkmanager_conf_file"
        if [[ $(grep -cE '\[logging\]' "$networkmanager_conf_file") -eq 0 ]]; then
            # echo "" >> "$networkmanager_conf_file"
            echo "[logging]" >> "$networkmanager_conf_file"
	    echo "level=$1" >> "$networkmanager_conf_file"
	else
	    sed -i "/^\[logging\]/a\level=$1" "$networkmanager_conf_file"
        fi
        echo "Debug logs are set to '$1' for ${pkg_name}."
    fi
    ;;
  *)
    echo "Not support ${debug} parameter: $@"
    exit 1;
    ;;
esac
