#!/usr/bin/env bash

theme_path="$HOME/.config/rofi/volume/volume_menu.rasi"

volume=''
microphone=''
pavucontrol=''

rofi_cmd() {
  rofi -theme "$theme_path" -dmenu -markup-rows
}

# Microphone Info
amixer get Capture | grep '\[on\]' &>/dev/null
if [[ "$?" == 0 ]]; then
  microphone=''
else
  microphone=''
fi

# Microphone Info
amixer get Master | grep '\[on\]' &>/dev/null
if [[ "$?" == 0 ]]; then
  volume=''
else
  volume=''
fi

run_cmd() {
  if [[ "$1" == '--opt1' ]]; then
    amixer set Master toggle
  elif [[ "$1" == '--opt2' ]]; then
    amixer set Capture toggle
  elif [[ "$1" == '--opt3' ]]; then
    exec pavucontrol
  fi
}

run() {
  echo -e "$volume\n$microphone\n$pavucontrol" | rofi_cmd
}

chosen="$(run)"
case ${chosen} in
    $volume)
    run_cmd --opt1
        ;;
    $microphone)
    run_cmd --opt2
        ;;
    $pavucontrol)
    run_cmd --opt3
        ;;
esac