#!/bin/bash

if [ "${1}"x == ""x ]; then
    echo -e "Usage: mod-bluetooth <command>"
    echo -e ""
    echo -e "Valid commands:"
    echo -e "\t discovery \t Activate discovery for 2 minutes"
    echo -e "\t status    \t Show current bluetooth network status"
    echo -e "\t address   \t Show current bluetooth device address (MAC)"
    echo -e "\t name      \t Show current bluetooth device name"
    exit 1
fi

SKIP_HW_DESC_INIT=1
source /etc/mod-hardware-descriptor.env

case "${1}" in
    "discovery")
        if [ ! -f /tmp/bluetooth-status ]; then
            echo "bluetooth not available, cannot enable discovery"
            exit 2
        fi
        if ( cat /tmp/bluetooth-status | grep -q "Unsupported" ); then
            echo "bluetooth unsupported, cannot enable discovery"
            exit 2
        fi
        echo "enabling discovery"
        dbus-send --system \
            --type=method_call --print-reply=literal \
            --dest=org.bluez /org/bluez/hci0 \
            org.freedesktop.DBus.Properties.Set string:org.bluez.Adapter1 string:Discoverable variant:boolean:true
    ;;

    "status")
        if [ -f /tmp/bluetooth-status ]; then
            cat /tmp/bluetooth-status
        else
            echo "Unavailable"
        fi
    ;;

    "address")
        if [ ! -f /tmp/bluetooth-status ] || ( cat /tmp/bluetooth-status | grep -q "Unsupported" ); then
            echo "(none)"
            exit 2
        fi
        hcitool dev | grep hci0 | awk '{ print $2 }'
    ;;

    "name")
        if [ -f /data/bluetooth/name ]; then
            cat /data/bluetooth/name
        elif [ -f /var/cache/mod/tag ]; then
            if [ "${PLATFORM}" = "dwarf" ]; then
                BATCH=$(cat /var/cache/mod/tag | cut -c 7,8)
                SERIAL=$(cat /var/cache/mod/tag | awk '{ gsub("-","\n"); print $2}')
            else
                BATCH=$(cat /var/cache/mod/tag | awk '{ gsub("-","\n"); print $3}')
                SERIAL=$(cat /var/cache/mod/tag | awk '{ gsub("-","\n"); print $6}')
            fi
            echo "${MODEL_NAME} ${BATCH}-${SERIAL}"
        else
            echo "${MODEL_NAME}"
        fi
    ;;

    "hmi")
        # needs to write status, name, address as fast as possible

        # 1. status
        if [ -f /tmp/bluetooth-status ]; then
            STATUS=$(cat /tmp/bluetooth-status)
        else
            STATUS="Unavailable"
        fi

        # 2. name
        if [ -f /data/bluetooth/name ]; then
            NAME=$(cat /data/bluetooth/name | tr -d '|')
        elif [ -f /var/cache/mod/tag ]; then
            if [ "${PLATFORM}" = "dwarf" ]; then
                BATCH=$(cat /var/cache/mod/tag | cut -c 7,8)
                SERIAL=$(cat /var/cache/mod/tag | awk '{ gsub("-","\n"); print $2}')
            else
                BATCH=$(cat /var/cache/mod/tag | awk '{ gsub("-","\n"); print $3}')
                SERIAL=$(cat /var/cache/mod/tag | awk '{ gsub("-","\n"); print $6}')
            fi
            NAME="${MODEL_NAME} ${BATCH}-${SERIAL}"
        else
            NAME="${MODEL_NAME}"
        fi

        # 3. address
        if [ ! -f /tmp/bluetooth-status ] || ( cat /tmp/bluetooth-status | grep -q "Unsupported" ); then
            ADDRESS="(none)"
        else
            ADDRESS=$(hcitool dev 2>/dev/null | grep hci0 | awk '{ print $2 }')
        fi

        echo "${STATUS}|${NAME}|${ADDRESS}"
    ;;

    *)
        echo "${0}: Invalid command"
        exit 1
    ;;
esac

exit 0
