#!/bin/sh
#
# Change the IP/hostname on cluster machines
#

# Required for recovery-mode scripts
if [ "$1" = "test" ]; then
  echo "Change IP address or hostname"
  exit 0
fi

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root."
    exit 0
fi


# Blatantly stolen from the installer
ask() {
  answer=''
  while [ y != "$answer" -a n != "$answer" ]; do
    echo -n "$1"
    read answer
    [ Y = answer ] && answer=y
    [ N = answer ] && answer=n
    [ -z "$answer" ] && answer=$2
  done
}

bail() {
    echo
    echo "Cancelled.  Press Enter to the menu."
    read dummy
    exit 0;
}

# sulogin gets upset if it gets a ^C
trap bail INT

PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"

if [ "$DEBATHENA_DEBUG" = "1" ]; then
    echo "**********\nDEBUG MODE\n**********"
    MASKS="$(pwd)/masks";
    NETPARAMS="$(pwd)/netparams";
    . "$(pwd)/require_network.sh"
else
    MASKS="/usr/share/debathena-recovery-mode-config/masks";
    NETPARAMS="/usr/share/debathena-recovery-mode-config/netparams";
    . /usr/share/debathena-recovery-mode-config/require_network.sh
fi

if [ "$DEBATHENA_DEBUG" != "1" ] && \
   [ "$(machtype -L)" != "debathena-cluster" ]; then
    echo "WARNING: This script is designed for debathena-cluster machines"
    echo "but this machine is running $(machtype -L).\n";
    ask "Are you SURE you want to continue? (y/N) " n
    if [ "$answer" = "n" ]; then
	exit 0
    fi
fi

echo "Testing networking, please wait..."
if ! require_network; then 
    echo "Can't verify that networking is available.  If you continue,"
    ask "errors may occur.  Continue? (y/N) " n
    if [ "$answer" = "n" ]; then
	exit 0
    fi
fi

while true; do
    while [ -z "$IPADDR" ] ; do
	echo -n "Enter IP address: "
	read IPADDR
    done

    NETMASK=`$NETPARAMS -f $MASKS $IPADDR|cut -d\  -f 1`
    GATEWAY=`$NETPARAMS -f $MASKS $IPADDR|cut -d\  -f 4`

    echo "Address: $IPADDR"
    echo
    echo "Autoconfigured settings:"
    echo "  Netmask: $NETMASK"
    echo "  Gateway: $GATEWAY"
    echo
    ask "Is this correct? (Y/n) " y
    [ "$answer" = "y" ] || continue
    echo "Looking up hostname...";
    FQDN="$(dig +short -x $IPADDR 2>/dev/null | sed -e 's/.$//')"
    if [ $? != 0 ]; then
	echo "Unable to look up hostname.  You may enter it below"
	echo "but beware that typos may render your machine unusable."
	FQDN=
	while [ -z "$FQDN" ] ; do
	    echo -n "Enter hostname: "
	    read FQDN
	    FQDN="$(echo $FQDN | tr -d ' ')"
            if echo "$FQDN" | egrep -qi "[^a-z0-9_-\.]"; then
		echo "Invalid characters in hostname, try again."
		FQDN=
	    fi
	done
    fi	
    if ! echo "$FQDN" | grep -qi "MIT.EDU$"; then
	FQDN="$FQDN.MIT.EDU"
    fi
    echo "Hostname: $FQDN"
    ask "Is this correct? (Y/n) " y
    if [ "$answer" = "y" ]; then
	break
    fi
done
HOSTNAME="$(echo "$FQDN" | cut -d. -f 1)"
DOMAIN="$(echo "$FQDN" | sed 's/[^\.]*\.//')"
if [ "$DEBATHENA_DEBUG" = "1" ]; then
    echo "$IPADDR $NETMASK $GATEWAY $HOSTNAME $DOMAIN $FQDN"
    echo "Done!"
    exit 0
fi
echo "Updating /etc/network/interfaces..."
mv -f "/etc/network/interfaces" "/etc/network/interfaces.old"
cat <<EOF >/etc/network/interfaces
# This file was created by athena-renumber

# The loopback interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address $IPADDR
        netmask $NETMASK
        gateway $GATEWAY
        dns-nameservers 18.70.0.160 18.71.0.151 18.72.0.3
        dns-search mit.edu
EOF
echo "Updating /etc/hosts..."
mv -f "/etc/hosts" "/etc/hosts.old"
cat <<EOF >/etc/hosts
# This file was created by athena-renumber
127.0.0.1       localhost
127.0.1.1       $fqdn $hostname

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
EOF
echo "Updating /etc/hostname..."
mv -f /etc/hostname /etc/hostname.old
echo $HOSTNAME > /etc/hostname
echo "Setting hostname...";
hostname $HOSTNAME
echo
echo "Done!  You will need to reboot the workstation for the changes to take effect."
ask "Would you like to reboot now? (Y/n) " y
if [ "$answer" = "n" ]; then
    echo "Please perform a full shutdown and reboot as soon as possible."
    echo "(Press Enter to return to the menu)"
    exit 0
fi
reboot
