#!/bin/sh
#
# Purpose: Used by dhclient-script to set the hostname of the system
# to match the DNS information for the host as provided by DHCP.
#
# This script is based on code found on the web:
# http://www.debian-administration.org/articles/447 and
# http://nxhelp.com/blog/2013/01/24/automatically-set-hostname-from-dhcp/
#

PATH=/sbin:$PATH
export PATH

# Should not update hostname on Main-Server, Roaming-Workstation and
# Standalone.  Those get their fixed hostname set during installation
# (or manually after installation) and should not change dynamically
# if moved between networks.
if [ -r /etc/debian-edu/config ] ; then
	. /etc/debian-edu/config
	case "$PROFILE" in
	Workstation|Thin-Client-Server|Minimal)
		;;
	*)
		exit 0
		;;
	esac
else
	exit 0
fi

log() {
    logger -t dhclient-exit-hooks.d/hostname "$1"
}

sethostname() {
    hostname="$1"
    namesource="$2"
    echo $hostname > /etc/hostname
    hostname $hostname
    log "changing hostname to $hostname based on $namesource"
}

case "$reason" in
	BOUND|RENEW|REBIND|REBOOT)
	namesource="DHCP IP address $new_ip_address"
	/usr/sbin/update-hostname-from-ip -I $new_ip_address
	;;
esac
