#!/bin/sh
# startdate: 2024-02-26-2 08:07
# Reference:
#    https://askubuntu.com/questions/1267949/how-do-i-automatically-switch-pulseaudio-input-to-headset-upon-connection/1284966#1284966
# Dependencies:
#    acpid, pulseaudio

# just in case we drop use-mic.sh
index="$( pacmd list-sources | grep -E 'index|ports|analog-input-headset-mic' | grep -E '\*\sindex:\s+[0-9]' | awk '{print $NF}' )"

acpi_listen | while IFS= read -r line ;
do
   echo "${line}" | grep -qE "HEADPHONE plug" 1>/dev/null 2>&1 && /usr/local/bin/use-mic.sh ;
   # or we could run "pacmd set-source-port ${index} analog-input-headset-mic
done
