#!/usr/bin/env bash
set -euo pipefail

detect() {
    return 0
}

virt=none
if systemd-detect-virt > /dev/null 2>&1 ; then
    virt=$(systemd-detect-virt)
fi

echo "<<<labels:sep(0)>>>"
echo "{\"machine/virtualization\": \"${virt}\"}"
if [ $virt == none ]; then
    ecc=$(dmidecode --type 16 | grep -m1 "Error Correction Type" | cut -d : -f 2)
    mb_name=$(dmidecode --type 2 | grep -m1 "Product Name" | cut -d: -f2)
    mb_manufacturer=$(dmidecode --type 2 | grep -m1 "Manufacturer" | cut -d: -f2)
    mb_serial=$(dmidecode --type 2 | grep -m1 "Serial Number" | cut -d: -f2)
    cpu_model=$(dmidecode --type 4 | grep -m1 "Version" | awk -F: '{gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2}')
    echo "{\"machine/ecc\": \"${ecc:1}\"}"
    echo "{\"mainboard/manufacturer\": \"${mb_manufacturer:1}\"}"
    echo "{\"mainboard/product\": \"${mb_name:1}\"}"
    echo "{\"mainboard/serial\": \"${mb_serial:1}\"}"
    echo "{\"cpu/model\": \"${cpu_model}\"}"
fi

echo "{\"cpu/threads\": \"$(nproc)\"}"

source /etc/os-release

echo "{\"os/distribution\": \"${ID}\"}"
echo "{\"os/version\": \"${VERSION_ID}\"}"
echo "{\"os/codename\": \"${VERSION_CODENAME}\"}"


echo "<<<local:sep(0)>>>"
echo "0 \"Last Execution\" - $(date '+%Y-%m-%d %H:%M:%S')"