#!/usr/bin/env bash

# available sinks listed here: pactl list sinks

CURRENT_SINK="$(pactl get-default-sink)"
CURRENT_PRESET="$(easyeffects --active-preset output)"
LOUD_SINK="alsa_output.pci-0000_00_1f.3.analog-stereo"
LOUD_PRESET="Defender"
HEAD_SINK="alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo"
HEAD_PRESET="Techno"

set_head() {
    if [ "$CURRENT_SINK" != "$HEAD_SINK" ]; then
        pactl set-default-sink "$HEAD_SINK" || exit 1
        easyeffects --load-preset "$HEAD_PRESET" || exit 2
        echo -e "Current sink\t: $HEAD_SINK"
        echo -e "Current preset\t: $HEAD_PRESET"
    fi
}

set_loud() {
    if [ "$CURRENT_SINK" != "$LOUD_SINK" ]; then
        pactl set-default-sink "$LOUD_SINK" || exit 1
        easyeffects --load-preset "$LOUD_PRESET" || exit 2
        echo -e "Current sink\t: $LOUD_SINK"
        echo -e "Current preset\t: $LOUD_PRESET"
    fi
}

show_help() {
    echo "Usage: $(basename "$0") [-h|--help|--loud|--head|--switch]"
    echo
    echo "Switch audio output and apply appropriate easyffects preset."
    echo
    echo "Options:"
    echo "    -h, --help    Show this help message"
    echo "    --loud        Enable loud speakers"
    echo "    --head        Enable headphones"
    echo "    --switch      Switch between loud and headphones"
    echo
}

if [ -z "$1" ]; then
    echo -e "Loud sink\t: $LOUD_SINK"
    echo -e "Loud preset\t: $LOUD_PRESET"
    echo -e "Head sink\t: $HEAD_SINK"
    echo -e "Head preset\t: $HEAD_PRESET"
    echo
    echo -e "Current sink\t: $CURRENT_SINK"
    echo -e "Current preset\t: $CURRENT_PRESET"
    exit
fi

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    show_help
    exit
fi

if [ "$1" == "--loud" ]; then
    set_loud
    exit
fi

if [ "$1" == "--head" ]; then
    set_head
    exit
fi

if [ "$1" == "--switch" ]; then
    case "$CURRENT_SINK" in
        *$LOUD_SINK*) set_head ;;
        *$HEAD_SINK*) set_loud ;;
        *) show_help ;;
    esac
fi
