if [ ! -t 0 ]; then
  alacritty -T 'JDB Office Mic' -o window.dimensions.columns=80 window.dimensions.lines=40 -e "$0"
  exit
fi

let cur_sink=`pacmd list-sinks | grep '\*' | cut -d ' ' -f 5`
jack_control start ds alsa
a2jmidid -e &
let a2j_pid=$!
sleep 1
carla /home/jdb/documents/jdb-office-mic.carxp &
let carla_pid=$!

pacmd set-default-sink jack_out

sleep 4

#alacritty -T 'JDB Office Mic' -o window.dimensions.columns=40 window.dimensions.lines=4 -e bash 'echo "JDB Office Mic is running. 🔴" && read'

# Uses VT100 escape codes to draw.
# see https://espterm.github.io/docs/VT100%20escape%20codes.html
echo -e "\n╓──────────────────────────────╖"
echo -e   "║JDB Office Mic is running. 🔴 ║"
echo -ne  "╙──────────────────────────────╜\033[1A"
let on=0
while true
do
  if read -t 2; then break
  elif [[ $on == 1 ]]; then
    echo -ne "\033[3D  ║"
    #echo -ne "\b\b\b\b   ║"
    let on=0
  else
    echo -ne "\033[4D🔴 ║"
    let on=1
  fi
done

echo -e "\033[2A\033[2K\033[1B\033[2K\033[1B\033[2K\033[2AJDB Office Mic is exiting...\n"
#echo -e "\033[1A║JDB Office Mic is exiting...  ║\n\n"

kill $carla_pid
kill $a2j_pid
sleep 2
jack_control stop
pacmd set-default-sink $cur_sink

echo "JDB Office Mic is shutdown."
