#!/bin/sh

# A general audio interface for LARBS.
# Based on a script by lukesmithxyz, checkout github.com/lukesmithxyz/voidrice
# Heavily extended and updated to a pipewire system

device=${LOOPBACK_DEVICE:-Easy Effects Source}

name=$(wpctl status | sed -n '/Sources/,$p' | sed -n '/Filter/q;p' | grep "$device" | sed -e "s/*/\ /g")
id=$( echo $name | cut -d '.' -f 1 | cut -c 5-)
node=$(wpctl inspect $id | grep node.name | cut -d '=' -f 2)

[ -z "$2" ] && num="5" || num="$2"

case "$1" in
  up) wpctl set-volume @DEFAULT_AUDIO_SINK@ "$num"%+
    pkill -RTMIN+4 $STATUSBAR ;;
  down) wpctl set-volume @DEFAULT_AUDIO_SINK@ "$num"%-
    pkill -RTMIN+4 $STATUSBAR ;;
  deaf) wpctl set-mute @DEFAULT_SINK@ 1
    pkill -RTMIN+4 $STATUSBAR ;;
  undeaf) wpctl set-mute @DEFAULT_SINK@ 0
    pkill -RTMIN+4 $STATUSBAR ;;
  mute) wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 1
    pkill -RTMIN+4 $STATUSBAR ;;
  unmute) wpctl set-mute @DEFAULT_AUDIO_SOURCE@ 0
    pkill -RTMIN+4 $STATUSBAR ;;
  deaf_toggle)
    if amixer get Master | tail -2 | grep -q '\[on\]'; then
      notify-send -u low 'sxhkd' 'Sound muted\nMicrophone muted'
      lmc mute
      lmc deaf
    else
      lmc mute
      lmc undeaf
      paplay ~/.local/bin/etc/sxhkd/audio/sound_resumed.wav
    fi ;;
  mute_toggle)
    echo $(amixer get Capture)
    if amixer get Capture | tail -2 | grep -q '\[on\]'; then
      lmc mute
      lmc undeaf
      paplay ~/.local/bin/etc/sxhkd/audio/mic_muted.wav
    else
      lmc unmute
      lmc undeaf
      paplay ~/.local/bin/etc/sxhkd/audio/mic_activated.wav
    fi ;;
  previous) playerctl previous
    sleep 1
    pkill -RTMIN+9 $STATUSBAR ;;
  playpause) playerctl play-pause
    sleep 1
    pkill -RTMIN+9 $STATUSBAR ;;
  next) playerctl next
    sleep 1
    pkill -RTMIN+9 $STATUSBAR ;;
  shuffle) playerctl shuffle toggle
    sleep 1
    pkill -RTMIN+9 $STATUSBAR ;;
  loop) playerctl loop none
    sleep 1
    pkill -RTMIN+9 $STATUSBAR ;;
  tap) wpctl set-volume $id 1.0
      touch ~/.local/state/loopback.lock
      pw-loopback -C $node &
      notify-send -u low 'sxhkd' "Started loopback of $device" ;;
  untap) killall pw-loopback
      rm ~/.local/state/loopback.lock
      notify-send -u low 'sxhkd' "Stopped all loopbacks" ;;
  tap_toggle)
    if [ -f ~/.local/state/loopback.lock ]; then
      lmc untap
    else
      lmc tap
    fi ;;
  *) cat << EOF
lmc: cli music interface for pulse for those with divine intellect too
grand to remember the pamixer commands.

Allowed options:
  up NUM	Increase volume (5% default)
  down NUM	Decrease volume (5% default)
  mute          Mute microphone
  unmute        Unmute microphone
  mute_toggle   Toggle between mute and unmute
  deaf          Deaf sound
  undeaf        Undeaf sound
  deaf_toggle   Toggle between deaf and undeaf
  playpause     Toggle play or pause in player
  previous      Play previous song in player
  next          Play next song in player
  shuffle       Toggle the shuffle mode
  loop          Toggle the loop mode
  tap           Start loopback for source device
  untap         Stop all active source loopbacks
  tap_toggle    Start or stop loopback depending if it is already running
  all else	Print this message

EOF
esac