#!/bin/bash
# VERSION=3
# CHANGES=create entry in /etc/hosts

ENABLED=1

function sethostname {
	UUID=`expr match "$(cat /proc/cmdline)" ".*carduuid=[0-9]-[0-9]\{2\}-[0]\{0,9\}\(.*\) "`
	HOSTNAME_NEW="berofix${UUID}"
	HOSTNAME_OLD="$(cat /etc/hostname)"

	if [ "${HOSTNAME_NEW}" != "${HOSTNAME_OLD}" ]; then
		echo -n " * Setting hostname: "
		mount -oremount,rw /
		echo ${HOSTNAME_NEW} > /etc/hostname
		mount -oremount,ro /
		echo "Done."
	fi

	if [ -z "$(grep ${HOSTNAME_NEW} /etc/hosts)" ]; then
		mount -oremount,rw /
		echo -e "127.0.0.1\tlocalhost\n127.0.1.1\t${HOSTNAME_NEW}" > /etc/hosts
		mount -oremount,ro /
	fi
}

case ${1} in
	start)
		if [ ${ENABLED} -eq 1 ]; then
			sethostname
		fi
		;;
	stop)
		if [ ${ENABLED} -eq 1 ]; then
			sethostname
		fi
		;;
esac
