#!/bin/bash
#

function init {
	[ -z "${PROD_DB_TOKEN}" ] && export PROD_DB_TOKEN=$1
	[ -z "${PROD_DB_TOKEN}" ] && echo "please specify PROD_DB_TOKEN as the first argument" && exit 1

	[ -z "${TTQN}" ] && export TTQN=$2
	[ -z "${TTQN}" ] && echo "please specify TTQN as the third argument" && exit 1

	[ -z "${PROD_DB_SERVER}" ] && export PROD_DB_SERVER=$3
	[ -z "${PROD_DB_SERVER}" ] && export PROD_DB_SERVER="https://production-db.vespy.tech"

	echo "PROD_DB_TOKEN: ${PROD_DB_TOKEN}"
	echo "PROD_DB_SERVER: ${PROD_DB_SERVER}"
	echo "TTQN: ${TTQN}"
}

function fetch_serial_number {
	local SERIAL=$(cat /proc/cpuinfo | grep Serial | awk '{print $3}')
	# http -v --ignore-stdin -A bearer -a ${PROD_DB_TOKEN} ${PROD_DB_SERVER}/api/v1/service/Hub/update-field/${TTQN}/mpu_serial_number value=${SERIAL}
	curl ${PROD_DB_SERVER}/api/v1/service/Hub/update-field/${TTQN}/mpu_serial_number \
		--oauth2-bearer ${PROD_DB_TOKEN} \
		-H 'content-type: application/json' \
		-H 'accept: application/json, */*;q=0.5' \
		-d "{\"value\":\"${SERIAL}\"}"
}

init $@ && fetch_serial_number
