#!/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

readonly dbgconf="/usr/share/deepin-debug-config/deepin-debug-config.d/dbg.conf"
DEBUG_PACKAGES=()

is_root() {
  local current_uid
  current_uid=$(id -u)

  if [ "$current_uid" -ne 0 ]; then
    echo "You need to have root privileges to run this script."
    return 1
  fi
  return 0
}

can_exec() {
  if ! is_root; then
    return 1
  fi

  if [ $# -ne 1 ]; then
    echo "Invalid argument"
    return 1
  fi
}



get_debug_package_list() {
    local package_name
    local package_version
    local pkg_array=()
    local debug_packages=()

    package_name="$1"
    package_version=$(dpkg -s "${package_name}" 2>/dev/null | grep '^Version:' | awk '{print $2}')
    if [ -z "$package_version" ]; then
        echo "Cannot find package_version for ${package_name}"
        return 1
    fi

    readarray -t pkg_array < <(dpkg -l | grep '^ii' | grep -w "${package_version}" | awk '{print $2}' | cut -d ':' -f 1 | grep -v dbgsym)

    echo "Number of packages: ${#pkg_array[@]}"

    for package in "${pkg_array[@]}"; do
        debug_package="${package}-dbgsym"
        if ! dpkg -l "${debug_package}" &>/dev/null; then
            debug_packages+=("${debug_package}=${package_version}")
        else
            echo "${debug_package} already installed"
        fi
    done

    DEBUG_PACKAGES=("${debug_packages[@]}")
    return 0
}

install_debug_packages() {
    local source_conf="$1"
    local apt_append_arg=""
    local package_spec package
    local to_install=()

    if [ -n "$source_conf" ]; then
        apt_append_arg="-c $source_conf"
    fi


    if [ ${#DEBUG_PACKAGES[@]} -gt 0 ]; then
        for package_spec in "${DEBUG_PACKAGES[@]}"; do
            package=$(echo "$package_spec" | cut -d '=' -f 1)
            if LANG=C apt-cache policy "$apt_append_arg" "$package"| grep -q "Candidate:"; then
                to_install+=("$package_spec")
            fi
        done

        if [ ${#to_install[@]} -gt 0 ]; then
            if ! apt-get install -y "$apt_append_arg" "${to_install[@]}"; then
                echo "Failed to install some dbgsym packages."
                return 1
            fi
        else
            echo "No dbgsym packages available to install."
            return 1
        fi
    else
        echo "No dbgsym packages to install."
        return 1
    fi

    echo "All dbgsym packages installed successfully."
}

install_dbg() {
    local package_name
    local get_from_source

    package_name="$1"
    get_from_source=0

    echo "Start to install dbgsym packages for ${package_name}"

    if ! get_debug_package_list "$package_name"; then
        return 1
    fi

    if [ ${#DEBUG_PACKAGES[@]} -eq 0 ]; then
        return 0
    fi

    if [ -f "$dbgconf" ]; then
        if apt update -c "$dbgconf"; then
            get_from_source=1
        fi
    fi

    if [ $get_from_source -eq 1 ]; then
        if install_debug_packages "$dbgconf"; then
            echo "install from dbg source sucess!"
            return 0
        fi
    fi
    echo "install from dbg source failed,now try to install from main source!"
    if ! install_debug_packages; then
        echo "Failed to install some dbgsym packages."
        return 1
    fi
    return 0
}

main() {
  if ! can_exec "$@"; then
    exit 1
  fi

  if ! install_dbg "$@"; then
    exit 1
  fi
}

main "$@"

