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

systemd_conf_file="/etc/systemd/system.conf"
systemd_conf_file_backup_nodebug="/etc/systemd/system.conf.bak-off-debug"

user_conf_file="/etc/systemd/user.conf"

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

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

# 备份systemd_conf_file
backup_no_debug_systemd_conf_file() {
    if [[ ! -f "$systemd_conf_file_backup_nodebug" ]]; then
        cp "$systemd_conf_file" "$systemd_conf_file_backup_nodebug"
    fi
    # 删除以LogLevel=开头的行
    sed -i '/^\s*LogLevel\s*=/d' "$systemd_conf_file_backup_nodebug"
}

# Process the input argument
case "${debug}" in
  "on")
    argument="debug"
    if [[ $(grep -cE '^\s*LogLevel\s*='"$argument"'' "$systemd_conf_file") -gt 0 ]]; then
        echo "Debug logs of system are already enabled for ${pkg_name}."
    else
        backup_no_debug_systemd_conf_file
        # 删除以LogLevel=开头的行
        sed -i '/^\s*LogLevel\s*=/d' "$systemd_conf_file"
        # 添加到最后
        echo "LogLevel="$argument"" >> "$systemd_conf_file"
        echo "Debug logs of system are enabled for ${pkg_name}. "
    fi

    if [[ $(grep -cE '^\s*LogLevel\s*='"$argument"'' "$user_conf_file") -gt 0 ]]; then
        echo "Debug logs of user are already enabled for ${pkg_name}."
    else
        # 删除以LogLevel=开头的行
        sed -i '/^\s*LogLevel\s*=/d' "$user_conf_file"
        # 添加到最后
        echo "LogLevel="$argument"" >> "$user_conf_file"
        echo "Debug logs of user are enabled for ${pkg_name}. "
    fi
    ;;
  "off")
    # Check for existing debug log options
        # if [[ -f "$systemd_conf_file_backup_nodebug" ]]; then
        #     # Restore the original service file
        #     cp "$systemd_conf_file_backup_nodebug" "$systemd_conf_file"
        # fi
    if [[ $(grep -cE '\s*#\s*LogLevel\s*=' "$systemd_conf_file") -gt 0 ]]; then
        if [[ $(grep -cE '^\s*LogLevel\s*=' "$systemd_conf_file") -eq 0 ]]; then
            echo "Debug logs of system are already disabled for ${pkg_name}."
        else
            sed -i '/^\s*LogLevel\s*=/d' "$systemd_conf_file"
            echo "Debug logs of system are disabled for ${pkg_name}."
        fi
    elif [[ $(grep -cE '^\s*LogLevel\s*=' "$systemd_conf_file") -eq 0 ]]; then
        echo "Debug logs of system are already disabled for ${pkg_name}."
    else
        # if [ -f "$backup_no_debug_systemd_conf_file" ]; then
        #     cp "$backup_no_debug_systemd_conf_file" "$systemd_conf_file"
        # fi
        sed -i '/^\s*LogLevel\s*=/d' "$systemd_conf_file"
        echo "Debug logs of system are disabled for ${pkg_name}."
    fi

    if [[ $(grep -cE '\s*#\s*LogLevel\s*=' "$user_conf_file") -gt 0 ]]; then
        if [[ $(grep -cE '^\s*LogLevel\s*=' "$user_conf_file") -eq 0 ]]; then
            echo "Debug logs of user are already disabled for ${pkg_name}."
        else
            sed -i '/^\s*LogLevel\s*=/d' "$user_conf_file"
            echo "Debug logs of user are disabled for ${pkg_name}."
        fi
    elif [[ $(grep -cE '^\s*LogLevel\s*=' "$user_conf_file") -eq 0 ]]; then
        echo "Debug logs of user are already disabled for ${pkg_name}."
    else
        sed -i '/^\s*LogLevel\s*=/d' "$user_conf_file"
        echo "Debug logs of user are disabled for ${pkg_name}."
    fi
    ;;
  "debug" | "info" | "notice" | "warning" | "err" | "crit" | "alert" | "emerg")
    if [[ $(grep -cE '^\s*LogLevel\s*=\s*'"${debug}"'\s*$' "$systemd_conf_file") -gt 0 ]]; then
        echo "Debug logs of system are already set to '${debug}' for ${pkg_name}."
    else
        # Backup
        backup_no_debug_systemd_conf_file
        sed -i '/^\s*LogLevel\s*=/d' "$systemd_conf_file"
        # 添加到最后
        echo "LogLevel="${debug}"" >> "$systemd_conf_file"
        echo "Debug logs of system are set to '${debug}' for ${pkg_name}."
    fi

    if [[ $(grep -cE '^\s*LogLevel\s*=\s*'"${debug}"'\s*$' "$user_conf_file") -gt 0 ]]; then
        echo "Debug logs of user are already set to '${debug}' for ${pkg_name}."
    else
        sed -i '/^\s*LogLevel\s*=/d' "$user_conf_file"
        # 添加到最后
        echo "LogLevel="${debug}"" >> "$user_conf_file"
        echo "Debug logs of user are set to '${debug}' for ${pkg_name}."
    fi
    ;;
  *)
    echo "Not support ${debug} parameter: $@"
    exit 1;
    ;;
esac
