#!/bin/bash

. /srv/rdaa/service/.env

mac="$(cat /sys/class/net/eth0/address)"
uid="$(echo $mac | sed 's/://g')"

rawip="$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')"

for address in ${rawip}; do
  if ip ro | grep default | grep -q ${address}; then
    ip=${address}
  fi
done

if [ -z "${ip}" ]; then
  echo "Routable IP address not found, check your network connection"
  exit 1
fi

echo "RDAA_UID=$uid" > /srv/rdaa/service/.uid
hostnamectl set-hostname $uid
sed -i "/127.0.1.1/c\127.0.1.1       $uid" /etc/hosts

shield_version=2

description="RDA will be authorized with uid:"${uid}", mac:"${mac}", ip:"${ip}

auth_request="$(curl -X POST 'https://'$RDBA_HOST_ADDRESS'/authserver-service/oauth/token' -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -H 'X-CSRF-TOKEN: ' -d 'grant_type='$grant_type'&client_id='$client_id'&username='$username_auth'&password='$password_auth'' | jq '.')"
bearer_token="$(echo $auth_request | jq --raw-output '.access_token')"

while [ -z "$bearer_token" ]; do
  auth_request="$(curl-s -X POST 'https://'$RDBA_HOST_ADDRESS'/authserver-service/oauth/token' -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -H 'X-CSRF-TOKEN: ' -d 'grant_type='$grant_type'&client_id='$client_id'&username='$username_auth'&password='$password_auth'' | jq '.')"
  bearer_token="$(echo $auth_request | jq --raw-output '.access_token')"
  echo "Cannot connect to RDBA! Will try another one..."
  echo -e "\nCannot connect to RDBA! Will try another one...\n" > /dev/tty0
  sleep 5
done

response="$(curl -s -X POST 'https://'$RDBA_HOST_ADDRESS'/rdas-service/api/v1/rdas' -H 'Content-Type: application/json' -d '{"uid":"'$uid'","description":"'"$description"'","shieldVersion":"'$shield_version'","ipAddress":"'$ip'"}' -H 'Authorization: Bearer '$bearer_token'')"

while [ -z "$response" ]; do
  response="$(curl -X POST 'https://'$RDBA_HOST_ADDRESS'/rdas-service/api/v1/rdas' -H 'Content-Type: application/json' -d '{"uid":"'$uid'","description":"'"$description"'","shieldVersion":"'$shield_version'","ipAddress":"'$ip'"}' -H 'Authorization: Bearer '$bearer_token'')"
  echo "Cannot connect to RDBA! Will try another one..."
  echo -e "\nCannot connect to RDBA! Will try another one...\n" > /dev/tty0
  sleep 5
done

error="$(echo $response | jq --raw-output '.error')"
if [ ! "$error" = "null" ];then
  echo $error
  echo -e "\n"$error"\n" > /dev/tty0
  if [ ! -d /srv/rdaa/etc ]; then
    mkdir /srv/rdaa/etc
    echo "authorized" > /srv/rdaa/etc/.state
  fi
  exit 0
fi

priv_key="$(echo $response | jq --raw-output '.privateKey')"

if [ ! -z $priv_key ]; then

  if [ ! -d /srv/rdaa/etc ]; then
    mkdir /srv/rdaa/etc
  fi

  rdos_version="$(/usr/local/bin/rdos_common_data os_version)"

  echo "authorized" > /srv/rdaa/etc/.state
  sed -i "s|RDAA_PRIVATE_KEY=.*|RDAA_PRIVATE_KEY=$priv_key|" /srv/rdaa/service/.env
  echo "RDA authorized in DataBase"
  echo "RDOS Version: $rdos_version"
  echo "RDA ENVIRONMENT is: $ENVIRONMENT"
  echo "RDA UID is: $uid"
  echo "RDA IP is: $ip"
  echo -e "\nRDA authorized in DataBase\n" > /dev/tty0
  echo -e "\nRDOS Version: $rdos_version\n" > /dev/tty0
  echo -e "\nRDA ENVIRONMENT is: $ENVIRONMENT\n" > /dev/tty0
  echo -e "\nRDA UID is: $uid\n" > /dev/tty0
  echo -e "\nRDA IP is: $ip\n" > /dev/tty0
  sync
  sleep 30
  exit 0
else
  echo "RDA authorization failed with response "$response
  echo -e "\nRDA authorization failed with response "$response"\n" > /dev/tty0
  exit 1
fi