#!/bin/sh 
#
#  remote.it OEM Settings manages remote.it settings to register OEM devices
#
#  not called directly.  functions are called by connectd_control
#
#  will store info in CONNECTD_DIR
#
#  remot3.it, Inc. : https://remote.it
#

oemGetSystemId()
{
if [ -e $CONNECTD_DIR/cpuid.txt ]; then
    cpuid=$(cat $CONNECTD_DIR/cpuid.txt)
else
    cpuid=$(awk '/Serial/ {print $3}' /proc/cpuinfo | sed 's/://g')

##
## Override default system ID for this platform

# check for legal value
    if [ ${#cpuid} -ge 1 ]; then
       continue
    else
       cpuid="Issue: system ID is not set properly. Fix oemGetSystemId() in /etc/connectd/oem_settings."   
    fi
fi
# return the System ID
echo $cpuid

}

oemGetDefaultHardwareId()
{

#
#  This method gets the hardware ID based on the default LAN MAC address
#  You can overide the network interface below for a custom configuration for your platform
# 

# get the default interface - this is the one currently connected to the internet
interface=$(oemGetInterface)

## 
## Override the default interface - change and uncomment the assignment below
## RPi interfaces: eth0, wlan0
## Ubuntu interfaces: enp3s0 wlp2s0
#interface='eth0'
#interface='enp3s0'

# get the adapter MAC
if [ -f "/sys/class/net/$interface/address" ]; then
    hw_mac=$(cat /sys/class/net/$interface/address | sed 's/://g')
else 
    hw_mac="Issue: hardware ID is not set properly.  Fix oemGetDefaultHardwareId() in /etc/connectd/oem_settings."
fi

echo $hw_mac

}

oemGetDefaultRegistrationKey()
{
 
## 
## Override the default interface - change the assignment below
## RPi interfaces: eth0, wlan0
## Ubuntu interfaces: enp3s0 wlp2s0

## interface must be set to something for your platform - default is for RPi, second option common for Ubuntu
#  The logic below detects the default interface and chooses the other one to use for the Registration Key.
#  This will only work on Raspberry Pi with both eth0 and wlan0 adapters.
#  Edit these expressions to suit the specific platform you are using.

    interface=$(oemGetInterface)
    if [ $interface = "wlan0" ]; then
         interface="eth0"
    elif [ "$interface" = "eth0" ]; then
         interface="wlan0"
    fi
# get the adapter MAC
    if [ -f "/sys/class/net/$interface/address" ]; then
        rk_mac=$(cat /sys/class/net/$interface/address | sed 's/://g')
    else
        interface='wlp2s0'
        if [ -f "/sys/class/net/$interface/address" ]; then
	    rk_mac=$(cat /sys/class/net/$interface/address | sed 's/://g')
        else
            rk_mac="Issue: registration key is not set properly.  Fix oemGetDefaultRegistrationKey() in /etc/connectd/oem_settings"
        fi
    fi

    echo $rk_mac
}

oemGetTCPServices()
{
 
 tcp=$(netstat -antp 2>/dev/null | grep 0.0.0.0: | awk '{ print $4 }' | awk -F':' '{print $2}' | xargs | sed -e 's/ /,/g')
 
 echo $tcp
 
}

oemGetOSLabel()
{
 
# return the OS label
os=$(uname -a)
echo $os

}

oemGetInterface()
{

#
#  This method returns the default interface use for grabbing the hardware ID MAC address. 
#

# host we want to "reach"
host=google.com

# get the ip of that host (works with dns and /etc/hosts. In case we get  
# multiple IP addresses, we just want one of them
host_ip=$(getent ahosts "$host" | awk '{print $1; exit}')

# only list the interface used to reach a specific host/IP. We only want the part
# between dev and src (use grep for that)
interface=$(ip route get "$host_ip" | grep -Po '(?<=(dev )).*(?= src| proto)' | awk '{print $1; exit}')

echo "$interface"

}

oemGetWebPath()
{
 
# return the default web path. Override if different
echo /var/www/html

}

#
#  Hook for OEM to add a function to save configuration changes
#  OEM must add the file in $BIN_DIR, called connectd_save_config
#  it must be set to be executable (chmod +x)
#

oemSaveConfig()
{
# hook for OEM to add platform specific call to save configuration changes
    if [ -e "$BIN_DIR"/connectd_save_config ]; then
        "$BIN_DIR"/connectd_save_config
    fi
}
