#!/bin/bash
# Prepare this node for being used as a base for a hard disk clonable image.
# © 2013, Multapplied Networks, Inc.

if ! dpkg --compare-versions `cat /etc/debian_version` ge 8 ;
then
    echo "$0 can only be run on a Debian Jessie host. Please re-image this host as Debian Jessie and try again."
    exit 1
fi

if [[ -a /var/run/bonding/node.pid ]]
then
    echo "Bonding is running; run 'service bonding stop' before continuing."
    exit 1
fi

printf "This will remove all bonding configuration and logs from this device.\
 Custom hooks and firewall scripts are not removed.\n\n"
printf "There is no recovery from this. Only run this command if you preparing\
 this node to be used as a base image for hard drive cloning.\n\n"

read -p "Are you sure? This cannot be undone. (y/N) " confirm
case ${confirm} in
    [Yy]* ) ;;
    [Nn]* ) exit ;;
    *     ) exit ;;
esac

# Remove the udev net rules, so the new interfaces get named properly
rm -f /etc/udev/rules.d/70-persistent-net.rules

# Run the deconfigure script, keep bonding down
/usr/sbin/bonding-deconfigure -n -q -d

# Reset the hostname to something generic
echo "default-bonder" > /etc/hostname
service hostname.sh start &> /dev/null

# Remove whatever log files we can find
service rsyslog stop &> /dev/null
find /var/log -type f  -exec rm {} \;

# Remove SSH keys - They will be generated by a drop-in for ssh.service next
# time it starts
rm -f /etc/ssh/ssh_host_*

# Clear out the bash history, and ask the user to do the same
rm -f "${HISTFILE}"
unset HISTFILE
echo "To avoid leaving any traces of your bash history, please run:"
echo "unset HISTFILE"
