#!/usr/bin/env bash

props='{
  "hostname":"**HOSTNAME**",
  "ostype":"Linux",
  "osversion":"**OSVERSION**",
  "boottype":"**BOOTTYPE**",
  "cpu":**CPU**,
  "ram":**RAM**,
  "netinfo":**INTERFACES**,
  "diskinfo":**DISKS**,
  "connectivity":{"AWS":**AWS**,
                  "OBS":**OBS**,
                  "NBB":**NBB**}
}'

source /etc/os-release
hostname=$(hostname -s)
cpuCount=$(grep -c ^processor /proc/cpuinfo)
ramCount=$(awk '/MemTotal/ { printf "%.3f \n", $2/1024/1024 }' /proc/meminfo)
ramCount=$(echo $ramCount | awk '{print int($1+0.5)}')
if [ "$ramCount" -lt "1" ]; then
  ramCount=1
fi
props=${props/\*\*HOSTNAME\*\*/$hostname}
props=${props/\*\*OSVERSION\*\*/$PRETTY_NAME}
props=${props/\*\*CPU\*\*/$cpuCount}
props=${props/\*\*RAM\*\*/$ramCount}
if [ -d "/sys/firmware/efi" ];then
  props=${props/\*\*BOOTTYPE\*\*/"EFI"}
else
  props=${props/\*\*BOOTTYPE\*\*/"BIOS"}
fi


blockDevices=$(find /sys/block -name 'sd*' -o -name 'vd*' -o -name 'xvd*' -o -name 'hd*' | sed 's/sys\/block/dev/')
bootDevice=$(fdisk -l | grep '*' | cut -d" " -f1 | sed 's/[0-9]*$//')
disks=('[')
for f in $blockDevices;do
  newDisk='{"mountpoint":"**MOUNTPOINT**","size":**SIZE**}'
  if [ "$f" == "$bootDevice" ];then
    mountpoint="root"
    # root=$(fdisk -l | grep -E "${f}[^0-9]" | cut -d" " -f2)
  else
    mountpoint=$(echo ${f} | cut -d"/" -f3)
  fi

  devSize=$(blockdev --getsize64 ${f} | awk '{byte =$1 /1024/1024**2; print byte }' | awk '{print int($1+0.5)}')
  if [ "$devSize" -lt "1" ]; then
    devSize=1
  fi
  newDisk=${newDisk/\*\*MOUNTPOINT\*\*/$mountpoint}
  newDisk=${newDisk/\*\*SIZE\*\*/$devSize}
  disks=(${disks[@]} $newDisk)
  disks=(${disks[@]} ",")
done
unset 'disks[${#disks[@]}-1]'
disks=(${disks[@]} "]")

props=${props/\*\*DISKS\*\*/${disks[@]}}

interfaces=$(ip addr | awk '
/^[0-9]+:/ {
  sub(/:/,"",$2); iface=$2 }
/^[[:space:]]*inet / {
  split($2, a, "/")
  print iface" : "a[1]
}')
ifArray=("[")
while read i;do
if echo "$i" | grep -q ": 127." ;
then
  continue
else
  newIf='{"alias":"**ALIAS**","ipaddress":"**IPADDRESS**"}'
  ifName=$(echo "$i" | awk '{print $1}')
  ifIp=$(echo "$i" | awk '{print $3}')
  newIf=${newIf/\*\*ALIAS\*\*/$ifName}
  newIf=${newIf/\*\*IPADDRESS\*\*/$ifIp}
  ifArray=(${ifArray[@]} $newIf)
  ifArray=(${ifArray[@]} ",")
fi
done <<< "${interfaces}"
unset 'ifArray[${#ifArray[@]}-1]'
ifArray=(${ifArray[@]} "]")

props=${props/\*\*INTERFACES\*\*/${ifArray[@]}}

OBSUrl="OBS,https://nuabee-operation.oss.eu-west-0.prod-cloud-ocb.orange-business.com/testDiscovery"
AWSUrl="AWS,https://s3.eu-west-3.amazonaws.com/nuabee-operation/testDiscovery"
NBBUrl="NBB,https://api.nuabee.fr/up"
for url in "${OBSUrl}" "${AWSUrl}" "${NBBUrl}";do
  IFS=","
  set $url
  urlstatus=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "${2}")
  props=${props/\*\*${1}\*\*/${urlstatus}}
  unset IFS
done
echo $props

if [ -z ${GITLAB_CI:+x} ];
then
  echo "Envoi des données à l'API"
else
  echo "Mode CI, pas d'envoi à l'API"
  exit
fi

curl -X POST https://api.nuabee.fr/dev/discovery -H "Content-Type: application/json" --data "${props}"
