#!/bin/sh
#device address is in /tmp/bluepup/chosen-reg-device

export TEXTDOMAIN=bluepup
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8

exit_func() {
 echo -n 'limbo' > /tmp/bluepup/current-operation
 exit $1
}

CR='
'
DEVICEID="$(cat /tmp/bluepup/chosen-reg-device)"

#NOTE:
#most of the unregister code is still in bluepup script.



AUDIOD=''
if pidof bluealsa >/dev/null;then AUDIOD='bluealsa'; fi
if pidof pulseaudio >/dev/null;then AUDIOD='pulseaudio'; fi

#might want to change to internal audio card...
BTflg=''
if [ -s /root/.asoundrc ];then #only run mscw if default o/p is $DEVICEID...
 if [ "$AUDIOD" == "pulseaudio" ];then #20201102 see similar code in mscw.
  PA_DEVID="$(echo -n "$DEVICEID" | tr ':' '_')"
  BTflg="$(pacmd list-sinks | grep 'name:.*bluez_sink' | grep -o "${PA_DEVID}")"
 else
  grep -q "${DEVICEID}" /root/.asoundrc ####NOTE: pulseaudio has different content, ref /usr/sbin/mscw
  [ $? -eq 0 ] && BTflg='yes'
 fi
 if [ "$BTflg" ];then
  grep -q 'Icon:.*audio-card' /var/local/bluepup/reg-info.${DEVICEID}
  if [ $? -eq 0 ];then
   mscw &
  fi
 fi
fi

rm -f /var/local/bluepup/reg-info.${DEVICEID}

sleep 1.2
exit_func 0
###end###
