#!/bin/bash

#
# Deprecated, now inside debian/init
#

function check_interface {
    if ! ifconfig $1 ; then
	echo "Nao existe a interface $1" 
	return 1
    fi
    ifconfig $1 | grep "inet addr"
    if [ ! $? ] ; then
	echo "Interface $1 nao tem IP"
	return 1
    fi
}

function change_hostname {
    FHN=$1
    hostname $FHN
    echo $FHN > /etc/hostname
    echo $FHN > /etc/mailname
    #echo -e "exim4-config    exim4/dc_other_hostnames  string  $HN:$FHN" | debconf-set-selections
    dpkg-reconfigure -phigh -u exim4-config
    #DEBCONF_DEBUG=developer dpkg-reconfigure exim4-config
}

if ! check_interface eth0 ; then
    exit 1
fi
IP=`ifconfig eth0 | grep "inet addr" | awk '{print $2}' | cut -b6- `
echo $IP
FHN=`host -t PTR $IP | awk '{print $5 }'`
if echo $FHN | grep "tagus.ist.utl.pt" ; then
    FHN=${FHN%.}
    HN=`echo $FHN | cut -d. -f1`
    echo $HN
    change_hostname $FHN
else
    if [ `hostname` == "p4-027.tagus.ist.utl.pt" ] ; then
	change_hostname p4-xxx.tagus.ist.utl.pt
    else
	echo "Nao encontro um nome apropriado, deixando o anterior"
    fi
fi
