#!/bin/bash
# Match model name of machine, like "UX31A".

HEAD="[utcs]"

is_productname_exists() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local NAME="$1"
    grep -q "${NAME}" /sys/class/dmi/id/product_name </dev/null 2>/dev/null
}

is_boardname_exists() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local NAME="$1"
    grep -q "${NAME}" /sys/class/dmi/id/board_name </dev/null 2>/dev/null
}

# Check CPU model name, like "Intel(R) Core(TM) i7-xxxx" or "AMD Ryzen ...".
is_cpu_model_exists() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local NAME="$1"
    # /proc/cpuinfo is always available; use first model name line.
    local MODEL
    MODEL=$(grep -m1 -E '^model name\s*:' /proc/cpuinfo </dev/null 2>/dev/null | cut -d':' -f2-)
    # Fallback for some architectures.
    if [ -z "$MODEL" ]; then
        MODEL=$(grep -m1 -E '^Processor\s*:' /proc/cpuinfo </dev/null 2>/dev/null | cut -d':' -f2-)
    fi
    echo "$MODEL" | grep -qi "${NAME}" 2>/dev/null
}

is_context_exists() {
    if [ $# -ne 2 ]; then
        warn "input param error"
        return 0
    fi
    local key=$1
    local confile=$2
    grep -q ^"${key}" ${confile} </dev/null 2>/dev/null
}

# Check PCI device with id
is_pci_exists() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local PCI_ID="$1"
    local PCI_INFO
    PCI_INFO=$(lspci -d "${PCI_ID}" </dev/null 2>/dev/null)
    test -n "${PCI_INFO}"
}

is_usb_exists() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local USB_ID="$1"
    local USB_INFO
    USB_INFO="$(lsusb -d "${USB_ID}" </dev/null 2>/dev/null)"
    test -n "${USB_INFO}"
}

is_linux_version_4_19() {
    local UNAME_R=`uname -r`
    local VERSION
    local PATCHLEVEL
    local SUBLEVEL
    VERSION="$(echo $UNAME_R | awk -F '[.-]' '{print $1}' 2>/dev/null)"
    PATCHLEVEL="$(echo $UNAME_R | awk -F '[.-]' '{print $2}' 2>/dev/null)"
    SUBLEVEL="$(echo $UNAME_R | awk -F '[.-]' '{print $3}' 2>/dev/null)"
    if [ $VERSION -eq 4 ] && [ $PATCHLEVEL -eq 19 ]; then
        return 0
    else
        return 1
    fi
}

is_linux_version_5_10() {
    local UNAME_R=`uname -r`
    local VERSION
    local PATCHLEVEL
    local SUBLEVEL
    VERSION="$(echo $UNAME_R | awk -F '[.-]' '{print $1}' 2>/dev/null)"
    PATCHLEVEL="$(echo $UNAME_R | awk -F '[.-]' '{print $2}' 2>/dev/null)"
    SUBLEVEL="$(echo $UNAME_R | awk -F '[.-]' '{print $3}' 2>/dev/null)"
    if [ $VERSION -eq 5 ] && [ $PATCHLEVEL -eq 10 ]; then
        return 0
    else
        return 1
    fi
}

is_installed() {
    local PKGNAME=${1}
    local EXIST
    EXIST=$(dpkg --get-selections $PKGNAME 2>/dev/null)
    test -n "${EXIST}"
}

msg() {
    echo ${HEAD} "$1"
}

warn() {
    echo ${HEAD} "$1"
}

head() {
    echo ${HEAD} ${1}
}

force_overwrite_install() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local PKGNAME="$1"
    DEBIAN_FRONTEND="noninteractive" apt-get -y \
        -o Dpkg::Options::="--force-overwrite" \
        -o Dpkg::Options::="--force-confdef" \
        -o Dpkg::Options::="--force-confold" \
        --no-install-recommends --no-install-suggests \
        --allow-unauthenticated \
        install $PKGNAME
}

keep_conf_install() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local PKGNAME="$1"
    DEBIAN_FRONTEND="noninteractive" apt-get -y \
        -o Dpkg::Options::="--force-confdef" \
        -o Dpkg::Options::="--force-confold" \
        --no-install-recommends --no-install-suggests \
        --allow-unauthenticated \
        install $PKGNAME
}

unkeep_conf_install() {
    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi
    local PKGNAME="$1"
    DEBIAN_FRONTEND="noninteractive" apt-get -y \
        -o Dpkg::Options::="--force-confdef" \
        -o Dpkg::Options::="--force-confnew" \
        --no-install-recommends --no-install-suggests \
        --allow-unauthenticated \
        install $PKGNAME
}

is_arch_x86_64() {
    local ARCH=`arch`
    if [ "$ARCH" == "x86_64" ]; then
        return 0
    else
        return 1
    fi
}

is_arch_aarch64() {
    local ARCH=`arch`
    if [ "$ARCH" == "aarch64" ]; then
        return 0
    else
        return 1
    fi
}

is_arch_loongarch() {
    local ARCH=`arch`
    if [ "$ARCH" == "loongarch64" ]; then
        return 0
    else
        return 1
    fi
}

is_arch_mips() {
    local ARCH=`arch`
    if [ "$ARCH" == "mips64el" ]; then
        return 0
    else
        return 1
    fi
}

is_arch_sw() {
    local ARCH=`arch`
    if [ "$ARCH" == "sw_64" ]; then
        return 0
    else
        return 1
    fi
}

save_install_one_drv()
{
    local PACKAGE_NAME
    local ret

    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi

    PACKAGE_NAME=$1
    echo "----> install ${PACKAGE_NAME}"
    is_installed $PACKAGE_NAME
    ret=$?
    if [ $ret == 0 ]; then
        echo "$PACKAGE_NAME has been installed"
    else
        keep_conf_install $PACKAGE_NAME
        ret=$?
    fi

    return $ret
}

unsave_install_one_drv()
{
    local PACKAGE_NAME
    local ret

    if [ $# -ne 1 ]; then
        warn "input param error"
        return 1
    fi

    PACKAGE_NAME=$1
    echo "----> install ${PACKAGE_NAME}"
    is_installed $PACKAGE_NAME
    ret=$?
    if [ $ret == 0 ]; then
        echo "$PACKAGE_NAME has been installed"
    else
        unkeep_conf_install $PACKAGE_NAME
        ret=$?
    fi

    return $ret
}