#!/bin/bash

# 系统音量控制脚本
# 基于 org.ukui.volume.control D-Bus 接口

# 设置环境变量
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
export DISPLAY=:0

DBUS_SERVICE="org.ukui.volume.control"
DBUS_PATH="/org/ukui/volume/control"
DBUS_INTERFACE="org.ukui.volume.control"

# 类型常量
INPUT=0
OUTPUT=1
SINK_INPUT=2
SOURCE_OUTPUT=3

# 显示使用帮助
show_help() {
    echo "使用方法: $0 <命令> [参数]"
    echo ""
    echo "命令:"
    echo "  get-volume   获取音量值"
    echo "  set-volume   设置音量值"
    echo "  set-mute     设置静音状态"
    echo "  set-enabled  设置设备启用状态"
    echo "  -h, --help   显示帮助信息"
    echo ""
    echo "get-volume 参数:"
    echo "  <type>       类型 (0:INPUT, 1:OUTPUT, 2:SINK_INPUT, 3:SOURCE_OUTPUT)"
    echo "  <index>      索引 (-1 或应用索引)"
    echo ""
    echo "set-volume 参数:"
    echo "  <type>       类型 (0:INPUT, 1:OUTPUT, 2:SINK_INPUT, 3:SOURCE_OUTPUT)"
    echo "  <index>      索引 (-1 或应用索引)"
    echo "  <volume>     音量值 (0-100)"
    echo ""
    echo "set-mute 参数:"
    echo "  <type>       类型 (0:INPUT, 1:OUTPUT, 2:SINK_INPUT, 3:SOURCE_OUTPUT)"
    echo "  <index>      索引 (-1 或应用索引)"
    echo "  <mute>       静音状态 (true/false)"
    echo ""
    echo "set-enabled 参数:"
    echo "  <cardname>   声卡名称"
    echo "  <portname>   端口名称"
    echo "  <enable>     启用状态 (true/false)"
    echo ""
    echo "示例:"
    echo "  $0 get-volume 1 -1           # 获取输出设备音量"
    echo "  $0 set-volume 1 -1 50        # 设置输出设备音量为 50%"
    echo "  $0 set-mute 1 -1 true        # 静音输出设备"
    echo "  $0 set-enabled \"pci-0000_00_1f.3\" \"analog-output-lineout\" true  # 启用指定设备"
}

# 获取音量值
get_volume() {
    local type="$1"
    local index="$2"
    
    local result
    result=$(dbus-send --print-reply --dest=$DBUS_SERVICE $DBUS_PATH \
        $DBUS_INTERFACE.getVolume \
        int32:$type \
        int32:$index 2>/dev/null)
    
    if [ $? -eq 0 ]; then
        local volume=$(echo "$result" | grep -oP 'int32 \K[0-9]+' | head -1)
        echo "$volume"
    else
        echo "获取音量失败" >&2
        exit 1
    fi
}

# 设置音量值
set_volume() {
    local type="$1"
    local index="$2"
    local volume="$3"
    
    echo "设置音量: type=$type, index=$index, volume=$volume"
    
    dbus-send --print-reply --dest=$DBUS_SERVICE $DBUS_PATH \
        $DBUS_INTERFACE.setVolume \
        int32:$type \
        int32:$index \
        int32:$volume
    
    if [ $? -eq 0 ]; then
        echo "音量设置成功"
    else
        echo "音量设置失败"
        exit 1
    fi
}

# 设置静音状态
set_mute() {
    local type="$1"
    local index="$2"
    local mute="$3"
    
    echo "设置静音状态: type=$type, index=$index, mute=$mute"
    
    dbus-send --print-reply --dest=$DBUS_SERVICE $DBUS_PATH \
        $DBUS_INTERFACE.setMute \
        int32:$type \
        int32:$index \
        boolean:$mute
    
    if [ $? -eq 0 ]; then
        echo "静音状态设置成功"
    else
        echo "静音状态设置失败"
        exit 1
    fi
}

# 设置设备启用状态
set_enabled() {
    local cardname="$1"
    local portname="$2"
    local enable="$3"
    
    echo "设置设备启用状态: cardname=$cardname, portname=$portname, enable=$enable"
    
    dbus-send --print-reply --dest=$DBUS_SERVICE $DBUS_PATH \
        $DBUS_INTERFACE.setEnabled \
        string:"$cardname" \
        string:"$portname" \
        boolean:$enable
    
    if [ $? -eq 0 ]; then
        echo "设备启用状态设置成功"
    else
        echo "设备启用状态设置失败"
        exit 1
    fi
}

# 主函数
main() {
    # 检查是否请求帮助
    if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        show_help
        exit 0
    fi
    
    # 执行命令
    case "$1" in
        get-volume)
            if [ $# -ne 3 ]; then
                echo "错误: get-volume 需要 2 个参数 (type, index)"
                show_help
                exit 1
            fi
            get_volume "$2" "$3"
            ;;
        set-volume)
            if [ $# -ne 4 ]; then
                echo "错误: set-volume 需要 3 个参数 (type, index, volume)"
                show_help
                exit 1
            fi
            set_volume "$2" "$3" "$4"
            ;;
        set-mute)
            if [ $# -ne 4 ]; then
                echo "错误: set-mute 需要 3 个参数 (type, index, mute)"
                show_help
                exit 1
            fi
            set_mute "$2" "$3" "$4"
            ;;
        set-enabled)
            if [ $# -ne 4 ]; then
                echo "错误: set-enabled 需要 3 个参数 (cardname, portname, enable)"
                show_help
                exit 1
            fi
            set_enabled "$2" "$3" "$4"
            ;;
        *)
            echo "错误: 未知命令 '$1'"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
if [ "$0" = "$BASH_SOURCE" ]; then
    main "$@"
fi
