#!/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="pulseaudio"
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

pulse_conf_file="/etc/pulse/daemon.conf"
pulse_conf_file_backup_nodebug="/etc/pulse/daemon.conf.bak-off-debug"

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

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

# 备份pulse_conf_file
backup_no_debug_pulse_conf_file() {
    if [[ ! -f "$pulse_conf_file_backup_nodebug" ]]; then
        cp "$pulse_conf_file" "$pulse_conf_file_backup_nodebug"
    fi
    sed -i '/^\s*log\-level\s*=/d' "$pulse_conf_file_backup_nodebug"
}

case "${debug}" in
  "on")
    if [[ $(grep -cE '^\s*log\-level\s*=\s*debug' "$pulse_conf_file") -gt 0 ]]; then
        echo "Debug logs are already enabled for ${pkg_name}."
    else
        backup_no_debug_pulse_conf_file
        sed -i '/log\-level\s*=/d' "$pulse_conf_file"
        # 添加到最后
        echo "log-level = debug" >> "$pulse_conf_file"
        echo "Debug logs are enabled for ${pkg_name}. "
    fi
    ;;
  "off")
    # log-level所在的行已经被注释了  
    #\s*: 表示匹配任意数量的空白字符（空格或制表符）。
    #;?: 表示可选的分号（;）。问号?表示前面的字符可以出现零次或一次。在daemon.conf文件中，配置项可能是以分号开头的注释行，也可能是不带分号的实际配置行
    # log\-level: 表示匹配字符串log-level，因为-是一个特殊字符，所以在前面加上反斜杠\进行转义。
    # \s*: 表示匹配任意数量的空白字符。
    # =: 表示匹配等号。
    # \s*: 表示匹配任意数量的空白字符。
    # debug: 表示匹配字符串debug。
    if [[ $(grep -cE '\s*;\s*log\-level\s*=' "$pulse_conf_file") -gt 0 ]]; then
        if [[ $(grep -cE '^\s*log\-level\s*=' "$pulse_conf_file") -eq 0 ]]; then
            echo "Debug logs are already disabled for ${pkg_name}."
        else
            sed -i '/^\s*log\-level\s*=/d' "$pulse_conf_file"
            echo "Debug logs are disabled for ${pkg_name}."
        fi
    elif [[ $(grep -cE '^\s*log\-level\s*=' "$pulse_conf_file") -eq 0 ]]; then
        echo "Debug logs are already disabled for ${pkg_name}."
    else
        # if [ -f "$backup_no_debug_pulse_conf_file" ]; then
        #     cp "$backup_no_debug_pulse_conf_file" "$pulse_conf_file"
        # fi
        sed -i '/^\s*log\-level\s*=/d' "$pulse_conf_file"
        echo "Debug logs are disabled for ${pkg_name}."
    fi
    ;;
  "debug" | "info" | "notice" | "warning" | "err" | "crit" | "alert" | "emerg")
    if [[ $(grep -cE '^\s*log\-level\s*=\s*'"${debug}"'\s*$' "$pulse_conf_file") -gt 0 ]]; then
        echo "Debug logs are already set to '${debug}' for ${pkg_name}."
    else
        # Backup
        backup_no_debug_pulse_conf_file
        sed -i '/^\s*log\-level\s*=/d' "$pulse_conf_file"
        # 添加到最后
        echo "log-level = "${debug}"" >> "$pulse_conf_file"
        echo "Debug logs are set to '${debug}' for ${pkg_name}."
    fi
    ;;
  *)
    echo "Not support ${debug} parameter: $@"
    exit 1;
    ;;
esac
