#!/bin/zsh -feu

# How to get the list of controls:
# use -l option?
# am F6 & trial/error
# then:
#
# amixer -c 1 scontrols
# same as:   amixer  -c 1|grep control
#
# $ amixer -c 1   get 'Line Out'
#   pswitch
# Playback [on]
#  Front Right: Playback [on]


# asus:
# "Auto-Mute Mode" disabled
#
verbose=n
CARD=1
# known/expected controls:
auto_mute_control='Auto-Mute Mode'

line_control='Line Out'
speaker_control='Speaker'
headphones_control='Headphone'

# default outputs:
jack=0
builtin=0
phones=0

usage()
{
cat <<EOF
usage: ${0##*/} [+-jbh] [-c #] [--] ARGS...

-l list the controls (of the card $CARD)
-c card

-b builtin speakers
-j line-out jack (in dock)
-p phones

********
This is the ALSA level way of choosing the output.
EOF
}

AMIXER=(amixer -c $CARD)
master_volume=80

while getopts :bc:hjlLpv OPT; do
    case $OPT in
        j|+j)
            jack=1
            ;;
        c)
            CARD=$OPTARG
            AMIXER=(amixer -c $CARD)
            ;;
        p)
            phones=1
            ;;
        b|+b)
            builtin=1
            ;;

        l)
            $AMIXER scontrols
            echo
            # $AMIXER scontents

            $AMIXER controls| sort -n -t '=' -k '2'
            exit 0
            ;;
        L)
            master_volume=100
            ;;
        v)
            verbose=y
            ;;
        h)
            usage
            exit 0
            ;;
        *)
            usage >&2
            exit 2
    esac
done
shift OPTIND-1
OPTIND=1

if [[ $verbose = n ]]; then
    AMIXER=(amixer -c $CARD -q)
fi

#  Items: 'Disabled' 'Speaker Only' 'Line Out+Speaker'
#  Item0: 'Line Out+Speaker'
# only to the jack:


# for `auto_mute_control'
if [[ $builtin = 1 ]]; then
    value='Disabled'
elif [[ $jack = 1 ]]; then
    # I must keep "Speaker+LO" unmuted, and the volume.
    # but to avoid the builtin speakers, I need to have this:
    value='Speaker Only'
    # 2021: "Auto-Mute Mode"
    value='Disabled'
else
    # value='Speaker Only'
    value='Line Out+Speaker'
    value='Enabled'
fi

save_control()
{
    # value:
    # fixme:  -q  breaks it!
    amixer -c $CARD cget $1 |sed -ne 's/^.*: values=\(.*\)$/\1/g;T ;p'
}

restore_control()
{
    # value:
    # fixme:  -q  breaks it!
    amixer -c $CARD cset $1 $2
}


set_control()
{
    # name value
    cecho red $@
    $AMIXER cget $1
    $AMIXER cset $@
}

set_mixer_control()
{
    # name value
    cecho red $@
    $AMIXER sget $1
    $AMIXER sset $@
}

HEADPHONES_SAVE_FILE=~/.headphones

# for `line_control'
if [[ $jack = 1 ]]; then
    line_out='on'
    speaker='on'

    headphone=unmuted
    headphone_value=0
    # maybe unmuted ?

elif [[ $builtin = 1 ]] then
    line_out='off'
    speaker='on'
    # fixme:
    headphone=unmuted
    headphone_value=0
else
    # Head phones:
    line_out='off'
    speaker='off'
    headphone=unmuted

    # maybe only if still at 0?
    headphone_value=$(< $HEADPHONES_SAVE_FILE)
    rm -f $HEADPHONES_SAVE_FILE
    # will be restored!
fi

# both:
set_mixer_control Master on
# set_control 'Master Playback Switch' on
set_mixer_control Master $master_volume

# Auto-Mute Mode
set_mixer_control $auto_mute_control $value
$AMIXER sset 'Speaker+LO' 100 || : ignore



# pswitch
# value=off
set_mixer_control $line_control       $line_out || : ignore
# a bit counter-intuitive, & restrictive:
set_mixer_control $speaker_control    $speaker  || : ignore

#set_control 'Headphone Playback Switch' $headphone
set_mixer_control $headphones_control $headphone || : ignore

# set volumes:
if [[ ${headphone_value-} = 0 ]]; then
    if [[ !  -e $HEADPHONES_SAVE_FILE ]]; then
        save_control "numid=3,iface=MIXER,name='Headphone Playback Volume'" >! $HEADPHONES_SAVE_FILE
    fi
fi
set_mixer_control $headphones_control $headphone_value || : ignore
# : # restore_control "numid=3,iface=MIXER,name='Headphone Playback Volume'" $(<$HEADPHONES_SAVE_FILE)

SET_PULSE_AUDIO=(pactl --server unix:/run/user/1000/pulse/native)

# not HDMI:
if false; then
    $SET_PULSE_AUDIO set-card-profile 1 output:analog-stereo+input:analog-stereo
fi
# HDMI:
#                                               set-card-profile 0 output:hdmi-stereo+input:analog-stereo

echo >/dev/null <<EOF
https://wiki.archlinux.org/index.php/PulseAudio/Examples#HDMI_output_configuration
todo: cat /sys/class/drm/card0-DP-1/status

#
cat /sys/class/drm/card0/card0-HDMI-A-1/status
    /sys/class/drm/card0/card0-HDMI-A-2/status
    /sys/class/drm/card0/card0-HDMI-A-3/status
EOF

# save:
# amixer -c 2    cget numid=3,iface=MIXER,name='Headphone Playback Volume'|sed  's/: values=//;'
# amixer -c 2 cset numid=3,iface=MIXER,name='Headphone Playback Volume' '0,0'
