#!/usr/bin/env bash

# set -euo pipefail

readonly LL_DRIVER_DATA_DIR="/tmp/utcs_ll_driver_data"

get_ll_arch() {
    local arch
    arch=$(dpkg --print-architecture)

    case "$arch" in
        amd64) printf '%s\n' "x86_64" ;;
        *) printf '%s\n' "$arch" ;;
    esac
}

install_uab_if_exists() {
    local driver_path=$1

    if [[ -f "$driver_path" ]]; then
        ll-cli --no-dbus install "$driver_path"
    else
        printf '文件不存在: %s\n' "$driver_path"
    fi
}

get_utcs_ll_driver_data() {
    mkdir -p "$LL_DRIVER_DATA_DIR"
    cd "$LL_DRIVER_DATA_DIR"

    apt download utcs-ll-driver-data

    local deb_file
    deb_file=$(find . -maxdepth 1 -type f -name '*.deb' -printf '%T@ %f\n' | sort -nr | head -n 1 | awk '{print $2}')
    dpkg-deb -x "$deb_file" .
}

remove_utcs_ll_driver_data() {
    if [[ -d "$LL_DRIVER_DATA_DIR" ]]; then
        rm -rf "$LL_DRIVER_DATA_DIR"
    fi
}