#!/usr/bin/env bash
#checks if there is a hostname in /etc/hosts for WORKER_NAME

[[ -e $RIG_CONF ]] && . $RIG_CONF

if [[ ! -z $WORKER_NAME ]]; then
	hostname=$WORKER_NAME
else
	hostname=worker #fallback hostname
fi


#starts with digits
[[ $hostname =~ ^([0-9]+).* ]] && hostname="hive$WORKER_NAME" #echo "${BASH_REMATCH[1]}"

#if ! grep -qE "127.0.0.1 .*\s+$WORKER_NAME" "/etc/hosts"; then
if ! grep -q "127.0.0.1 $hostname" "/etc/hosts"; then
	#echo "Hostname missing in /etc/hosts, adding"
	printf "\n127.0.0.1 $hostname\n" >> /etc/hosts
fi
#echo "WORKER_NAME=$WORKER_NAME"
hostnamectl set-hostname "$hostname"
