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

dde_env_file="/etc/profile"
dde_env_file_backup_nodebug="/etc/profile.bak-off-debug"
# /etc/X11/Xsession.d/00deepin-dde-env最近有做修改，默认dde的log改成warning，但是目前还未提测，放在这有备无患
dde_env_file2="/etc/X11/Xsession.d/00deepin-dde-env"
dde_env_file2_backup_nodebug="/etc/X11/Xsession.d/00deepin-dde-env"

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

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

# 备份dde_env_file
backup_no_debug_dde_env_file() {
    if [[ ! -f "$dde_env_file_backup_nodebug" ]]; then
        cp "$dde_env_file" "$dde_env_file_backup_nodebug"
    fi

    if [[ ! -f "$dde_env_file2_backup_nodebug" ]]; then
        cp "$dde_env_file2" "$dde_env_file2_backup_nodebug"
    fi
    # 删除含有DDE_DEBUG_LEVEL的行
    sed -i "/DDE_DEBUG_LEVEL=/d" "$dde_env_file_backup_nodebug"
}

case "${debug}" in
  "on")
    if [[ $(grep -cE '^\s*export\s*DDE_DEBUG_LEVEL=debug' "$dde_env_file") -gt 0 ]]; then
        if [[ $(grep -cE 'DDE_DEBUG_LEVEL=' "$dde_env_file2") -gt 0 ]]; then
            sed -i "s/DDE_DEBUG_LEVEL=.*/DDE_DEBUG_LEVEL=debug/" "$dde_env_file2"
        fi
        echo "Debug logs are already enabled for ${pkg_name}."
    else
        # 删除含有DDE_DEBUG_LEVEL的行
        sed -i "/^\s*export\s*DDE_DEBUG_LEVEL=/d" "$dde_env_file"
        backup_no_debug_dde_env_file
        echo "export DDE_DEBUG_LEVEL=debug" >> "$dde_env_file"
        if [[ $(grep -cE 'DDE_DEBUG_LEVEL=' "$dde_env_file2") -gt 0 ]]; then
            sed -i "s/DDE_DEBUG_LEVEL=.*/DDE_DEBUG_LEVEL=debug/" "$dde_env_file2"
        fi
        echo "Debug logs are enabled for ${pkg_name}."
    fi
    ;;
  "off")
    if [[ $(grep -cE '^\s*export\s*DDE_DEBUG_LEVEL=warning' "$dde_env_file") -gt 0 ]]; then
        if [[ $(grep -cE 'DDE_DEBUG_LEVEL=' "$dde_env_file2") -gt 0 ]]; then
            sed -i "s/DDE_DEBUG_LEVEL=.*/DDE_DEBUG_LEVEL=warning/" "$dde_env_file2"
        fi
        echo "Debug logs are already disabled for ${pkg_name}."
    else
        # if [ -f "$dde_env_file_backup_nodebug" ]; then
        #     cp "$dde_env_file_backup_nodebug" "$dde_env_file"
        # fi
        # 删除含有DDE_DEBUG_LEVEL的行
        sed -i "/^\s*export\s*DDE_DEBUG_LEVEL=/d" "$dde_env_file"
        echo "export DDE_DEBUG_LEVEL=warning" >> "$dde_env_file"
        if [[ $(grep -cE 'DDE_DEBUG_LEVEL=' "$dde_env_file2") -gt 0 ]]; then
            sed -i "s/DDE_DEBUG_LEVEL=.*/DDE_DEBUG_LEVEL=warning/" "$dde_env_file2"
        fi
        echo "Debug logs are disabled for ${pkg_name}."
    fi
    ;;
  "debug" | "info" | "warning" | "error" | "fatal" | "disable")
    if [[ $(grep -cE '^\s*export\s*DDE_DEBUG_LEVEL='"${debug}"'' "$dde_env_file") -gt 0 ]]; then
        if [[ $(grep -cE 'DDE_DEBUG_LEVEL=' "$dde_env_file2") -gt 0 ]]; then
            sed -i "s/DDE_DEBUG_LEVEL=.*/DDE_DEBUG_LEVEL="${debug}"/" "$dde_env_file2"
        fi
        echo "Debug logs are already set to '"${debug}"' for ${pkg_name}."
    else
        backup_no_debug_dde_env_file
        # 删除含有DDE_DEBUG_LEVEL的行
        sed -i "/^\s*export\s*DDE_DEBUG_LEVEL=/d" "$dde_env_file"
        echo "export DDE_DEBUG_LEVEL="${debug}"" >> "$dde_env_file"
        if [[ $(grep -cE 'DDE_DEBUG_LEVEL=' "$dde_env_file2") -gt 0 ]]; then
            sed -i "s/DDE_DEBUG_LEVEL=.*/DDE_DEBUG_LEVEL="${debug}"/" "$dde_env_file2"
        fi
        echo "${pkg_name} debug logs are configured to '${debug}'"
    fi    
    ;;
   *)
    echo "Not support ${debug} parameter: $@"
    exit 1;
    ;;
esac
