#!/bin/bash
#
#  Bluetooth Module Installation Tool v1.0.0
#  Created by Enrico Buttignol (ebuttignol@radiantcooling.com) on Dec 14, 2022
#  Copyright by Messana Hydronic Technologies
#
#  Changelog:
#
#  Mmm dd, yyyy - Name Surname (email@server.com) - description
#
#  sudo ./mht-bt-install

# Variable definitions
# Colors
COLOR_SECTION='\033[0;36m'
COLOR_INFO='\033[0;96m'
COLOR_EXECUTED='\033[0;32m'
COLOR_WARNING='\033[33m'
COLOR_ERR='\033[0;31m'
NOCOLOR='\033[0m'

# Values
VPN=$( { ip a s tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'; } 2> /dev/null )
IS_PRODUCTION_ENV=$(/usr/share/messana/scripts/mht-get-mcu-environment.sh "$VPN")
PORTAL_URL="https://service.radiantcooling.com"
API_KEY="pPq8DJsExPjMLLx)INM2HdRTB3kQcDxm"
REDIS_VERSION=$(redis-server -v | grep -Po '(?<=Redis server v=)([\d]*.[\d]*.[\d]*)' | awk -F'[^0-9]+' '{ print $1 }')
OLD_REDIS_VERSION=3
if [ "$REDIS_VERSION" -gt "$OLD_REDIS_VERSION" ]; then
    REDIS_WARNING_PASSWORD="--no-auth-warning"
else
    REDIS_WARNING_PASSWORD=""
fi
REDISKEY=$(awk '/^requirepass /{print $2}' /etc/redis/redis.conf | uniq)
CODE=$(redis-cli -a "$REDISKEY" $REDIS_WARNING_PASSWORD GET CFG_MCU_000_Code)
MCU_NAME=$(redis-cli -a "$REDISKEY" $REDIS_WARNING_PASSWORD GET CFG_MCU_000_Name)

printf "\n"
printf "%s""${COLOR_SECTION}[»»»] MHT BLUETOOTH MODULE PKGS CHECK AND INSTALL STARTED [«««]${NOCOLOR}"
printf "\n"

# Install hwinfo package
apt install hwinfo -y

# Check if the Bluetooth module is available
BT_VAR1=$(hwinfo --bluetooth | awk '/Hardware Class/ {print $3}')
BT_VAR2='bluetooth'
BT_REQUIRED_PKGS='bluetooth bluez libbluetooth-dev libudev-dev'
BT_REQUIRED_PKGS_CHK='bluetooth'

# Check the Bluetooth module version
# ┌─────────────────────────────────┐
# | HCI version | Bluetooth version |
# |-------------|-------------------|
# | 0 (0x0)     | 1.0b              |
# | 1 (0x1)     | 1.1               |
# | 2 (0x2)     | 1.2               |
# | 3 (0x3)     | 2.0               |
# | 4 (0x4)     | 2.1               |
# | 5 (0x5)     | 3.0               |
# | 6 (0x6)     | 4.0               |
# | 7 (0x7)     | 4.1               |
# | 8 (0x8)     | 4.2               |
# | 9 (0x9)     | 5.0               |
# | 10 (0xa)    | 5.1               |
# | 11 (0xb)    | 5.2               |
# | 12 (0xc)    | 5.3               |
# └─────────────────────────────────┘

if [ "$BT_VAR1" = "$BT_VAR2" ]; then
    BT_AVAILABLE='true'
    printf "%s""${COLOR_INFO}[i]${NOCOLOR} Is the Bluetooth module available? ${COLOR_EXECUTED}${BT_AVAILABLE}${NOCOLOR}\n"
    BT_REQUIRED_PKGS_OK=$(dpkg-query -W --showformat='${Status}\n' $BT_REQUIRED_PKGS | grep "install ok installed")
    printf "\nChecking for packages $BT_REQUIRED_PKGS\n"
    if [ "" = "$BT_REQUIRED_PKGS_OK" ]; then
        printf "%s""${COLOR_INFO}[i]${NOCOLOR} The required Bluetooth packages are not installed, setting up ${COLOR_EXECUTED}$BT_REQUIRED_PKGS${NOCOLOR}.\n"
        apt-get -qy install $BT_REQUIRED_PKGS
    else
        /usr/share/messana/scripts/prepare-bleno
        BT_VERSION=$(hciconfig -a | awk 'NR==14 { print substr($3,1,9) }')
        printf "%s""${COLOR_INFO}[i]${NOCOLOR} The Bluetooth version is: ${COLOR_WARNING}${BT_VERSION}${NOCOLOR}\n"
    fi
else
    BT_AVAILABLE='false'
    printf "%s""${COLOR_INFO}[i]${NOCOLOR} Is the Bluetooth module available? ${COLOR_WARNING}${BT_AVAILABLE}${NOCOLOR}\n"
fi

/usr/share/messana/scripts/mht-hw-details-update.sh

printf "%s""${COLOR_SECTION}[»»»] MHT BLUETOOTH MODULE PKGS CHECK AND INSTALL COMPLETED [«««]${NOCOLOR}\n"
printf "\n"
printf "\n"

shred -u /etc/cron.daily/mht-bt-install

exit 0
