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

service_file="/usr/lib/systemd/system/wpa_supplicant.service"
service_file_backup_nodebug="/usr/lib/systemd/system/wpa_supplicant.service.bak-off-debug"
wpa_supplicant="/sbin/wpa_supplicant"

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

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

# 备份service文件
backup_no_debug_service_file() {
    # 备份service_file到service_file_backup_nodebug
    if [[ ! -f "$service_file_backup_nodebug" ]]; then
        cp "$service_file" "$service_file_backup_nodebug"
    fi
    # 需要保证service_file_backup_nodebug中没有debug参数
    if [[ $(grep -cE '\s-d{1,3}\s' "$service_file_backup_nodebug") -gt 0 ]]; then
        # Remove existing debug log options
        sed -i 's/\s-d\{1,3\}\s/ /' "$service_file_backup_nodebug"
    fi
    # 为service_file_backup_nodebug中的/sbin/wpa_supplicant字段加上-s参数
    if [[ $(grep -cE '\s-s\s' "$service_file_backup_nodebug") -eq 0 ]]; then
        sed -i "s#$wpa_supplicant#$wpa_supplicant -s#" "$service_file_backup_nodebug"
    fi

}

# 修改service文件
modify_service_file() {
    # Check if "-d", "-dd", "-ddd" already exist
    if [[ $(grep -cE '\s-(d{1,3}|K)\s' "$service_file") -gt 0 ]]; then
        # Remove existing debug log options
        #sed -i 's/\s-d\{1,3\}\s/ /' "$service_file"
	sed -i -E 's/\s?-(d{1,3}|K)\s?/ /g' "$service_file"
    fi
  
    # Check if "-s" option exists and remove it
    if [[ $(grep -cE '\s-s\s' "$service_file") -gt 0 ]]; then
        sed -i 's/\s-s\s/ /' "$service_file"
    fi

    # Add the specified log level argument
    sed -i "s#$wpa_supplicant#$wpa_supplicant $@#" "$service_file"
}


case "${debug}" in
  "on" | "debug")
    if [[ $(grep -cE "\s-ddd\s" "$service_file") -gt 0 ]]; then
        echo "Debug logs are already enabled for ${pkg_name}."
    else
        # Backup the service file
        backup_no_debug_service_file

        # Modify the service file to enable debug logs
        modify_service_file "-ddd -K"
	sed -i -E 's/[[:space:]]+/ /g' "$service_file"
        echo "Debug logs are enabled for ${pkg_name}."
    fi
    ;;
  "off" | "warning")
    # Check for existing debug log options
    if [[ $(grep -cE '\s-d{1,3}\s' "$service_file") -eq 0 ]]; then
        echo "Debug logs are already disabled for ${pkg_name}."
    else
        # if [[ -f "$service_file_backup_nodebug" ]]; then
        #     # Restore the original service file
        #     cp "$service_file_backup_nodebug" "$service_file"
        # fi
        # Add "-s" option if it does not exist
        if [[ $(grep -cE '\s-s\s' "$service_file") -eq 0 ]]; then
            sed -i "s#$wpa_supplicant#$wpa_supplicant -s#" "$service_file"
        fi
        # Remove existing debug log options
        #sed -i 's/\s-d\{1,3\}\s/ /' "$service_file"
	sed -i -E 's/\s?-(d{1,3}|K)\s?/ /g' "$service_file"
	sed -i -E 's/[[:space:]]+/ /g' "$service_file"
        echo "Debug logs are disabled for ${pkg_name}."
    fi
    ;;
  "-d" | "-dd" | "-ddd")
    # Check if the specified log level already exists
    if [[ $(grep -cE "\s$1\s" "$service_file") -eq 0 ]]; then
        # Backup the service file
        backup_no_debug_service_file

        # Remove existing debug log options
        sed -i 's/\s-d\{1,3\}\s/ /' "$service_file"

        # Add the specified log level argument
        modify_service_file "$1"
        echo "Debug logs are set to $1 for ${pkg_name}."
    else
        echo "Debug logs are already set to $1 for ${pkg_name}."
    fi
    ;;
  *)
    echo "Not support ${debug} parameter: $@"
    exit 1;
    ;;
esac
