#!/bin/bash

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

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

CR='
'

DEVICEID="$(cat /tmp/bluepup/chosen-new-device)"
#/tmp/bluepup/new-info.$DEVICEID ex:
#Device 20:15:10:15:1C:AF (public)
#	Name: Bluetooth Mouse
#	Alias: Bluetooth Mouse
#	Class: 0x00002580
#	Icon: input-mouse
#	Paired: no
#	Trusted: no
#	Blocked: no
#	Connected: no
#	LegacyPairing: yes

PHflg="$(grep -o 'Icon: phone' /tmp/bluepup/new-info.${DEVICEID})" #20201106

grep -q 'Blocked: no' /tmp/bluepup/new-info.${DEVICEID}
if [ $? -ne 0 ];then
 echo -e "\033[0;94m[bluetooth]\033[0m# bluetoothctl unblock ${DEVICEID}" >> /tmp/bluepup/btctl_in
 bluetoothctl unblock ${DEVICEID} >> /tmp/bluepup/btctl_in
 /usr/local/bluepup/support/wait-response "unblock ${DEVICEID}" "${?}"
 [ $? -ne 0 ] && exit_func 1
fi

##now pair...
BTDret=1
grep -q 'Paired: yes' /tmp/bluepup/new-info.${DEVICEID}
if [ $? -ne 0 ];then
 MSG1="$(gettext 'Attempting pair with:')"
 echo "<b><span color='#e08000'>${MSG1}${CR}${DEVICEID}</span></b>" > /tmp/bluepup/frame-status.msg
 
 echo -e "\033[0;94m[bluetooth]\033[0m# bluetoothctl pairable on" >> /tmp/bluepup/btctl_in
 bluetoothctl pairable on >> /tmp/bluepup/btctl_in
 /usr/local/bluepup/support/wait-response "pairable on" "${?}"
 [ $? -ne 0 ] && exit_func 1
 
 #20201106 very frustrating, pair works for my phone, but connect fails.
 # common problem reported  on internet. however, bt-device succeeds...
 # note, bt-device does not have separate "pair" command, only "-c" which pairs and connects.
 if [ "$PHflg" ];then
  echo -e "\033[0;94m[bluetooth]\033[0m# bt-device --connect=${DEVICEID}" >> /tmp/bluepup/btctl_in
  echo 'yes' | bt-device --connect=${DEVICEID} >> /tmp/bluepup/btctl_in
  BTDret=$?
  sleep 2
  PAIRflg="$(bluetoothctl paired-devices | grep -o "${DEVICEID}")"
  if [ "$PAIRflg" == "" ];then
   /usr/local/bluepup/support/wait-response "pair ${DEVICEID}" "1"
   [ $? -ne 0 ] && exit_func 1
  fi
 else
  echo -e "\033[0;94m[bluetooth]\033[0m# bluetoothctl pair ${DEVICEID}" >> /tmp/bluepup/btctl_in
  bluetoothctl pair ${DEVICEID} >> /tmp/bluepup/btctl_in
  /usr/local/bluepup/support/wait-response "pair ${DEVICEID}" "${?}"
  [ $? -ne 0 ] && exit_func 1
 fi
fi

#***also we can check***
# bluetoothctl paired-devices

grep -q 'Trusted: yes' /tmp/bluepup/new-info.${DEVICEID}
if [ $? -ne 0 ];then
 echo -e "\033[0;94m[bluetooth]\033[0m# bluetoothctl trust ${DEVICEID}" >> /tmp/bluepup/btctl_in
 bluetoothctl trust ${DEVICEID} >> /tmp/bluepup/btctl_in
 /usr/local/bluepup/support/wait-response "trust ${DEVICEID}" "${?}"
 [ $? -ne 0 ] && exit_func 1
fi

if [ $BTDret -ne 0 ];then #20201106 bt-device already connected.
 #20201103 we are having connect fail. it may be that have to connect immediately
 # after pair, so instead of doing it in separate 'connect' script, do it here...
 grep -q 'Connected: yes' /tmp/bluepup/new-info.${DEVICEID}
 if [ $? -ne 0 ];then
  echo -e "\033[0;94m[bluetooth]\033[0m# bluetoothctl connect ${DEVICEID}" >> /tmp/bluepup/btctl_in
  bluetoothctl connect ${DEVICEID} >> /tmp/bluepup/btctl_in
  #20201104 try again...
  if [ $? -ne 0 ];then
   MSG2b="$(gettext 'Connect fail:')"
   MSG2c="$(gettext 'Trying again...')"
   echo "<b><span color='#a00000'>${MSG2b}${CR}${DEVICEID}</span>${CR}<big><span color='#ff0000'>${MSG2c}</span></big></b>" > /tmp/bluepup/frame-status.msg
   sleep 1
   bluetoothctl connect ${DEVICEID} >> /tmp/bluepup/btctl_in
   /usr/local/bluepup/support/wait-response "connect ${DEVICEID}" "${?}"
   #[ $? -ne 0 ] && exit_func 1
  fi
 fi
fi

#success, so record as registered...
sleep 1
DEVINFO="$(LANG=C bluetoothctl info ${DEVICEID})"
#peculiar situation with my bt speaker, got nothing in DEVINFO (spurious thing, normally is ok)...
if [ "$DEVINFO" == "" ];then
 sleep 2
 DEVINFO="$(LANG=C bluetoothctl info ${DEVICEID})"
fi
echo "$DEVINFO" > /tmp/bluepup/reg-info.${DEVICEID}
cp -a -f /tmp/bluepup/reg-info.${DEVICEID} /var/local/bluepup/

#update msg in Status fram...
REGmsg1="$(gettext 'Register success:')"
echo "<b><span color='#00a000'>${REGmsg1}${CR}${DEVICEID}</span></b>" > /tmp/bluepup/frame-status.msg

#write yes, so that the timer can detect it...
echo -n "true" > /tmp/bluepup/reg-success-flg

#transfer it from the "New devices" list to "Registered devices"...
NEWREG0="$(grep "${DEVICEID}" /tmp/bluepup/found-new-devices)"
NEWREG1="$(echo -n "$NEWREG0" | sed -e 's%defbticon%mini-cross%' )" #change icon.
echo "$NEWREG1" >> /tmp/bluepup/found-reg-devices #will be updated by the timer, indirectly...
#timer waits on change to 'found-new-devices' to update both New and Registered lists...
#note, cannot use "sed -i", have to do it this way...
sed "/${DEVICEID}/d" /tmp/bluepup/found-new-devices > /tmp/bluepup/fnd-temp #timer in gtkdialog will cause auto-update.
cat /tmp/bluepup/fnd-temp > /tmp/bluepup/found-new-devices

#situation when registered a new device, 'register' script ran, then device moved from "New devices"
#to "Registered devices", but if user did not click on the device in "Registered devices",
#instead immediately clicked the "CONNECT" button, then
#/tmp/bluepup/chosen-reg-device file will be empty. so, workaround...
cp -f /tmp/bluepup/chosen-new-device /tmp/bluepup/chosen-reg-device
echo -n '' > /tmp/bluepup/chosen-new-device

#20201107 tell bluepup if this is a phone...
PHflg="$(grep -o 'Icon: phone' /tmp/bluepup/reg-info.${DEVICEID})"
if [ "$PHflg" ];then
 echo -n 'true' > /tmp/bluepup/device-is-a-phone
else
 echo -n 'false' > /tmp/bluepup/device-is-a-phone
fi

#20201108
CONNnew="$(grep -o 'Connected: .*' /tmp/bluepup/reg-info.${DEVICEID} | tr -s ' ' | cut -f 2 -d ' ')"
PAIRnew="$(grep -o 'Paired: .*' /tmp/bluepup/reg-info.${DEVICEID} | tr -s ' ' | cut -f 2 -d ' ')"

#20201103 now connecting in this script, instead of separate 'connect', so 
# import this code from 'connect'...
if [ "$CONNnew" == "yes" ];then
 #run-mscw will test if an audio device, and if so will launch Multiple Sound Card Wizard...
 /usr/local/bluepup/support/run-mscw ${DEVICEID} &
 #change the icon...
 sed -i -e "s%.*${DEVICEID}%mini-tick-green|${DEVICEID}%" /tmp/bluepup/found-reg-devices #will be updated by the timer, indirectly...
 #finally, write to this file, a timer will auto-detect change...
 echo -n "$DEVICEID" > /tmp/bluepup/reg-dev-chosen-processed
elif [ "$PAIRnew" == "yes" ];then #20201108
 sed -i -e "s%.*${DEVICEID}%mini-tick-orange|${DEVICEID}%" /tmp/bluepup/found-reg-devices #will be updated by the timer, indirectly...
 #finally, write to this file, a timer will auto-detect change...
 echo -n "$DEVICEID" > /tmp/bluepup/reg-dev-chosen-processed
else
 REGmsg4="$(gettext 'WARNING: not paired')"
 echo "<b><big><span color='#a00000'>${REGmsg4}</span></big></b>" >> /tmp/bluepup/frame-status.msg
fi

sleep 1.2
echo -n 'limbo' > /tmp/bluepup/current-operation #20201105
sleep 10
echo "<span color='#ffa000'><b>$(gettext 'Click the SCAN button to')${CR}$(gettext 'search for new devices')</b></span>" > /tmp/bluepup/scan-status
###end###
