#!/bin/sh

# "Upgrade" Debathena to latest Ubuntu
#
#
#

FORCE=no
if [ "$1" = "--force" ]; then
    FORCE=yes
fi

if [ `id -u` != 0 ]; then
    echo "You must run this script as root."
    exit 1
fi

# Not supported on Debian
if [ "$(lsb_release -si)" != "Ubuntu" ]; then
    echo "This is only supported on Ubuntu.".
    exit 1
fi

if [ "$(machtype -L)" != "debathena-cluster" ]; then
    if [ "$FORCE" = "yes" ]; then
	echo "WARNING: upgrading from $(machtype -L) to -cluster"
    else
    	echo "This is not a cluster machine; won't continue."
    	exit 1
    fi	
fi

[ -x /sbin/kexec ] || ( echo "Can't find kexec" && exit 1 )

UBUNTU_RELEASE=$(lsb_release -sr)
CLUSTERINFO=`getcluster -b $(lsb_release -sr)`
[ $? != 0 ] && echo "Failed to get clusterinfo!!" && exit 1
eval $CLUSTERINFO
if [ ! -z "$NEW_PRODUCTION_RELEASE" ]; then
    NEWCLUSTER=`getcluster -b $NEW_PRODUCTION_RELEASE`
    [ $? != 0 ] && complain "Failed to get new prod clusterinfo" && exit 1
    eval $NEWCLUSTER
#elif [ ! -z "$NEW_TESTING_RELEASE" ] && \
#     [ "$FORCE" = "yes" ]; then
#    NEWCLUSTER=`getcluster -b $NEW_TESTING_RELEASE`
#    [ $? != 0 ] && echo "Failed to get new testing clusterinfo" && exit 1
#    eval $NEWCLUSTER
else
    echo "No new releases."
    exit 0
fi

if [ "$(lsb_release -sc)" = "$UBUNTU_RELEASE" ]; then
    complain "Tried to upgrade to already running release; shouldn't happen"
    exit 1
fi

# That's a space and then a tab inside the brackets
if egrep -q '^flags[ 	].* lm( |$)' /proc/cpuinfo; then 
    arch=amd64
else 
    arch=i386
fi

IPADDR=`ifconfig eth0 |perl -ne'/^\s+inet addr:([\d\.]+)/ && print $1'`
if [ -z "$IPADDR" ]; then
    echo "Couldn't get IP address from ifconfig!"
    exit 1
fi

echo "OK, about to upgrade to $UBUNTU_RELEASE ($arch), using "
echo -n "$IPADDR for the IP address.  Should I continue? [y/N] "
read ans
echo $ans | grep -qi ^y$
if [ $? -ne 0 ]; then
    echo "OK, quitting..."
    exit 0
fi


mkdir /h
cd /h
wget -N http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/initrd.gz || ( echo "Failed to download initrd.gz" && exit 1)
wget -N http://debathena.mit.edu/net-install/${UBUNTU_RELEASE}/${arch}/linux || ( echo "Failed to download kernel" && exit 1)

echo "********************************************************************"
echo "                             DESTROY"
echo "This script will PERMANENTLY DESTROY *ALL* DATA ON THIS MACHINE!"
echo "                             DESTROY"
echo
echo "This script is designed for use on IS&T PUBLIC CLUSTER MACHINES ONLY."
echo "You will NOT BE ABLE TO CUSTOMIZE THE INSTALLATION."
echo 
echo 
echo "-> You have FIFTEEN (15) seconds to power down this machine"
echo "-> before THE ENTIRE HARD DRIVE IS ERASED."
echo
sleep 15

echo "Don't say we didn't warn you..."
sleep 1

# This is just the guts of the hackboot script:
dkargs="DEBCONF_DEBUG=5"
kargs="netcfg/get_hostname= locale=en_US console-setup/layoutcode=us \
       interface=auto \
       url=http://18.9.60.73/installer/${UBUNTU_RELEASE}/debathena.preseed \
       debathena/clusterforce=yes debathena/clusteraddr=$IPADDR --"

/sbin/kexec -l linux --append="$dkargs $kargs" --initrd=initrd.gz \
    && sleep 3 && chvt 1 && sleep 2 && /sbin/kexec -e
echo "Failed to kexec."
exit 1

