#! /bin/bash
### BEGIN INIT INFO
# Provides:          x2gohostname
# Required-Start:    networking
# Required-Stop:
# X-Start-Before:    x2gothinclient
# X-Stop-After:      x2gothinclient 
# Default-Start:     2 3 4 5
# Default-Stop:	     0 1 6
# Short-Description: Set hostname based on ethernet address on device eth0
# Description:       Read the machines hostname as ethernet address from 
#                    the network device eth0 and update the kernel value
#                    with this value. ':' are replaced by '-'.
### END INIT INFO

PATH=/sbin:/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start () {
        x2gohostnamefile='/tmp/x2gohostname'
	ifconfig0=$(/sbin/ifconfig eth0)
	ifargs0=( $ifconfig0 )
	ifether0=$(echo ${ifargs0[4]} | /usr/bin/tr ':' '-')
        echo $ifether0 > $x2gohostnamefile
        /bin/hostname -F $x2gohostnamefile
        rm $x2gohostnamefile
}

do_stop () {
	/bin/hostname -F /etc/hostname
}

do_status () {
	HOSTNAME=$(hostname)
	if [ "$HOSTNAME" ] ; then
                echo $HOSTNAME
		return 0
	else
		return 4
	fi
}

case "$1" in
  start|"")
	do_start
	;;
  restart|reload|force-reload)
	echo "Error: argument '$1' not supported" >&2
	exit 3
	;;
  stop)
	do_stop
	;;
  status)
	do_status
	exit $?
	;;
  *)
	echo "Usage: hostname.sh [start|stop]" >&2
	exit 3
	;;
esac

:
